2013-12-12 17 views
0
public String getCurrentTime(){ 
    Calendar c= Calendar.getInstance(); 
    Log.i("Calander getinstance",Calendar.getInstance()+""); 
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss "); 
    String strDate = sdf.format(c.getTime()); 
    return strDate; 
} 

public void alert() 
{ 
     Calendar cal = Calendar.getInstance(); 
     Intent myIntent = new Intent(MainActivity.this, myReceiver.class); 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent,0); 
     AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); 
     alarmManager.set(AlarmManager.RTC,Long.parseLong(getCurrentTime()), pendingIntent); 
} 

在getCurrentTime,它必須返回的字符串,但可以alarmManager.set得到字符串

alarmManager.set(AlarmManager.RTC,Long.parseLong(getCurrentTime()), pendingIntent); is only get for Long. Is that Another way. Help Plz. 
+0

爲什麼ü想串花花公子 –

+0

PHP返回 「YYYY-MM-DD HH:MM:SS」 格式。字符串返回。所以,我把當前時間改成這種格式。如果鬧鐘管理器只有很長時間,我會遇到問題。 – vcav

+0

對不起。我看不到你的答案。它在哪裏? – vcav

回答

0

不能解析格式化一次回long.You需要重新解析與格式化字符串,以獲得時間。

因此,在設置您的鬧鐘管理器之前,您需要創建另一個SimpleDateFormat對象。

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

現在

alarmManager.set(AlarmManager.RTC,(sdf.parse(getCurrentTime())).getTime(), pendingIntent); 
+0

好的,我明白了。現在,我在嘗試。 – vcav

+0

它的工作。謝謝。 – vcav

+0

我有一個問題。當我的應用完全關閉時,我可以顯示通知嗎? – vcav

0

變化

public String getCurrentTime() 

public long getCurrentTime() 

,並返回c.getTime().getTime()。這將返回當前時間milliSeconds。您可以操作Calendar實例來獲取所需的時間。

+0

我想要「YYYY-MM-DD HH:MM:SS」格式。所以我無法將String更改爲Long。所以,我可以在警報管理器中做些改變。 – vcav

+0

您需要擴展'AlarmManager'類並執行您自己的自定義實現,但最終'AlarmManager'只接受'long'值。 – Apoorv

+0

所以,唯一的辦法是改變長期回報... – vcav

0

也許這樣寫:

long triggerTime = System.currentTimeMillis(); 
alarmManager.set(AlarmManager.RTC, triggerTime, pendingIntent);