所有的答案似乎都是正确的,所以我将在这里给出一个完整的答案。
首先,在Android中启动一个Broadcast是实现你尝试做的事情的最简单方法,当应用程序被手动终止时,定义一个自定义的BroadcastReceiver
来触发服务重启。
现在让我们跳入代码。
在YourService.java
中创建您的服务
请注意onCreate()
方法,在其中为版本大于Android Oreo的Build以不同的方式启动前台服务。这是因为最近引入了严格的通知政策,我们必须定义自己的通知渠道才能正确显示它们。
onDestroy()
方法中的this.sendBroadcast(broadcastIntent);
语句是异步发送具有操作名称"restartservice"
的广播的语句。稍后我们将使用它作为触发器来重启我们的服务。
在这里,我们定义了一个简单的计时器任务,每1秒钟在Log
中打印一次计数器值,并在每次打印时将其自身递增。
public class YourService extends Service {
public int counter=0;
@Override
public void onCreate() {
super.onCreate();
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O)
startMyOwnForeground();
else
startForeground(1, new Notification());
}
@RequiresApi(Build.VERSION_CODES.O)
private void startMyOwnForeground()
{
String NOTIFICATION_CHANNEL_ID = "example.permanence";
String channelName = "Background Service";
NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
chan.setLightColor(Color.BLUE);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
assert manager != null;
manager.createNotificationChannel(chan);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
Notification notification = notificationBuilder.setOngoing(true)
.setContentTitle("App is running in background")
.setPriority(NotificationManager.IMPORTANCE_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.build();
startForeground(2, notification);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
startTimer();
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
stoptimertask();
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("restartservice");
broadcastIntent.setClass(this, Restarter.class);
this.sendBroadcast(broadcastIntent);
}
private Timer timer;
private TimerTask timerTask;
public void startTimer() {
timer = new Timer();
timerTask = new TimerTask() {
public void run() {
Log.i("Count", "========= "+ (counter++));
}
};
timer.schedule(timerTask, 1000, 1000);
}
public void stoptimertask() {
if (timer != null) {
timer.cancel();
timer = null;
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
创建一个广播接收器,以响应在 Restarter.java
中自定义的广播
你刚才在 YourService.java
中自定义了一个 action 名为"restartservice"
的广播,现在这个广播将会触发一个方法来重启你的服务。在 Android 中,我们使用BroadcastReceiver
来实现。
我们覆盖内置的 BroadcastReceiver
中的 onReceive()
方法,并添加语句以重新启动服务。由于在 Android Oreo 8.1 及以上版本中存在严格的后台策略,因此使用 startService()
将无法正常工作,一旦应用程序被杀死,服务将很快终止重启。因此,我们在高版本中使用 startForegroundService()
并显示持续通知以保持服务运行。
public class Restarter extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("Broadcast Listened", "Service tried to stop");
Toast.makeText(context, "Service restarted", Toast.LENGTH_SHORT).show();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(new Intent(context, YourService.class));
} else {
context.startService(new Intent(context, YourService.class));
}
}
}
在应用启动时定义MainActivity.java
调用服务。
这里我们定义了一个单独的isMyServiceRunning()
方法来检查后台服务的当前状态。如果服务没有在运行,则使用startService()
启动它。
由于应用已经在前台运行,因此我们不需要将服务作为前台服务启动,以防止它被终止。
请注意,在onDestroy()
中,我们专门调用stopService()
,以便调用我们修改后的覆盖方法。如果不这样做,当应用被杀死时,服务将会自动结束,而不会调用我们修改过的YourService.java
中的onDestroy()
方法。
public class MainActivity extends AppCompatActivity {
Intent mServiceIntent;
private YourService mYourService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mYourService = new YourService();
mServiceIntent = new Intent(this, mYourService.getClass());
if (!isMyServiceRunning(mYourService.getClass())) {
startService(mServiceIntent);
}
}
private boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
Log.i ("Service status", "Running");
return true;
}
}
Log.i ("Service status", "Not running");
return false;
}
@Override
protected void onDestroy() {
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("restartservice");
broadcastIntent.setClass(this, Restarter.class);
this.sendBroadcast(broadcastIntent);
super.onDestroy();
}
}
最后,在AndroidManifest.xml
中注册它们
上面的三个类都需要分别在AndroidManifest.xml
中进行注册。
请注意,我们使用操作名称为"restartservice"
定义了一个带有intent-filter
的标签,其中将Restarter.java
注册为一个receiver
。
这确保了每当系统遇到具有给定操作名称的广播时,都会调用我们的自定义BroadcastReciever
。
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<receiver
android:name="Restarter"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="restartservice" />
</intent-filter>
</receiver>
<activity android:name="MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name="YourService"
android:enabled="true" >
</service>
</application>
如果应用程序被从任务管理器中终止,那么这将重新启动您的服务。 只要用户没有从应用程序设置中的强制停止
应用程序,此服务将在后台继续运行。
更新:感谢Dr.jacky指出这一点。上述方法仅在服务的onDestroy()
被调用时才会起作用,而这有时可能不是情况,我之前不知道。谢谢。