每天12小时重复通知

时间:2017-01-13 21:30:32

标签: android alarmmanager android-notifications

我希望每天12小时重复我的通知,但我的代码无效... 我在OnCreate的MainActivity中启动我的Alarm Manager,如下所示:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ma);

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 12);
    calendar.set(Calendar.MINUTE, 00);
    calendar.set(Calendar.SECOND, 0);
    Intent intent1 = new Intent(MainActivity.this, AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0,intent1, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager am = (AlarmManager) MainActivity.this.getSystemService(MainActivity.this.ALARM_SERVICE);
    am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

和我的AlarmReceiver类:

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, MainActivity.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.photo)
            .setContentTitle("ça marche fdp")
            .setContentText("Et ouai t'as bien réussi à afficher une notification bordel de cul").setSound(alarmSound)
            .setAutoCancel(true).setWhen(when)
            .setContentIntent(pendingIntent)
            .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
    notificationManager.notify(0, mNotifyBuilder.build());



}

}

你知道问题出在哪里吗?请不要不喜欢我只想学习代码:(

1 个答案:

答案 0 :(得分:1)

更新您的清单文件,以便为您的接收器提供以下参数:

<receiver
        android:name="com.example.alarmmanagernotifcation.AlarmRecei‌​ver"
        android:enabled="true"
        android:process=":remote" />

然后我相信问题可能出在您选择的图标上:

.setSmallIcon(R.drawable.photo)

如果图像不兼容,那么你不会在应用程序外看到崩溃,如果手机已插入,它只会在Android监视器内部显示致命异常,因此它可以被忽视并且通知不会被解雇。

使用的drawable需要针对不同的手机密度具有不同的尺寸。要制作正确的drawable,请右键单击drawable包,然后执行New - &gt;图像资产。从下拉菜单中选择通知图标,然后使用它生成图标的所有不同大小。