不知道为什么,明明我的service结构也没写错,做些小事也可以,但是将传感器放在里面运行就会导致程序闪退。
代码如下:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
bt=(Button)view.findViewById(R.id.startSpeed);
t1=(TextView)view.findViewById(R.id.showSpeed);
//接受service发出的广播,注册监听
rec = new MyReceiver();
getActivity().registerReceiver(rec,new IntentFilter("fromSpeedService"));
/*开始测速监听*/
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取服务对象
Intent it= new Intent(getActivity(), SpeedService.class);
//检测服务是否已经在运行,防止二次启动
if(isServiceRunning())
Toast.makeText(getActivity(),"测速服务已在运行",Toast.LENGTH_SHORT).show();
else{
getActivity().startService(it);
Toast.makeText(getActivity(),"已开始后台测速",Toast.LENGTH_SHORT).show();
}
}
});
}
//接受广播消息处理
private class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent){
t1.setText(intent.getStringExtra("i"));
}
}
public class SpeedService extends Service {
private final String TAG="dd";
private TextView t1,t2,t3,t4,t5;
private SensorManager speedManager;
private Sensor sensor;
private Listener listener;
private Intent it;
private PowerManager powerManager=null;
private PowerManager.WakeLock wakeLock =null;
public SpeedService() {
it=new Intent("fromSpeedService");
}
@Override
public void onCreate(){
super.onCreate();
new Thread(new Runnable() {
@Override
public void run() {
/*int i=10;
it.putExtra("i",Integer.toString(i));
sendBroadcast(it);*/
//获取传感器管理器
speedManager=(SensorManager)getSystemService(SENSOR_SERVICE);
//检测是否存在该传感器
sensor=speedManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);
if(sensor==null) {
Toast.makeText(SpeedService.this,"不存在加速度传感器!",Toast.LENGTH_SHORT).show();
} else{
//给传感器注册监听器
listener=new Listener();
speedManager.registerListener(listener,sensor,SensorManager.SENSOR_DELAY_NORMAL);
Toast.makeText(SpeedService.this,"111111",Toast.LENGTH_SHORT).show();
}
/*
powerManager=(PowerManager)getSystemService(Context.POWER_SERVICE);
wakeLock=powerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK,this.getClass().getName());
wakeLock.acquire();*/
}
}).start();
}
@Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet impelmented");
}
//启动service
@Override
public int onStartCommand(Intent intent,int flags,int startId){
return super.onStartCommand(intent,flags,startId);
}
//结束服务时取消注册监听
@Override
public void onDestroy(){
speedManager.unregisterListener(listener);
super.onDestroy();
}
//重载的传感器监听器类
private class Listener implements SensorEventListener {
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy){ }
@Override
public void onSensorChanged(SensorEvent event) {
int sensorType=event.sensor.getType();
//获取传感器类型
if(sensorType==Sensor.TYPE_LINEAR_ACCELERATION){
float[] value=event.values;
float acc=(float)Math.sqrt(Math.pow(value[0],2)+Math.pow(value[1],2)+Math.pow(value[2],2));
String aa=Integer.toString(Math.round(acc));
it.putExtra("i",aa);
sendBroadcast(it);
}
}
//处理速度与加速度
public void handleData(int speed,int Aspeed){
}
}
}
麻烦大佬们帮我看一下,这个service我是通过fragment里的一个按钮启动的,但每次一启动程序就闪退,头发都要愁掉光了。。。