2014-01-10 23 views
0

我是android编程的新手。我想知道可以一次显示通知吗,时间格式String(2014-1-10 1:30:33)?这是关于(2014-1-1 1:30:33),并在2014年1月10日1:30:33显示,无论何时该应用程序正在运行。 \通知android?

public void Notification(String s) 
{ 
     String ns = Context.NOTIFICATION_SERVICE; 

     NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); 

     int icon = R.drawable.notification_icon; 
     CharSequence tickerText = "Ready for Play time."; // ticker-text 
     long when = System.currentTimeMillis(); 
     Context context = getApplicationContext(); 
     CharSequence contentTitle = "Play Time"; 
     CharSequence contentText = "Your match is at "+s; 

     Intent notificationIntent = new Intent(this,ScheldueNotification .class); 
     //PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 
     PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this.getApplicationContext(),0, notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT); 
     Notification notification = new Notification(icon, tickerText, when); 
     notification.setLatestEventInfo(context, contentTitle, contentText, pendingNotificationIntent); 
     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 
     // and this 

     mNotificationManager.notify(MY_NOTIFICATION_ID, notification); 

     Intent myIntent = new Intent(MainActivity.this, myReceiver.class); 
      PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent,0); 

      AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); 
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss "); 

      /*java.util.Date date; 
       try { 
        date = formatter.parse(getCurrentTime()); 
       } catch (java.text.ParseException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       }*/ 


      try { 
       alarmManager.set(AlarmManager.RTC,(sdf.parse(getCurrentTime())).getTime(), pendingIntent); 
      } catch (java.text.ParseException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     notification.defaults |= Notification.DEFAULT_SOUND; 
     notification.vibrate = new long[] { 0, 100, 200, 300 }; 
     notification.flags |= Notification.FLAG_AUTO_CANCEL; 
     //alert(); 

} 

编辑:在我的代码中。工作不正常。

+0

请更具体一些,尝试一些代码。 – SoulRayder

+0

我编辑了我的代码。 – Halo

回答

0

您可以为此使用Alarm Manager。 请参阅Alexander Fragotsis在this上的回答。

+0

应该使用服务还是广播?只有警报管理员才足够了。 – Halo

+0

亚历山大Fragotsis已经表示在他的答案中使用服务。 –

+0

等一会儿。我会尽力。 – Halo