5

在Android 4.4.2中,單擊我的前臺服務通知正在終止我的進程。前臺服務正在通知中點擊

在較舊的設備上(Samsuing Tab 2運行4.2.2),我可以從最近的任務中刪除活動,並且仍然可以在後臺運行我的Service。然後當我點擊Notification我的應用程序Activity再次開始相當愉快。

但是,一旦我點擊運行4.4.2的Nexus 7上的通知,我的進程就會被終止(直到點擊在後臺運行愉快)。該PendingIntent似乎並沒有在各消防,或至少,它沒有擊中BroadcastReceiver的第一行:

05-21 16:17:38.939: I/ActivityManager(522): Killing 2268:com.test.student/u0a242 (adj 0): remove task 

我已經通過this答案運行,並且使用命令dumpsys activity proccesses我已經確認我的服務正確運行在前臺。

那麼,點擊這個通知會導致我的進程被終止是什麼?參與移動服務的前景

代碼如下:

服務:

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    Log.i("NativeWrappingService", "Starting Service..."); 
    startForeground(NotificationIcon.NOTIFICATION_STUDENT, NotificationManager.getStudentIcon(this).getNotification());  
    return super.onStartCommand(intent, flags, startId); 
} 

NotificationIcon:(getStudentIcon(this).getNotification()

public Notification getNotification() { 
    Builder mBuilder = new Builder(mContext); 

    if(mSmallIcon != -1)  mBuilder.setSmallIcon(mSmallIcon); 
    if(mLargeIcon != null) mBuilder.setLargeIcon(mLargeIcon); 
    if(mTitle  != null) mBuilder.setContentTitle(mTitle); 
    if(mSubTitle != null) mBuilder.setContentText(mSubTitle); 
    if(mSubTitleExtra != null) mBuilder.setContentInfo(mSubTitleExtra); 

    mBuilder.setOngoing(mOngoing); 
    mBuilder.setAutoCancel(mAutoCancel); 
    mBuilder.setContentIntent(getPendingIntent(mContext, mAction, mBundle, mActivity)); 

    return mBuilder.build(); 
} 

private PendingIntent getPendingIntent(Context context, String action, Bundle extras, String activity) { 
    Intent newIntent = new Intent(context, BroadcastToOrderedBroadcast.class); 
    Bundle bundle; 
    if(extras != null) bundle = extras; 
    else    bundle = new Bundle(); 

    if(activity != null && !activity.equalsIgnoreCase("")) { 
     BundleUtils.addActivityToBundle(bundle, activity); 
    } 

    BundleUtils.addActionToBundle(bundle, action); 

    newIntent.putExtras(bundle); 

    return PendingIntent.getBroadcast(NativeService.getInstance(), mNotificationID, newIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
} 
+0

這是一個已知的錯誤,正在跟蹤[這裏](https://code.google.com/p/android/issues/detail?id=53313)。您可以嘗試[comment#7](https://code.google.com/p/android/issues/detail?id=53313#c7)中列出的解決方案。 –

回答

4

添加FLAG_RECEIVER_FOREGROUND標誌,以避免您的通知使用的PendingIntent以便允許服務以前臺優先級運行。

Intent newIntent = new Intent(context, BroadcastToOrderedBroadcast.class); 
newIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); 
return PendingIntent.getBroadcast(NativeService.getInstance(), mNotificationID, newIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

下面是這種信息的源:Android Issue Tracker tool

+0

是不是等同於FLAG_ACTIVITY_NEW_TASK?他們都是0x10000000,你能支持我可以設置FLAG_RECEIVER_FOREGROUND嗎? –

+0

@Manish Mulimani我經歷了你的建議。 當我的應用程序關閉,如果我點擊通知我的服務正在重新啓動。 BroadCast接收器也不起作用? –

相關問題