在BroadcastReceiver中,Android Studio intent.getParcelableExtra为null

时间:2017-09-20 06:19:12

标签: notifications alarmmanager restart

我想在重启后发送通知。 AlarmInitReceiver正常接收。 但是intent.getParcelableExtra无法在AlarmInitReceiver类中获取通知对象。 我使用try / catch和Toast对象来描述异常消息。 谢谢大家。

enter image description here

的AndroidManifest.xml



    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hp.test">
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


        <receiver android:name="Main2Activity"></receiver>

        <receiver android:name="AlarmInitReceiver"
            android:enabled="true"
            android:exported="true"
            android:label="StartMyServiceAtBootReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
        </receiver>

        <service android:name="NofyService"/>
    </application>

</manifest>
&#13;
&#13;
&#13;

MainActivity.java

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    scheduleNotification(getNotification("123"));
}
private void scheduleNotification(Notification notification) {

    final Intent notificationIntent = new Intent(this, AlarmInitReceiver.class);
    notificationIntent.putExtra(AlarmInitReceiver.NOTIFICATION_ID, 1);
    notificationIntent.putExtra(AlarmInitReceiver.NOTIFICATION, notification);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    //set time
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, 12);
    calendar.set(Calendar.MINUTE, 03);

    alarmManager.set(AlarmManager.RTC_WAKEUP,  calendar.getTimeInMillis(), pendingIntent);
}

private Notification getNotification(String content) {
    NotificationChannel channelMsg = new NotificationChannel(
            "msg",
            "Channel msg",
            NotificationManager.IMPORTANCE_HIGH);
    channelMsg.setDescription("socketMsg");
    channelMsg.enableLights(true);
    channelMsg.enableVibration(true);
    NotificationManager notificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    notificationManager.createNotificationChannel(channelMsg);

    Notification.Builder builder =
            new Notification.Builder(this)
                    .setSmallIcon(android.R.drawable.ic_notification_clear_all)
                    .setContentTitle("TeamGoGoal")
                    .setContentText(content)
                    .setChannelId("msg");

    return builder.build();
}}

AlarmInitReceiver.java

public class AlarmInitReceiver extends BroadcastReceiver {
public static String NOTIFICATION_ID = "notification-id";
public static String NOTIFICATION = "notification";
@Override
public void onReceive(Context context, Intent intent) {
    try{
        if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
            NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
            Notification notification = intent.getParcelableExtra(NOTIFICATION);
            int id = intent.getIntExtra(NOTIFICATION_ID, 0);
            notificationManager.notify(id, notification);
        }
    }catch(Exception e){
        Toast.makeText(context,e.toString(),Toast.LENGTH_LONG).show();
    }
}}

0 个答案:

没有答案