2013-03-14 52 views
0

我想每12小时向我的用户发送一条通知消息,但无法确定从哪里开始。有人可以提供一步一步的指导,为我的用户添加一个简单的通知?在此先感谢无法编码Android的重复通知?

+0

@KenWhite将链接移至评论。点击[Here](http://mobile.tutsplus.com/tutorials/android/android-fundamentals-scheduling-recurring-tasks/)获取教程。 – melvynkim 2013-03-14 22:28:57

+0

您的问题是关于发送通知或关于计划重复性事件?该主题似乎表明它与周期性事件有关,但文本似乎表明它与发送通知有关。你能否[编辑]澄清你在问什么?谢谢。 – 2013-03-14 22:29:30

回答

0

您可以使用此代码:

private static final int TIME = 1000*60*60*12; 

     new Timer().scheduleAtFixedRate(new TimerTask() { 
      public void run() { 
       showNotification(); 
      } 
     }, 0, TIME);//start immediatly, run every 12hours 


    public void showNotification() { 
     final NotificationManager mNotification = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

     final Intent launchNotifiactionIntent = new Intent(this, ActivityLauchedOnClickNotif.class); 
     final PendingIntent pendingIntent = PendingIntent.getActivity(this, 
        REQUEST_CODE, launchNotifiactionIntent, 
        PendingIntent.FLAG_ONE_SHOT); 

     Notification.Builder builder = new Notification.Builder(this) 
      .setWhen(System.currentTimeMillis()) 
      .setContentTitle(titleString) 
      .setContentText(messageString) 
      .setContentIntent(pendingIntent); 

     mNotification.notify(NOTIFICATION_ID, builder.build()); 
    } 

此代码的工作,因为API 11! (notification.builder)