![](/img/trans.png)
[英]Android: IntentService: How to: pass callback into IntentService from Activity?
[英]How to finish the activity in IntentService in android?
我正在使用GCMIntentService類擴展IntentService來獲取通知,同時單擊該通知我可以啟動我的活動。 它的工作,但我無法完成我的活動,所以我面臨着后退按鈕按下的問題。
*以下代碼*
Intent resultIntent = new Intent(this, MainActivity.class);
resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// Set pending intent
mNotifyBuilder.setContentIntent(resultPendingIntent);
在這段代碼中,啟動MainActivity並按兩次后退按鈕僅兩次存在於應用程序中,因為我無法完成通知單擊中的活動。 因此,請幫助我如何完成GCMIntentService類中的活動。
因此,請幫助我如何完成GCMIntentService類中的活動。
那不是正確的答案。 您想要的是確保任務對用戶而言是有意義的(從后向堆棧角度而言)。 根據您應用的導航情況,這可能涉及向您的resultIntent
添加以下標志resultIntent
:
Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
(如果您想將其他活動保留在后台,但是要確保只有MainActivity
一個實例)
Intent.FLAG_ACTIVITY_CLEAR_TASK
和 Intent.FLAG_ACTIVITY_NEW_TASK
(如果您要為此Notification
處理使用單獨的專用任務, Intent.FLAG_ACTIVITY_NEW_TASK
留任何現有任務;這也需要android:taskAffinity=""
中<activity>
元素中的android:taskAffinity=""
)
在活動創建或任何時候,您都將注冊自定義廣播接收器:
registerReceiver(closeMyActivity, new IntentFilter("ex.my_app_name.my_custom_broadcast"));
在這里您將收到自定義廣播,以結束您的活動。
private final BroadcastReceiver closeMyActivity = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
if (null != intent && intent.hasExtra("aCloseActivity")) {
if(intent.getBooleanExtra("aCloseActivity", false)){
// Here you can close your activity
}
}
}
};
廣播接收器銷毀后,請不要忘記在其活動中注銷它:
unregisterReceiver(closeMyActivity);
並從您的服務發送廣播:
getApplicationContext().sendBroadcast(new Intent("ex.my_app_name.my_custom_broadcast").putExtra("aCloseActivity", true));
希望對您有所幫助,但也希望看到其他答復,也許有更好的解決方案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.