没有错误,但只是没有通知显示。我尝试了一些警报管理器的各种选项,但从未显示通知。
主要活动:
public void startTimer(){
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 13);
calendar.set(Calendar.MINUTE, 36);
Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmManager.INTERVAL_DAY, pendingIntent);
Toast.makeText(MainActivity.this, "TIMER SET!",
Toast.LENGTH_LONG).show();
}
Toast出现了,所以我认为闹钟也应该有效,但它没有。
AlarmReceiver.class
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent1 = new Intent(context,MainActivity.class);
intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//if we want ring on notifcation then uncomment below line//
// Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
PendingIntent pendingIntent = PendingIntent.getActivity(context,100,intent1, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context).
// setSmallIcon(R.drawable.applogo).
setContentIntent(pendingIntent).
setContentText("this is my notification").
setContentTitle("my notificaton").
// setSound(alarmSound).
setAutoCancel(true);
notificationManager.notify(100,builder.build());
}
}
清单
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<receiver android:name="world.cryneo.partytime.java.AlarmReceiver">
<intent-filter>
<action android:name="world.cryneo.partytime.java.alarms" />
</intent-filter>
</receiver>
我做错了什么?