如何在android中重启时不会解除警报通知

时间:2015-09-04 04:40:28

标签: android alarmmanager

  • 我想在android开始3天的通知提醒 在设定的日期和时间之后
  • 我知道怎么做,但手机重启后通知不起作用
  • 我希望闹钟管理员能够记住手机重启前的设定日期和时间
  • 我该怎么办?我搜索了很多教程,但找不到一个,大多数人都说将boot_completed放在广播接收器中,这不是解决方案。
  • 任何明确的教程链接,清楚地解释了源代码?

我当前的应用代码低于

“明显”

    <?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());
}
}

1 个答案:

答案 0 :(得分:1)

您应该注册具有启动意图的接收器,并在其中启动/重启警报

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

您还需要添加此permisison

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

编辑:这是官方参考

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

  

默认情况下,设备关闭时会取消所有警报。至   防止这种情况发生,你可以设计你的应用程序   如果用户重新启动,则自动重新启动重复警报   设备。这可确保AlarmManager继续执行此操作   任务,无需用户手动重启警报。