PendingIntent cause Error

2019-07-10 03:49发布

I have a BroadcastReceiver which gets data from Activity by Intent, with PendingIntent. The code work fine but when i'm restarting my device and onReceive calling i am getting Error... I don't know what the error because he appear after my phone restarting and the logchat cannot notice the phone and i don't see the error...

Activity:

 Intent intent = new Intent(addOne.this,AlarmReceiver.class);
 intent.putExtra("msg", title.getText().toString());
 intent.putExtra("note", note.getText().toString());

AlarmManager alarmMgr=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
PendingIntent alarmIntent = PendingIntent.getBroadcast(getApplicationContext(), id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmMgr.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);

Receiver:

String msg=intent.getStringExtra("msg");
String note=intent.getStringExtra("note");

Intent startIntent = new Intent(context, alarmDialog.class);                                
startIntent.putExtra("msg",msg);
startIntent.putExtra("note",note);
startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startIntent);

Manifest:

    <receiver android:name=".AlarmReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

I tried change the FLAG to FLAG_CANCEL_CURRENT still nothing changed. Thanks for helping.

1条回答
劫难
2楼-- · 2019-07-10 04:14

I think, may be there where error comes due to activity not starting after restart your phone and receiver still try to receive your string messages.

I don't know, How you can solve your this problem. when your phone restart your receiver gets null value. Try to use shared preference or String buffer to store your receiver String value.

remove this code

     Intent intent = new Intent(addOne.this,AlarmReceiver.class);
     intent.putExtra("msg", title.getText().toString());
     intent.putExtra("note", note.getText().toString());

try this

      Intent i = new Intent("my.action.string");               
      i.putExtra("msg",title.getText().toString());
      i.putExtra("note", note.getText().toString());
      sendBroadcast(i);

and specify this action in manifest file which is your receiver like this

<receiver android:name=".AlarmReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <action android:name="my.action.string" />
    </intent-filter>
</receiver>

Now make change your receiver code with code

public void onReceive(Context context, Intent intent)
{
       if(intent.getAction().equals("my.action.string"))
       {     

       SharedPreferences  sharedpreferences = context.getSharedPreferences(MyPREFERENCESS, context.MODE_PRIVATE);
    // get your intent which you have passed in receiver 

      String msg=intent.getStringExtra("msg");
   //  Now save your string with shared preference
      sharedpreferences.edit().putString("msg1",msg).commit();

      String note=intent.getStringExtra("note");
      sharedpreferences.edit().putString("note1",note).commit();
     }

 // now get your sharedpreference String  with boot complete

 // and start activity with passing your string data

     if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
     {
        // get the sharedpreference string and pass it with intent
        // to alarmDialog.class

        String message = sharedpreference.getString("msg1",msg1);          
        String notes = sharedpreference.getString("note1",note1);
        Intent startIntent = new Intent(context, alarmDialog.class);                                
        startIntent.putExtra("msg",message);
        startIntent.putExtra("note",notes);

        //startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        context.startActivity(startIntent);

   }`
}`

You have set action Intent.ACTION_BOOT_COMPLETED, With above code you your sharedpreference string get and pass with intent to alarmDialog.class when your device boot is complete.

you have specified this action in your manifest file so now no need to do second time. try this code. Try this .

查看更多
登录 后发表回答