安卓重启后触发广播收录机通知



我实际上试图制作一个Android应用程序,使用以下命令为用户触发通知:

通知管理器 ==> 广播接收器

但是,在系统重新启动后,通知不起作用。

仅供参考:我像这样将意图过滤器放到广播管理器中:

    <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

您需要清单中的启动权限。

<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>

最新更新