2017-09-13 49 views
0

我使用的是onDestroy方法,我學到的方法在應用程序關閉時並不總是被調用。這給我造成了一個問題,我希望每次關閉應用程序時都會彈出通知。有一個更好的方法嗎?順便說一句,onStop和不適合我,因爲我的應用程序作爲後臺服務運行。有趣的是,我的onDestroy方法每次運行我的後臺服務時都會起作用,但每當它關閉時,只要應用程序界面打開,我的onDestroy就不會被調用。這似乎很奇怪,因爲我認爲onDestroy沒有被調用,如果設備缺乏資源,運行我的服務將佔用更多的資源。 IDK。任何幫助,將不勝感激!當應用程序關閉時創建通知

`@Override 
public void onDestroy(){ 
    super.onDestroy(); 

    Log.d("asdasd","asdasdasdasdasd"); 
    if(notif.isChecked()) { 
     Intent intent1 = new Intent(this, MainActivity.class); 
     PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent1, 0); 
     Notification noti = new Notification.Builder(this) 
       .setContentTitle("S-Dimmer") 
       .setContentText(getResources().getString(R.string.sDimmerStopped)) 
       .setSmallIcon(R.drawable.ic_action_name1) 
       .setContentIntent(pIntent).getNotification(); 
     noti.flags = Notification.FLAG_NO_CLEAR; 
     NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
     nm.notify(0, noti); 
     doas.isRunning = false; 
    } 
}` 

回答

0

這種方法並不總是被調用,所以你應該嘗試從後臺服務,因爲還有一些銷燬過程警報。 所以請儘量做你想要的服務。

Please check out this ..

的onDestroy()可以或可以不被任何給定的活動或服務調用。一般的規則是調用onDestroy(),或者進程終止,或者代碼崩潰。

你沒有。無論何時,用戶或操作系統可能會因任何原因終止進程。當發生這種情況時,您可能會或可能不會使用onDestroy()調用。雖然您可以通過服務上的onTaskRemoved()稍微提高成功率,但這本身已被證明不可靠,並且不會覆蓋所有方案。

0

你正在做錯誤的方式。在方法的onDestroy所有其他東西之前應該super.onDestory()來實現

試試這個

@Override 
public void onDestroy(){ 


    Log.d("asdasd","asdasdasdasdasd"); 
    if(notif.isChecked()) { 
     Intent intent1 = new Intent(this, MainActivity.class); 
     PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent1, 0); 
     Notification noti = new Notification.Builder(this) 
       .setContentTitle("S-Dimmer") 
       .setContentText(getResources().getString(R.string.sDimmerStopped)) 
       .setSmallIcon(R.drawable.ic_action_name1) 
       .setContentIntent(pIntent).getNotification(); 
     noti.flags = Notification.FLAG_NO_CLEAR; 
     NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
     nm.notify(0, noti); 
     doas.isRunning = false; 
    } 
    super.onDestroy(); 
}` 
+0

Upvoted,謝謝。 –

+0

不,你其實是錯的......把super.onDestroy放在哪裏並不重要,因爲有時整個方法不會被觸發。 – mhdjazmati

0
onTaskRemoved() 
{ 
if(notif.isChecked()) { 
     Intent intent1 = new Intent(this, MainActivity.class); 
     PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent1, 0); 
     Notification noti = new Notification.Builder(this) 
       .setContentTitle("S-Dimmer") 
       .setContentText(getResources().getString(R.string.sDimmerStopped)) 
       .setSmallIcon(R.drawable.ic_action_name1) 
       .setContentIntent(pIntent).getNotification(); 
     noti.flags = Notification.FLAG_NO_CLEAR; 
     NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
     nm.notify(0, noti); 
     doas.isRunning = false; 
    } 
} 
+0

看着這個,發現[這個](https://stackoverflow.com/questions/41744933/android-ondestroy-isnt-called-when-i-close-the-app-from-the-recent-apps-button? rq = 1)感謝您的幫助! Upvoted –

相關問題