[問題] 請教避免Process被kill(force stop)方法
目前已經寫了一個小app
將Service設定為Foreground以監控手機狀態
上述功能目前可正常運作
但是希望做到如果Process被Kill可以自動重啟的功能
用一般Task Killer刪除Process是會自動重啟
但是如果用手機內建「設定/應用程式」功能去停用Process
程式就真的不會醒來了(淚)
上網爬文有提到可利用uncaughtException來執行重啟程式
程式大概如下列所示
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout2);
intent = getIntent();
intent.setClassName("myPackageName", "className");
m_restartIntent = PendingIntent.getActivity
(getApplicationContext(), 0,
intent, Intent.FLAG_ACTIVITY_NEW_TASK);
}
public void uncaughtException(Thread thread, Throwable ex)
{
Log.e("unKonwException", "uncaught exception is catched!");
AlarmManager amg = (AlarmManager)
getSystemService(Context.ALARM_SERVICE);
amg.set(AlarmManager.RTC, System.currentTimeMillis() + 3000,
m_restartIntent);
System.exit(0);
}
疑問一、
當故意測試幾個會有Exception的情形如java.lang.nullpointe exception
uncaughtException()會偵測的到
但是它會直接重啟myPackageName.className 這個Activity
有辦法只重啟Service嗎
疑問二、
當故意以手機內建「設定/應用程式」功能去停用Process
uncaughtException()卻無法catch exception
當然也辦法重啟程式
想請教在這種情境下
uncaughtException()究竟能不能catch exception
疑問三、
除了uncaughtException()外
且暫時不考慮用另一個app來監聽
還有其他方式可以做到被手機內建「設定/應用程式」功能去停用Process後
還能自動重啟service的作法嗎?
請板上高人能指點小弟迷津<(_ _)>
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 210.69.35.10
※ 編輯: SF2SG 來自: 210.69.35.10 (11/05 18:38)
推
11/05 19:05, , 1F
11/05 19:05, 1F
→
11/05 19:06, , 2F
11/05 19:06, 2F
→
11/05 19:06, , 3F
11/05 19:06, 3F
→
11/05 19:07, , 4F
11/05 19:07, 4F
→
11/05 19:08, , 5F
11/05 19:08, 5F
→
11/05 19:09, , 6F
11/05 19:09, 6F
推
11/05 19:21, , 7F
11/05 19:21, 7F
→
11/05 19:22, , 8F
11/05 19:22, 8F
→
11/05 19:23, , 9F
11/05 19:23, 9F
→
11/05 19:24, , 10F
11/05 19:24, 10F
推
11/05 19:52, , 11F
11/05 19:52, 11F
→
11/06 00:48, , 12F
11/06 00:48, 12F
→
11/06 00:49, , 13F
11/06 00:49, 13F
推
11/06 00:52, , 14F
11/06 00:52, 14F
推
11/06 09:05, , 15F
11/06 09:05, 15F
→
11/06 21:12, , 16F
11/06 21:12, 16F
→
11/06 21:12, , 17F
11/06 21:12, 17F
→
11/06 21:13, , 18F
11/06 21:13, 18F
→
11/06 21:13, , 19F
11/06 21:13, 19F
→
11/06 21:14, , 20F
11/06 21:14, 20F
→
11/06 21:14, , 21F
11/06 21:14, 21F