我实际上试图制作一个Android应用程序,使用以下方式为用户发送通知:
通知经理==>广播接收器
然而,在系统重启后,通知不起作用。
仅供参考:我把INTENT过滤器放到广播经理那样:
<receiver
android:name=".AlarmReceiver"
android:enabled="true"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
广播接收器代码:
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, DetailsNotification.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
context).setSmallIcon(R.drawable.notification)
.setContentTitle("Alarm Fired")
.setContentText("Events a To be PErformed").setSound(alarmSound)
.setAutoCancel(true).setWhen(when)
.setContentIntent(pendingIntent)
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
notificationManager.notify((int) System.currentTimeMillis(), mNotifyBuilder.build());
}
}
主要活动代码:
Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, Hours);
calendar.set(Calendar.MINUTE, Minutes);
calendar.set(Calendar.SECOND, 0);
Intent intent1 = new Intent(MainActivity.this, AlarmReceiver.class);
intent1.putExtra("wache","test_test");
intent1.setAction(Long.toString(System.currentTimeMillis()));
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0,intent1, PendingIntent.FLAG_ONE_SHOT);
AlarmManager am = (AlarmManager) MainActivity.this.getSystemService(MainActivity.this.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
Log.d("youngSaid","notification 1 programmé a : " + String.valueOf(MainActivity.Hours) + " " + MainActivity.Minutes);
Calendar calendar2 = Calendar.getInstance();
calendar2.set(Calendar.HOUR_OF_DAY, Hours2);
calendar2.set(Calendar.MINUTE, Minutes2);
calendar2.set(Calendar.SECOND, 0);
Intent intent2 = new Intent(MainActivity.this, AlarmReceiver.class);
intent2.putExtra("wache","azerazer");
intent2.setAction(Long.toString(System.currentTimeMillis()));
PendingIntent pendingIntent2 = PendingIntent.getBroadcast(MainActivity.this, 0,intent2, PendingIntent.FLAG_ONE_SHOT);
AlarmManager am2 = (AlarmManager) MainActivity.this.getSystemService(MainActivity.this.ALARM_SERVICE);
am2.setRepeating(AlarmManager.RTC_WAKEUP, calendar2.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent2);
Toast.makeText(MainActivity.this, "Notification Lancé", Toast.LENGTH_SHORT).show();
Log.d("youngSaid","notification 2 programmé a : " + String.valueOf(MainActivity.Hours2) + " " + MainActivity.Minutes2);
}
});
请帮助:) BR
答案 0 :(得分:0)
您需要在清单中启动权限。
<receiver
android:name=".AlarmReceiver"
android:enabled="true"
android:exported="true"
/** The uses permission is defined in Manifest not in receiver*/
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>