代码之家  ›  专栏  ›  技术社区  ›  TVASO

未显示提醒通知

  •  1
  • TVASO  · 技术社区  · 6 年前

    我正在制作一个应用程序来提醒用户一些事情。我想在将来某个时候显示通知。我根据一些教程编写了下面的代码,但它似乎不起作用。在我期待通知的时候,它没有出现。

    我用的是 BroadcastReceiver 以及 AlarmManager 在所需的时间发出通知。这是我的(简化)代码。

    设置时间的代码:

    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 回复  |  直到 6 年前
        1
  •  1
  •   Elletlar FireTr3e    6 年前
       <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"

        2
  •  0
  •   TVASO    6 年前

    我找到了另一种方法。而不是使用 BroadcastListener 以及 AlarmManager ,我正在使用新的 Thread 。它会等到 System.currentTimeMillis() == time 并使用在UI线程上运行runnable runOnUIThread() 。在该runnable中,将发出通知。

    我不知道这是否是一个好/有效的解决方案,但它做得很好。