用于控制接近传感器的后台服务

时间:2013-07-19 12:46:00

标签: android android-service proximity background-service proximitysensor

我需要运行一项服务(总是在后台,我希望服务不断检查,即使应用程序已关闭),它控制接近传感器的状态,并且当它被覆盖时启动一项活动。我试过,但我有错误。有这样的例子吗?

P.S。在我的AndroidManifest中,我添加了:

            <service
            android:name="xxx.xxxxx.xxxxx.MyService"
            android:enabled="true" />

这里是我的MyService.java :( Eclipse不会报告任何错误,但是当我在我的设备上尝试它时,应用会关闭力量。)

import android.app.Service;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.IBinder;
import android.widget.Toast;


public class MyService extends Service implements SensorEventListener {
Sensor proxSensor;
SensorManager sm;

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    Toast.makeText(this, "Service Created", Toast.LENGTH_LONG).show();
    sm=(SensorManager)getSystemService(SENSOR_SERVICE);
    proxSensor=sm.getDefaultSensor(Sensor.TYPE_PROXIMITY);

    sm.registerListener(this, proxSensor, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
    // TODO Auto-generated method stub

}

@Override
public void onSensorChanged(SensorEvent event) {
    // TODO Auto-generated method stub
    Intent panel = new Intent(this, Panel.class);
    startActivity(panel);
}
@Override
public void onDestroy() {
    Toast.makeText(this, "Service Stopped", Toast.LENGTH_LONG).show();
}

@Override
public void onStart(Intent intent, int startid) {
    Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
}

    }

这是我的Main.java:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.lino99.smartTask.R;

public class Main extends Activity implements OnClickListener {
Button buttonStart, buttonStop;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

buttonStart = (Button) findViewById(R.id.buttonStart);
buttonStop = (Button) findViewById(R.id.buttonStop);

buttonStart.setOnClickListener(this);
buttonStop.setOnClickListener(this);
}

public void onClick(View src) {
switch (src.getId()) {
case R.id.buttonStart:
  startService(new Intent(this, MyService.class));
  break;
case R.id.buttonStop:
  stopService(new Intent(this, MyService.class));
  break;
}
}
}

1 个答案:

答案 0 :(得分:4)

好的,您的代码中的一些更改可能会清除您的错误并使您的服务继续进行并且您的接近传感器响应:

这就是您的服务应该是这样的:

@Override
public void onCreate() {//onCreat shouldn't be used for sensor u should use onStartCommand
    Toast.makeText(this, "Service Created", Toast.LENGTH_LONG).show();
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
    // TODO Auto-generated method stub

}

@Override
public void onSensorChanged(SensorEvent event) {
    // TODO Auto-generated method stub
    //here u should use the following code otherwise your sensor will be crazy
    if(event.values[0] == 0){
    Intent panel = new Intent(this, Panel.class);
    startActivity(panel);
    }
}
@Override
public void onDestroy() {//here u should unregister sensor
    Toast.makeText(this, "Service Stopped", Toast.LENGTH_LONG).show();
    sm.unregisterListener(this);
}

@Override//here u should register sensor and write onStartCommand not onStart
public int onStartCommand(Intent intent, int flags, int startId) {
    Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
    sm=(SensorManager)getSystemService(SENSOR_SERVICE);
    proxSensor=sm.getDefaultSensor(Sensor.TYPE_PROXIMITY);
    sm.registerListener(this, proxSensor, SensorManager.SENSOR_DELAY_NORMAL);

    //here u should make your service foreground so it will keep working even if app closed

    NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Intent bIntent = new Intent(MyService.this, MainActivity.class);       
    PendingIntent pbIntent = PendingIntent.getActivity(MyService.this, 0 , bIntent, 0);
    NotificationCompat.Builder bBuilder =
            new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("Title")
                .setContentText("Subtitle")
                .setAutoCancel(true)
                .setOngoing(true)
                .setContentIntent(pbIntent);
    barNotif = bBuilder.build();
    this.startForeground(1, barNotif);

        //then you should return sticky
        return Service.START_STICKY;
}

}

好的,现在试试代码,让我知道它是如何为你工作的,希望我能帮助你。