2013-10-29 50 views

回答

1

使用AlarmManager

AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 

// Replace MyActivity.class with the activity class you want to run periodically 
Intent i = new Intent(context, MyActivity.class); 
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0); 

long now = System.currentTimeMillis(); 
long interval = 60 * 60 * 1000; // one hour 

am.setRepeating(AlarmManager.RTC_WAKEUP, now, interval, pi); 

而且,這個權限添加到您的AndroidManifest.xml:

<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission> 
0

您可以做的是使用後臺服務。從服務 您可以根據需要啓動您的應用程序。

使用該服務從

Intent intent = new Intent(getBaseContext(), myActivity.class); 
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
getApplication().startActivity(intent); 

服務參考 http://www.vogella.com/articles/AndroidServices/article.html

0

使用報警管理器(AM)或GCM。通過AM,您可以設置計時器並在一段時間內啓動您的應用程序。通過GCM,您可以從服務器發送通知並運行一些操作。

使用服務不是一個好主意。 AM和GCM是更可靠的方式。我認爲AM是你需要的。

P.S.不要這樣做。用戶會罵你)

+1

GCM是一種矯枉過正,它需要連接性,並不總是可靠的在確切的指定時間交付(也稱爲未發送消息的已知錯誤)。 AlarmManager是要走的路。 – kamituel

+0

是的,GCM相當複雜。如果間隔時間比imho AM小(一天一次/幾次),但如果間隔大約一週,則GCM是一種很好的替代方法。 – Leonidos

相關問題