1

インテントを使用してデータを保存し、アプリケーションの他の場所に復元しています。私は他の場所でそれらを使用しましたが、今は期待どおりに機能していません.

public class GCMIntentService extends GCMBaseIntentService {    
    public GCMIntentService() {
        super(ConstantsGCM.GCM_SENDER_ID);
    }



    @Override
    protected void onMessage(Context context, Intent intent) {
        ...
        String ns = Context.NOTIFICATION_SERVICE;   
        NotificationManager notManager = (NotificationManager) context.getSystemService(ns);
        String room = intent.getExtras().getString(ConstantsGCM.GCM_ROOM);      
        Intent notIntent;       
        PendingIntent contIntent;
        Notification notif; 


        notif = new Notification(icon, textStatus, time);                       
        notIntent = new Intent(contexto,RoomsActivity2.class);

        Bundle b2 = new Bundle();                                           
        b2.putString(ConstantsRooms.ROOM, room);
        notIntent.putExtras(b2);

        contIntent = PendingIntent.getActivity(contexto, 0, notIntent, 0);               
        notif.setLatestEventInfo(contexto, tittle, description, contIntent);    
        notif.flags |= Notification.FLAG_AUTO_CANCEL;                   
        notManager.notify((int)(Math.random()*1000), notif);

このコードは、通知が来るときに実行されます。この通知をクリックすると、Activity RoomsActivities2.class が実行されます。ここでは、このコードを呼び出します。

public String getMessageString(String cod){
    String result = "";
    bundle  = getIntent().getExtras();

    if (bundle != null){
        result = bundle.getString(cod);
    }
    return result;
}

しかし、インテントに保存された最後のデータを取得できませんでした。何が悪いの?私はそれを正しく使用していないと思います。アクティビティからデータを取得できないのはなぜですか?

それが起こっていると私が思うことは次のとおりです。アプリケーションは多くの通知を受け取り、最初の通知は正しく機能します。しかし、さらに多くの通知を受け取り続けると、データはオーバーライドされず、コードをデバッグするときに別のデータを設定していますが、常に最初の通知を取得します。

4

1 に答える 1

2

OK、保留中の意図を扱ってからしばらく経ちましたが、次の 2 つのことを覚えています。

交換:

contIntent = PendingIntent.getActivity(contexto, 0, notIntent, 0);

と:

contIntent = PendingIntent.getActivity(contexto, 0, notIntent, PendingIntent.FLAG_UPDATE_CURRENT);

これにより、バンドルが維持されます。

しかし、そのフラグは既存の保留中のインテントを最新のもので上書きしますが、それは望ましくないかもしれません。

同じインテントを持つ同じコンテキストから複数の保留中のインテントがある場合 (ただし、異なるバンドル!)、2 番目のパラメーターを使用できます。

contIntent = PendingIntent.getActivity(contexto, requestCode, notIntent, PendingIntent.FLAG_UPDATE_CURRENT);

すべての保留中のインテントに一意requestCodeの .

于 2013-06-21T03:46:40.607 に答える