如何在 android 中制作重启时不关闭的警报通知

how to make alarm notification that doesn't dismiss upon restart in android

我当前的应用代码如下

"manifest"

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ynwa.finalalert"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="22" />

    <!-- Permission to use AlarmManager -->
    <uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
    <!-- Permission to Send SMS -->
    <uses-permission android:name="android.permission.SEND_SMS" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.ynwa.finalalert.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <receiver
            android:name="com.ynwa.finalalert.AlertReceiver"
            android:enabled="true" />
    </application>

</manifest>

"MainActivity"

public class MainActivity extends AppCompatActivity {
Calendar calendar = Calendar.getInstance();
TextView display;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    display = (TextView) findViewById(R.id.textView);

    Button dateButton = (Button) findViewById(R.id.button);
    dateButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new DatePickerDialog(MainActivity.this, listener, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();

        }
    });
}
DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener(){

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

        display.setText("Selected date is: " + dayOfMonth + "/" + (monthOfYear + 1) + "/" + year);

        calendar.set(Calendar.MONTH, monthOfYear);
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);

        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.AM_PM,Calendar.AM);

        Long alertTime = calendar.getTimeInMillis()+259200*1000;
        // Define a time value of 3 days after set date

        Intent alertIntent = new Intent(MainActivity.this, AlertReceiver.class);

        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

        alarmManager.set(AlarmManager.RTC_WAKEUP, alertTime,
                PendingIntent.getBroadcast(MainActivity.this, 1, alertIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT));

    }
};}

"AlertReceiver"

public class AlertReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {

    createNotification(context, "Donation App", "You can donate now!", "Alert");

}

public void createNotification(Context context, String msg, String msgText, String msgAlert){

    PendingIntent notificIntent = PendingIntent.getActivity(context, 0,
            new Intent(context, MainActivity.class), 0);

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(context)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle(msg)
                    .setTicker(msgAlert)
                    .setContentText(msgText);

    mBuilder.setContentIntent(notificIntent);

    mBuilder.setDefaults(Notification.DEFAULT_SOUND);

    mBuilder.setAutoCancel(true);

    NotificationManager mNotificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    mNotificationManager.notify(1, mBuilder.build());
}
}

您应该注册一个具有启动意图的接收器,并且start/restart您的闹钟在其中

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

你也需要添加这个权限

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

编辑:这里是官方参考

https://developer.android.com/training/scheduling/alarms.html#boot

By default, all alarms are canceled when a device shuts down. To prevent this from happening, you can design your application to automatically restart a repeating alarm if the user reboots the device. This ensures that the AlarmManager will continue doing its task without the user needing to manually restart the alarm.