我必须在用户选择的时间和日期向用户显示通知,我的代码在下面。
public static void setAlarm(Context context, long id, int rYear, int rMonth, int rDay, int rHour, int rMinute) {
if (Share.wantReminder) {
Share.wantReminder = false;
Calendar calendar = new GregorianCalendar(rYear, rMonth, rDay, rHour, rMinute);
Calendar calendar1 = new GregorianCalendar();
calendar1.setTimeInMillis(System.currentTimeMillis());
calendar1.set(Calendar.YEAR, calendar.get(Calendar.YEAR));
calendar1.set(Calendar.MONTH, calendar.get(Calendar.MONTH));
calendar1.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH));
calendar1.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY));
calendar1.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE));
Intent intent = new Intent(context, ReminderService.class);
intent.setAction(String.valueOf(id));
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar1.getTimeInMillis(), pendingIntent);
Toast.makeText(context, "Alarm set", Toast.LENGTH_SHORT).show();
}
}
我的 Manifest.xml
<receiver android:name=".service.ReminderService">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
操作栏没有通知
我该怎么办?
答案 0 :(得分:0)
请分享ReminderService.class 您必须完成通知设置才能触发此地方。
<receiver android:name=".ReminderService">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
我的方法在这里触发,并且不接受服务
<receiver android:name=".AlarmReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.TIME_SET" />
<action android:name="android.intent.action.TIMEZONE_CHANGED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
答案 1 :(得分:0)
在 Manifest.xml
中对接收器进行以下更改<receiver
android:name=".service.ReminderService"
android:process=":remote"
android:excludeFromRecents="true" />
在 ReminderService 类的 onReceive()方法内创建和显示通知,如下所示。
public class ReminderService extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent)
{
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
wl.acquire();
//Put your code here to create notification
wl.release();
}
}
在您的 Manifest.xml
中添加以下权限<uses-permission android:name="android.permission.WAKE_LOCK" />