如何在不打开应用程序的情况下每天在Android中的特定时间重复通知

时间:2018-12-06 11:37:09

标签: android

我需要基于android反复实现通知。例如每天早上6点。我已经遍历了AlarmManager和Notification类,但是我不确定如何根据需要使用/配置setRepeating()/ setInexactRepeating()方法。任何人都可以建议使用哪种最佳方法以及setXXXRepeating()方法需要发送哪些参数。

Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, 6); calendar.set(Calendar.MINUTE, 00);

Intent intent = new Intent(getApplicationContext(), TimeAlarm.class);

PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 100,intent, PendingIntent.FLAG_UPDATE_CURRENT);

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

还有Alaram.class的代码...

NotificationManager manager = (NotificationManager)  context.getSystemService(context.NOTIFICATION_SERVICE);

Intent repeating_intent = new Intent(context, MainActivity.class);

repeating_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

PendingIntent pendingIntent = PendingIntent.getActivity(context, 100, repeating_intent, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setContentIntent(pendingIntent)
            .setSmallIcon(android.R.drawable.ic_btn_speak_now)
            .setContentTitle("Title")
            .setContentText("Text")
            .setAutoCancel(true);

manager.notify(100,builder.build());
    <receiver android:name=".BootReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <actionandroid:name="android.intent.action.BOOT_COMPLETED"/>
   <category android:name="android.intent.category.HOME" />
        </intent-filter>
    </receiver>
    <receiver android:name=".AlaramNotificationReceiver"/>

    <service
        android:name=".MyService"
        android:enabled="true"
        android:exported="true"/>

这是我的清单文件。

上面的代码不起作用。请帮助

0 个答案:

没有答案