在特定时间向用户发送通知(Android)。

11

我正在尝试使用Alarm Manager在特定时间向用户发送通知,但基本上什么也没有发生,对我来说代码看起来很好。下面是我的Alarm Manager代码:

/** 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);
}

"Notification"类中的通知代码如下:

    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也没有弹出。我不知道是否有什么非常愚蠢的地方我错过了,但任何帮助都将不胜感激!:)


在你调用服务的地方。 - Sree
哦,我应该在哪里呢? - Connor McFadden
你可以随意调用任何地方,但请记住,安卓系统会为了省电而杀死后台服务。 - Sree
你的服务在清单文件中声明了吗? - njzk2
代码看起来很好。虽然你不一定需要一个服务,但广播接收器也可以。 - njzk2
3个回答

9
这是您的接收器类:
 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));


} 

我推荐这个教程: 如何发送通知

1
请详细说明一下?通知在这个过程中的作用是什么? - Connor McFadden
@ConnorMcFadden 闹钟将会触发 BroadcastReceiver,然后触发通知。请记得在 AndroidManifest 中声明 Broadcast。 - Mariano Zorrilla
你如何在BroadcastReceiver中使用this - John Sardinha
1
需要在清单文件中添加接收器才能使其正常工作: <receiver android:name=".tools.Reciever"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"> </action> </intent-filter> </receiver> - Muneef M

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

0
在应用程序的LAUNCHER活动的onCreate方法中编写alarmmanager代码解决了我的问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接