intent.putExtra() in pending intent not working

后端 未结 5 715
你的背包
你的背包 2021-02-02 08:25

I am passing a pending intent through alarmreceiver, from a service class. But, after the pendingIntent fires, the intent.putExtra() information is not being received by the bro

相关标签:
5条回答
  • 2021-02-02 08:43

    Remember to put an unique id in the PendingIntent constructor, or you could get some weird thing when you try to get the putExtra values.

        PendingIntent pendingIntent = PendingIntent.getBroadcast(
               getApplicationContext(), 
               UUID.randomUUID().hashCode(), 
               aint, 
               PendingIntent.FLAG_UPDATE_CURRENT
        );
    
    0 讨论(0)
  • 2021-02-02 08:51

    Try this

    Intent aint = new Intent(getApplicationContext(), AlarmReceiver.class);
    aint.putExtra("msg", msg);
    aint.putExtra("phone", phone);
    
    
    
    PendingIntent pendingIntent = PendingIntent.getBroadcast(
        getApplicationContext(),
        id, 
        aint,
        // as stated in the comments, this flag is important!
        PendingIntent.FLAG_UPDATE_CURRENT);
    
    0 讨论(0)
  • 2021-02-02 08:52

    This is because you are first initialize Intent and add to PendingIntent. After that you are adding info in intent. You should add info to intent then add this intent to PendingIntent.

    0 讨论(0)
  • 2021-02-02 08:57

    You have to use getStringExtra() and be sure the String are not null:

         Intent intent = getIntent();    
         msg = intent.getStringExtra("msg");
         phonen = intent.getStringExtra("phone");
    
         if(msg!=null){
          Toast.makeText(context,msg,
            Toast.LENGTH_LONG).show();
         }
    

    and reverse Your putExtras before PendingIntent:

       Intent aint = new Intent(getApplicationContext(), AlarmReceiver.class);
       aint.putExtra("msg", msg);
       aint.putExtra("phone", phone);
       PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), id, aint, PendingIntent.FLAG_UPDATE_CURRENT);
    
    0 讨论(0)
  • 2021-02-02 09:01

     final Intent my_intent = new Intent(this.context , Alarm_Receiver.class);
    
        //put in extra string into my intent
        //tell the clock that the user pressed the "alarm on button"
        my_intent.putExtra("extra" , 1);
        int state = my_intent.getExtras().getInt("extra");
        Log.i("extraa" , "the state in the main activity in alarm on Button is: " + state);

    I had the same problem and I found out that you should put the putExtra before you involve the Intent

    0 讨论(0)
提交回复
热议问题