我有一个带有 BroadcastReceiver 的服务,它通过“exampleMethod()”方法与活动通信。该服务启动并正常工作,但一段时间后它被 Android 停止,我必须重新启动它。是否可以确保服务不会长时间停止?
public class SMS_Service extends Service{
private BroadcastReceiver rec_sms = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
MainActivity.exampleMethod();
}
};
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}