提醒通知未显示

时间:2018-05-06 10:58:05

标签: java android broadcastreceiver alarmmanager

我正在制作应用以提醒用户注意某事。我想在将来的某个时间显示通知。我已经按照一些教程编写了下面的代码,但它似乎没有用。在我预期通知时,它不会显示。

我使用BroadcastReceiverAlarmManager在所需时间发出通知。这是我的(简化)代码。

设置时间的代码:

try {
        Date date = format.parse(timeInput);//This part works
        long time = date.getTime();//Get the time in milliseconds

        Intent i = new Intent(getBaseContext(), AlarmReceiver.class);

        PendingIntent alarmSender = PendingIntent.getBroadcast(getBaseContext(), 0, i, 0);

        AlarmManager am = (AlarmManager) getBaseContext().getSystemService(Context.ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP, time, alarmSender);

        Toast.makeText(getBaseContext(), "Keep the app running to receive a reminder notification", Toast.LENGTH_LONG).show();

        super.onBackPressed();

    }catch(Exception e){
        Toast.makeText(getBaseContext(), "Parsing error. Format:\ndd/MM/yyyy and HH:mm", Toast.LENGTH_SHORT).show();
    }

AlarmReceiver.onReceive()方法:

@Override
public void onReceive(Context context, Intent intent) {

    Intent i = new Intent(context, MenuActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, i, 0);


    NotificationCompat.Builder nBulder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.notify_icon)
            .setContentTitle("title")
            .setContentText("text")
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setContentIntent(pendingIntent)
            .setAutoCancel(true);
    NotificationManagerCompat nManager = NotificationManagerCompat.from(context);
    nManager.notify(0, nBulder.build());

}

在清单文件中正确声明了所有内容。

2 个答案:

答案 0 :(得分:1)

   <receiver
        android:name=".AlarmReceiver"
        android:enabled="true"
        android:exported="true"></receiver>

    public class AlarmReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO: This method is called when the BroadcastReceiver is receiving
            // an Intent broadcast.
           throw new UnsupportedOperationException("Not yet implemented");
       }
   }

小改动:

    try {
        long time = System.currentTimeMillis();
        Intent i = new Intent(getApplicationContext(), AlarmReceiver.class);
        PendingIntent alarmSender = PendingIntent.getBroadcast(getApplicationContext(), 0, i, 0);
        AlarmManager am = (AlarmManager) getApplication().getSystemService(Context.ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP, time, alarmSender);
    } catch(Exception e){
        Toast.makeText(getBaseContext(), "Parsing error. Format:\ndd/MM/yyyy and HH:mm", Toast.LENGTH_SHORT).show();
    }

Difference between getContext() , getApplicationContext() , getBaseContext() and "this"

答案 1 :(得分:0)

我找到了另一种方法。我没有使用BroadcastListenerAlarmManager,而是使用新的Thread。它等待System.currentTimeMillis() == time并使用runOnUIThread()在UI线程上运行runnable。在该可运行的情况下,会发出通知。

我不知道这是否是一个好的/有效的解决方案,但它可以很好地完成工作。