1

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

PS 在我的 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;
}
}
}
4

1 回答 1

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;
}

}

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

于 2013-07-29T03:01:23.677 回答