一般來說Android手機開發人員在開發APP時總是會需要知道自己的測試手機有哪些硬體,
這裡提供一個方法包含源碼可以供測試
然後為了方便在手機上顯示
可以如圖去設定ListView
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class SensorActivity extends Activity {
private ListView sensorListView;///宣告物件可以用create field可以方便建立
private SensorManager sensor_manager;
private Context context;
private final String TAD="Sensor_List";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sensor);
context=this;
sensorListView =(ListView)findViewById(R.id.listView_id);/////R.id 與 R.java差別
sensor_manager=(SensorManager)getSystemService(SENSOR_SERVICE);//(SensorManager)強制轉型
List<Sensor> sensorList=sensor_manager.getSensorList(Sensor.TYPE_ALL);//<sensor>是泛型,<sensor>包含很多資料然後丟到List
List<String> listName =new ArrayList<>();//android7.0以後不用寫
for(Sensor sensor:sensorList){
listName.add(sensor.getType()+"."+sensor.getName()+"+"+sensor.getPower()+"mA");//getType用來取得不同的sensor id Name power
}
ArrayAdapter <String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,listName);//this =context,而android是用在手機上,如果只是模擬就不要寫就可以了
sensorListView.setAdapter(adapter);
setTitle("Sensor List :"+listName.size());
sensorListView.setOnItemClickListener(itemOnClick);
Toast.makeText(this,"123",Toast.LENGTH_SHORT).show();
}
private AdapterView.OnItemClickListener itemOnClick=new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Sensor sensor;
Intent newActivityIntent;
Log.d(TAD,"Item on click");///此為測試,因為中間有debug
String sensorName =adapterView.getItemAtPosition(position).toString();///告訴你在哪個位置並取出位置然後變成String(toString)
Toast.makeText(context,sensorName,Toast.LENGTH_SHORT).show();//然後Toast讓它顯示出來我才知道對不對,然後只顯示一下下時間(LENGTH_SHORT)
Log.d(TAD,"toast ok");///此為測試,因為中間有debug
int index =sensorName.indexOf(".");//把string轉成interger方便找起始點與結束點
String sensorType=sensorName.substring(0,index);//0,index找0~結束點
int sensorId=Integer.valueOf(sensorType);
Log.d(TAD,"switch start"+sensorId);
switch (sensorId){
case Sensor.TYPE_ACCELEROMETER:
sensor=sensor_manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
if(sensor==null){
Toast.makeText(context,"there is no ACC sensor:",Toast.LENGTH_SHORT).show();
}else {
newActivityIntent =new Intent(context,ACCMeterActivity.class);
Log.d(TAD,"Intent start");///此為測試,因為中間有debug
startActivity(newActivityIntent);
}
break;
case Sensor.TYPE_LIGHT:
break;
case Sensor.TYPE_PROXIMITY:
break;
default:
Toast.makeText(context,"This function does not work",Toast.LENGTH_SHORT).show();
break;
}
}
};
}
一旦設定好了就可以測試囉
新的手機通常Sensor會比較多
BY 緩緩成地仙
留言列表