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

この回答を実行し、コマンドを使用して、サービスフォアグラウンドで正しく実行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);
}

通知アイコン: ( 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);
}
4

1 に答える 1

4

サービスをフォアグラウンド優先で実行できるようにするために、通知から使用される PendingIntentにFLAG_RECEIVER_FOREGROUNDフラグを追加します。

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 ツール

于 2014-06-02T16:55:54.527 に答える