3

ユーザーがアプリを一時停止したときに通知を送信しようとしています。簡単にするために、ユーザーは通知アクションを使用してアプリケーションにすばやく移動できます。これは私が使用しているコードです。Android 4より前のすべてのバージョンで機能しますが、どれが問題なのかわかりません

 NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher)    
                .setContentTitle("Titulo")
                .setContentText("Titulo");

        mBuilder.setOngoing(true);

        // Creates an explicit intent for an Activity this 
        Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class);
        // put the flags
        resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        // Adds the back stack for the Intent (but not the Intent itself)
        stackBuilder.addParentStack(MainActivity.class);
        // Adds the Intent that starts the Activity to the top of the stack
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(
                    0,
                    PendingIntent.FLAG_UPDATE_CURRENT
                );
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        // mId allows you to update the notification later on.
        mNotificationManager.notify(mId, mBuilder.build());

そのため、Android 4.0以降で通知を押すと、再開する代わりにアクティビティが再度作成されます。助けてください、私はそれを機能させることができません。

編集 (マニフェスト singletop を忘れる)

android:launchMode="singleTop"同じ結果、動作しません...

アクティビティに地図が含まれています。私は新しいバージョンの Google マップを使用しています。v2.

4

4 に答える 4

2

多くの検索を行った後に実際に機能した唯一の解決策は、次のことです。

NotificationCompat.Builder builder = new NotificationCompat.Builder(this).set...(...).set...(..);

Intent resultIntent = new Intent(this, MainClass.class);
resultIntent.setAction("android.intent.action.MAIN");
resultIntent.addCategory("android.intent.category.LAUNCHER");

PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

builder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, builder.build());

これにより、別のアクティビティを作成せずに現在のアクティビティが開きます!

于 2014-10-30T15:21:10.290 に答える
0

@Patrick が提供する解決策は、質問から取得され、回答として追加されました。

NotificationCompat.Builder mBuilder = 
                new NotificationCompat.Builder(this) 
                .setSmallIcon(R.drawable.logo)    
                .setContentTitle(getString(R.string.txt)) 
                .setContentText(getString(R.string.txt)); 

        mBuilder.setOngoing(true); 

        // Creates an explicit intent for an Activity in your app 
        Intent resultIntent = new Intent(this, Activity.class); 

        resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

        // The stack builder object will contain an artificial back stack for the 
        // started Activity. 
        // This ensures that navigating backward from the Activity leads out of 
        // your application to the Home screen. 
        TaskStackBuilder stackBuilder = TaskStackBuilder.from(this); 
        // Adds the back stack for the Intent (but not the Intent itself) 
        stackBuilder.addParentStack(Activity.class); 
        // Adds the Intent that starts the Activity to the top of the stack 
        stackBuilder.addNextIntent(resultIntent); 
        PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, 0); 
        mBuilder.setContentIntent(resultPendingIntent); 
        NotificationManager mNotificationManager = 
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
        // mId allows you to update the notification later on. 
        mNotificationManager.notify(mId, mBuilder.getNotification());
于 2013-12-20T18:59:36.990 に答える