android for-each and for different

本文深入探讨了在Android设备上使用传感器进行数据收集的方法。通过具体的代码示例,展示了如何利用SensorManager获取各种传感器类型,包括加速度计、陀螺仪等,并通过for循环输出传感器信息。同时,对比了foreach与for循环在日志输出上的表现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 when  use for-each to do some things

 

for example

 


package io.flutter.plugins;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;


import java.util.List;

import tool.sport.sport_app.R;

import static android.content.Context.SENSOR_SERVICE;

public class MainActivityLaunch extends AppCompatActivity  implements SensorEventListener {



    private  SensorManager mSensorManager ;
    private  Sensor mAcceleromet;
    private  List<Sensor> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        mSensorManager  =  (SensorManager) getSystemService(SENSOR_SERVICE);
        mAcceleromet = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);


        list=mSensorManager.getSensorList(Sensor.TYPE_ALL);

        System.out.println("  --------------------1------------------------ ")  ;
        System.out.println("  ---------------------2----------------------- ")  ;
        System.out.println("  ---------------------3----------------------- ")  ;

        System.out.println("  ---------------------4----------------------- ")  ;
        System.out.println("  -----------------------5--------------------- ")  ;

        for(int i=0;i< list.size();i++){



            Log.d(""+list.get(i).getName(),"--------------------------------------------"+  "  liuzhiwei " );
        }


        System.out.println("  ---------------------<<<<<<<<<6->>>>>>>>>>---------------------- ")  ;
        System.out.println("  -----------------------<<<<<<<<7->>>>>>>>>>>>-------------------- ")  ;

        try {
            for(Sensor sensor:list  ){


                Log.d("","--------qqqqqqq-----------"+sensor.getName() );


            }
        } catch (Exception e) {
            e.printStackTrace();
        }


        System.out.println("  -------------------8------------------------ ")  ;

        System.out.println("  ------------------9-------------------------- ")  ;
        System.out.println("  -------------------10------------------------- ")  ;

        System.out.println("  --------------------11------------------------ ")  ;

        System.out.println("  --------------------12------------------------ ")  ;


        System.out.println("  -------------------------------------------- ")  ;

        System.out.println("  -------------------------------------------- ")  ;


        System.out.println("  -------------------------------------------- ")  ;

        System.out.println("  -------------------------------------------- ")  ;







    }

    protected void onResume(){
        super.onResume();
        mSensorManager.registerListener(this,mAcceleromet,SensorManager.SENSOR_DELAY_NORMAL);
    }

    protected void onPause(){
        super.onPause();
        mSensorManager.unregisterListener(this);
    }



    /**
     * Called when there is a new sensor event.  Note that "on changed"
     * is somewhat of a misnomer, as this will also be called if we have a
     * new reading from a sensor with the exact same sensor values (but a
     * newer timestamp).
     *
     * <p>See {@link SensorManager SensorManager}
     * for details on possible sensor types.
     * <p>See also {@link SensorEvent SensorEvent}.
     *
     * <p><b>NOTE:</b> The application doesn't own the
     * {@link SensorEvent event}
     * object passed as a parameter and therefore cannot hold on to it.
     * The object may be part of an internal pool and may be reused by
     * the framework.
     *
     * @param event the {@link SensorEvent SensorEvent}.
     */
    @Override
    public void onSensorChanged(SensorEvent event) {

    }

    /**
     * Called when the accuracy of the registered sensor has changed.  Unlike
     * onSensorChanged(), this is only called when this accuracy value changes.
     *
     * <p>See the SENSOR_STATUS_* constants in
     * {@link SensorManager SensorManager} for details.
     *
     * @param sensor
     * @param accuracy The new accuracy of this sensor, one of
     *                 {@code SensorManager.SENSOR_STATUS_*}
     */
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }
}

 

for each   can not output anythings   about log  in android studio

 

for loop can  output  

 

 

09-18 11:51:40.213 2568-2568/tool.sport.sport_app I/System.out:   --------------------1------------------------ 
09-18 11:51:40.213 2568-2568/tool.sport.sport_app I/System.out:   ---------------------2----------------------- 
09-18 11:51:40.213 2568-2568/tool.sport.sport_app I/System.out:   ---------------------3----------------------- 
09-18 11:51:40.213 2568-2568/tool.sport.sport_app I/System.out:   ---------------------4----------------------- 
09-18 11:51:40.213 2568-2568/tool.sport.sport_app I/System.out:   -----------------------5--------------------- 
09-18 11:51:40.213 2568-2568/tool.sport.sport_app D/MPU6500 Acceleration Sensor: --------------------------------------------  liuzhiwei 
09-18 11:51:40.213 2568-2568/tool.sport.sport_app D/MPU6500 Gyroscope Sensor: --------------------------------------------  liuzhiwei 
09-18 11:51:40.213 2568-2568/tool.sport.sport_app D/MPU6500 Uncalibrated Gyroscope Sensor: --------------------------------------------  liuzhiwei 
09-18 11:51:40.213 2568-2568/tool.sport.sport_app D/AK09911C Magnetic field Sensor: --------------------------------------------  liuzhiwei 
09-18 11:51:40.213 2568-2568/tool.sport.sport_app D/AK09911C Magnetic Sensor UnCalibrated: --------------------------------------------  liuzhiwei 
09-18 11:51:40.213 2568-2568/tool.sport.sport_app D/Barometer Sensor: --------------------------------------------  liuzhiwei 
09-18 11:51:40.213 2568-2568/tool.sport.sport_app D/TMG399X Proximity Sensor: --------------------------------------------  liuzhiwei 
09-18 11:51:40.213 2568-2568/tool.sport.sport_app D/TMG399X RGB Sensor: --------------------------------------------  liuzhiwei 
09-18 11:51:40.213 2568-2568/tool.sport.sport_app D/MPL Rotation Vector: --------------------------------------------  liuzhiwei 
09-18 11:51:40.213 2568-2568/tool.sport.sport_app D/MPL Game Rotation Vector: --------------------------------------------  liuzhiwei 
09-18 11:51:40.213 2568-2568/tool.sport.sport_app D/SAMSUNG Step Detector Sensor: --------------------------------------------  liuzhiwei 
09-18 11:51:40.213 2568-2568/tool.sport.sport_app D/SAMSUNG Step Counter Sensor: --------------------------------------------  liuzhiwei 
09-18 11:51:40.213 2568-2568/tool.sport.sport_app D/SAMSUNG Significant Motion Sensor: --------------------------------------------  liuzhiwei 
09-18 11:51:40.213 2568-2568/tool.sport.sport_app D/Screen Orientation Sensor: --------------------------------------------  liuzhiwei 
09-18 11:51:40.213 2568-2568/tool.sport.sport_app D/Orientation Sensor: --------------------------------------------  liuzhiwei 
09-18 11:51:40.213 2568-2568/tool.sport.sport_app D/Gravity Sensor: --------------------------------------------  liuzhiwei 
09-18 11:51:40.213 2568-2568/tool.sport.sport_app D/Linear Acceleration Sensor: --------------------------------------------  liuzhiwei 
09-18 11:51:40.213 2568-2568/tool.sport.sport_app I/System.out:   ---------------------<<<<<<<<<6->>>>>>>>>>---------------------- 
09-18 11:51:40.213 2568-2568/tool.sport.sport_app I/System.out:   -----------------------<<<<<<<<7->>>>>>>>>>>>-------------------- 
09-18 11:52:03.703 2568-2568/tool.sport.sport_app I/System.out:   -------------------8------------------------ 
09-18 11:52:03.703 2568-2568/tool.sport.sport_app I/System.out:   ------------------9-------------------------- 
09-18 11:52:03.703 2568-2568/tool.sport.sport_app I/System.out:   -------------------10------------------------- 
09-18 11:52:03.703 2568-2568/tool.sport.sport_app I/System.out:   --------------------11------------------------ 
09-18 11:52:03.703 2568-2568/tool.sport.sport_app I/System.out:   --------------------12------------------------ 
09-18 11:52:03.713 2568-2568/tool.sport.sport_app I/System.out:   -------------------------------------------- 
09-18 11:52:03.713 2568-2568/tool.sport.sport_app I/System.out:   -------------------------------------------- 
09-18 11:52:03.713 2568-2568/tool.sport.sport_app I/System.out:   -------------------------------------------- 
09-18 11:52:03.713 2568-2568/tool.sport.sport_app I/System.out:   -------------------------------------------- 
09-18 11:52:03.733 2568-2568/tool.sport.sport_app D/SensorManager: registerListener :: 0, MPU6500 Acceleration Sensor, 200000, 0,  
09-18 11:52:03.753 2568-2568/tool.sport.sport_app D/SecWifiDisplayUtil: Metadata value : none
09-18 11:52:03.773 2568-2568/tool.sport.sport_app D/ViewRootImpl: #1 mView = com.android.internal.policy.PhoneWindow$DecorView{4f01ee2 I.E...... R.....ID 0,0-0,0}
09

 

### Android Studio Project Build-Logic Module Purpose and Usage In the context of an Android Studio project, the `build-logic` module serves as a centralized location for defining common build configurations across multiple modules within a multi-module project[^1]. This approach enhances maintainability by ensuring consistency in configuration settings such as plugin application, dependency management, and property definitions. #### Centralized Configuration Management The primary function of this module is to encapsulate shared Gradle logic used throughout different parts of the project. By isolating these concerns into one place, developers avoid duplicating code snippets related to dependencies or plugins inside each subproject's `build.gradle` files. Instead, all relevant scripts reside under the `buildSrc` directory (or another designated folder), which acts like any other standard Java/Kotlin source set but specifically tailored towards customizing builds[^2]. For instance: ```groovy // File path: build-logic/convention/plugins/android-library-conventions.gradle.kts plugins { id("com.android.library") kotlin("android") } android { compileSdkVersion(33) defaultConfig { minSdk = 21 targetSdk = 33 } } ``` This script applies consistent settings for Android libraries without needing repetition elsewhere. #### Dependency Version Control Another significant advantage lies in managing versions centrally through properties defined either directly within Groovy/Kotlin DSLs or via external JSON/YAML files loaded during evaluation time. Such practice reduces errors caused by mismatched artifact revisions while making updates straightforward since only single entries require modification instead of scattered occurrences spread over numerous places[^3]: Example setup might look something similar below where specific API levels are managed uniformly: ```kotlin object Versions { const val COMPILE_SDK_VERSION = 33 const val MIN_SDK_VERSION = 21 const val TARGET_SDK_VERSION = 33 object Libraries { const val KOTLIN = "1.8.0" const val COROUTINES = "1.6.4" } } ``` Then referenced accordingly when declaring dependencies: ```groovy dependencies { implementation("org.jetbrains.kotlin:kotlin-stdlib:${Versions.Libraries.KOTLIN}") implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:${Versions.Libraries.COROUTINES}") } ``` --related questions-- 1. How does integrating third-party libraries impact performance optimization strategies? 2. What best practices should be followed regarding organizing large-scale applications with many feature modules? 3. Can you provide examples demonstrating how to implement continuous integration pipelines using GitHub Actions alongside Android projects configured with separate build-logic directories? 4. In what ways do modern IDE features facilitate debugging complex issues arising from misconfigurations between interdependent components?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值