我有一個BroadcastReceiver服務,它通過「exampleMethod()」方法與活動進行通信。此服務啓動並正常工作,但過了一段時間它被Android停止,我必須重新啓動它。可以確保服務很長一段時間不會停止?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;
}