android 服务自动结束,Android服务自动停止
我正在制作一個(gè)帶有鬧鐘功能的應(yīng)用程序。我正在使用這種服務(wù),不斷檢查設(shè)備的當(dāng)前時(shí)間與我的數(shù)據(jù)庫(kù)中的時(shí)間。Android服務(wù)自動(dòng)停止
我的問(wèn)題是,如果應(yīng)用程序從后臺(tái)刪除或設(shè)備是rebooted,此service停止。我已經(jīng)使用START_STICKY來(lái)保持它在后臺(tái)運(yùn)行,并使用broadcast receiver在重新啟動(dòng)時(shí)啟動(dòng)它。
主要問(wèn)題是,無(wú)論我編碼是在MOTO G設(shè)備上工作。重新啟動(dòng),從后臺(tái)清除所有內(nèi)容,服務(wù)運(yùn)行良好。但在Xiomi電話和Huawei Honour,它從后臺(tái)清除或重新啟動(dòng)后停止。
的Service代碼:
public class RemindService extends Service {
final long delayMillis=500;
Handler h=null;
Runnable r;
SharedPreferences sp;
PendingIntent pendingIntent;
private static final int NOTIFY_ME_ID=1337;
@Override
public void onCreate() {
h=new Handler(Looper.getMainLooper());
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent, int flags,int startId) {
r = new Runnable() {
public void run() {
//SOME OF MY IF-ELSE CONDITIONS
Intent myIntent = new Intent(RemindService.this, ReminderPopUp.class);
int randomPIN = (int)(Math.random()*9000)+1000;
pendingIntent = PendingIntent.getActivity(RemindService.this, randomPIN, myIntent,PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC|AlarmManager.RTC_WAKEUP, System.currentTimeMillis() , pendingIntent);
h.postDelayed(this, delayMillis);
}
};
h.post(r);
return Service.START_STICKY;
}
@Override
public void onDestroy() {
h.removeCallbacks(r);
}
}
我Manifest聲明:
android:name="test.aguai.medieazy.RemindService"
android:enabled="true" />
有其他人遇到這個(gè)問(wèn)題呢?我認(rèn)為這是修改操作系統(tǒng)的問(wèn)題,但無(wú)論如何,我的應(yīng)用程序無(wú)法正常工作。請(qǐng)幫忙。
2015-06-29
Prakhar
+0
你試過(guò)AlarmManager了嗎? –
總結(jié)
以上是生活随笔為你收集整理的android 服务自动结束,Android服务自动停止的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 信息管理系统 github_Java+M
- 下一篇: android sina oauth2.