我想在每天早上9點醒來,我的整個Android應用程序。我嘗試使用android的鬧鐘管理器。 但它調用等待的意圖。但在我的情況沒有待定的意圖,我想喚醒完整的應用程序。 以下是我的代碼。由於我在android開發初學者非常感謝任何幫助。如何在特定時間喚醒android應用程序?
public void SetAlarm() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 09);
cal.set(Calendar.MINUTE, 00);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
alarmMgr.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
}
而且在MainActivity類別:
public class MainActivity extends Activity {
String ua = "Mozilla/5.0 (Android; Tablet; rv:20.0) Gecko/20.0 Firefox/20.0";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setBackgroundColor(0);
WebSettings webSettings = myWebView.getSettings();
myWebView.getSettings().setUserAgentString(ua);
webSettings.setJavaScriptEnabled(true);
myWebView.setWebChromeClient(new WebChromeClient());
webSettings.setDomStorageEnabled(true);
myWebView.setWebViewClient(new MyAppWebViewClient());
myWebView.loadUrl("file:///android_asset/www/index.html");
SetAlarm();
}
}
但在上午9時它不醒來我的應用程序了。任何錯誤?
請確保您的清單中具有喚醒設備權限:<使用權限android:name =「android.permission.WAKE_LOCK」/>' – FabianCook
我已添加此權限 – SSS
@SSS請參閱我的答案,它會工作100% – Harshid