我试过报警管理器。有用。我在 AlarmReceiver 中使用了 Contex.startService()。我可以检查我的服务是否正在运行,以决定是否重新启动服务。因此,即使我的服务被用户在设置中手动停止或被某些清洁应用程序杀死,该服务也可以再次启动。这是下面的主要代码。
package com.example.androidnetwork;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
private static final long INTERVAL = 5 * 1000; // unit: ms
private Button mRegisterReceiverBtn;
private Button mCheckNetStatusBtn;
private Button mStopButton;
private AlarmManager mAlarmManager;
private PendingIntent mPendingIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
initView();
}
private void initData() {
mAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
}
private void initView() {
mRegisterReceiverBtn = (Button) findViewById(R.id.activity_main_start_btn);
mCheckNetStatusBtn = (Button) findViewById(R.id.activity_main_start_check_net_status_bnt);
mStopButton = (Button) findViewById(R.id.activity_main_stop);
mRegisterReceiverBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
MainActivity.this.createServiceControllerPendIntent();
mAlarmManager.setInexactRepeating(
AlarmManager.ELAPSED_REALTIME_WAKEUP, 0, INTERVAL,
mPendingIntent);
}
});
mCheckNetStatusBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
MainActivity.this.startActivity(new Intent(MainActivity.this,
NetStatusActivity.class));
}
});
mStopButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mPendingIntent != null) {
mAlarmManager.cancel(mPendingIntent);
} else {
createServiceControllerPendIntent();
mAlarmManager.cancel(mPendingIntent);
}
// MainActivity.this.stopService(ServiceController
// .getServiceIntent());
Intent intent = new Intent();
intent.setClass(getApplicationContext(),
NetStatusMonitorService.class);
stopService(intent);
}
});
}
private void createServiceControllerPendIntent() {
Intent intent = new Intent(MainActivity.this,
ServiceController.class);
mPendingIntent = PendingIntent.getBroadcast(MainActivity.this,
0, intent, 0);
}
@Override
protected void onDestroy() {
deInitView();
super.onDestroy();
}
private void deInitView() {
}
public static class ServiceController extends BroadcastReceiver {
private static Intent sMonitorServiceIntent = null;
@Override
public void onReceive(Context context, Intent intent) {
Log.d("Service Controller", "onReceiver");
if (isServiceRunning(context, NetStatusMonitorService.class) == false) {
String info = "starting service by AlarmManager";
Log.d("Service Controller", info);
sMonitorServiceIntent = new Intent(context,
NetStatusMonitorService.class);
context.startService(sMonitorServiceIntent);
}
}
// this method is very important
private boolean isServiceRunning(Context context, Class<?> serviceClass) {
ActivityManager am = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo serviceInfo : am
.getRunningServices(Integer.MAX_VALUE)) {
String className1 = serviceInfo.service.getClassName();
String className2 = serviceClass.getName();
if (className1.equals(className2)) {
return true;
}
}
return false;
}
public static Intent getServiceIntent() {
return sMonitorServiceIntent;
}
}
}