【问题标题】:Notification sent to user at a specific time Android在特定时间向用户发送通知 Android
【发布时间】:2016-06-21 04:57:00
【问题描述】:

我正在尝试使用警报管理器在特定时间向用户发送通知。基本上,什么都没有发生,对我来说代码看起来很好。我的警报管理器代码如下:

/** Notify the user when they have a task */
public void notifyAtTime() {
    Intent myIntent = new Intent(PlanActivity.this , Notification.class);     
    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
    PendingIntent pendingIntent = PendingIntent.getService(PlanActivity.this, 0, myIntent, 0);

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 12);
    calendar.set(Calendar.MINUTE, 17);
    calendar.set(Calendar.SECOND, 00);

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000 , pendingIntent);
}

“通知”类中的通知代码如下:

    public class Notification extends Service {


    @Override
    public void onCreate() {   

        Toast.makeText(this, "Notification", Toast.LENGTH_LONG).show();

        Intent intent = new Intent(this,PlanActivity.class);
        PendingIntent pending = PendingIntent.getActivity(this, 0, intent, 0);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                this).setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("Football")
                .setContentText("Don't forget that you have Football planned!")
                .setContentIntent(pending);

        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.notify(0, mBuilder.build());
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
}

在 Notification 类中设置的 Toast 也没有出现。我不知道我是否真的很愚蠢,但任何帮助将不胜感激! :)

【问题讨论】:

  • 你在哪里调用你的服务/
  • 嗯.. 我应该在哪里?
  • 你可以随心所欲地调用任何地方,但请记住,android会为了省电而杀死后台服务
  • 您的服务是否在清单中声明?
  • 代码看起来不错。不过,您实际上并不需要服务,广播接收器就可以了。

标签: android notifications alarmmanager


【解决方案1】:

这是你的接收器类:

 public class OnetimeAlarmReceiver extends BroadcastReceiver {

  public void onReceive(Context context, Intent intent) {

  //Toast.makeText(context, "Repeating Alarm worked.", Toast.LENGTH_LONG).show();


  // try here

 // prepare intent which is triggered if the
 // notification is selected

  Intent intent = new Intent(this, NotificationReceiver.class);
  PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

 // build notification
 // the addAction re-use the same intent to keep the example short
 Notification n  = new Notification.Builder(this)
    .setContentTitle("New mail from " + "test@gmail.com")
    .setContentText("Subject")
    .setSmallIcon(R.drawable.icon)
    .setContentIntent(pIntent)
    .setAutoCancel(true)
    .addAction(R.drawable.icon, "Call", pIntent)
    .addAction(R.drawable.icon, "More", pIntent)
    .addAction(R.drawable.icon, "And more", pIntent).build();


    NotificationManager notificationManager = 
     (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    notificationManager.notify(0, n); 


   }
 }

在第二个活动中设置警报:

  private void setAlarm(){

    Intent intent = new Intent(this, OnetimeAlarmReceiver.class);   

    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, REQUEST_CODE, intent, 0);

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + alarm_time , pendingIntent);
    System.out.println("Time Total ----- "+(System.currentTimeMillis()+total_mili));


} 

我想推荐这个教程How to send notification

【讨论】:

  • 请详细说明一下?通知是从哪里来的?
  • @ConnorMcFadden 警报将触发 BroadcastReceiver,然后触发 Notification。记得在你的 AndroidManifest 上声明广播。
  • 你如何在广播接收器中使用this?
  • 需要在清单中添加接收器才能使其正常工作
【解决方案2】:

尝试将通知代码放在 onStartCommand 函数中,而不是 onCreate。

【讨论】:

    【解决方案3】:

    在应用程序的 LAUNCHER 活动的 onCreate 方法中编写警报管理器代码解决了我的问题

    【讨论】:

      猜你喜欢
      • 2019-08-23
      • 2020-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-19
      相关资源
      最近更新 更多