2014-02-06 112 views
2

我想在每天早上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時它不醒來我的應用程序了。任何錯誤?

+0

請確保您的清單中具有喚醒設備權限:<使用權限android:name =「android.permission.WAKE_LOCK」/>' – FabianCook

+1

我已添加此權限 – SSS

+0

@SSS請參閱我的答案,它會工作100% – Harshid

回答

1

介紹(BroadcastReceiver)多一步來打開您的完整應用程序。

AlarmManager - >broadcastReceiver - >您的應用程序飛濺/發射器頁面

完蛋了。

1

如果打開,你應該使用PendingIntent.getActivity的活動,而不是PendingIntent.getBroadcast

+0

好吧..我會嘗試getActivity,而不是getBroadcast – SSS

1

當你想設置每天,而不是alarmMgr.set使用alarmMgr.setReapting報警。

1
Intent intent = new Intent(con, YourAppReciever.class); 
PendingIntent sender = PendingIntent.getBroadcast(con, 0, intent, 
      PendingIntent.FLAG_UPDATE_CURRENT); 

創建一個接收器並監聽這個廣播。

public class YourAppReciever extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 

     context.startActivity(new Intent(context, MainActivity.class)); 
    } 

} 
+0

類型yourAppReciever未定義方法startActivity(意圖)。我收到這個錯誤。 – SSS

+0

查看編輯答案 – Triode

0

請遵循以下說明。

1. MainActivity.java

Intent myIntent = new Intent(getBaseContext(), MyReceiver.class); 
      PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 0, myIntent, 0); 
      AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
      Calendar calendar = Calendar.getInstance(); 
      calendar.setTimeInMillis(System.currentTimeMillis()); 
      calendar.add(Calendar.SECOND, 10); 
      long interval = 60 * 1000; // 
      alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), interval, pendingIntent); 

步驟2: Boradcast接收機,用於開始您的活動。

MyReceiver.java

public class MyReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Intent myIntent =new Intent(context, MainActivity.class); 
     myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     context.startActivity(myIntent); 
     System.out.println("Receiver Call"); 
    } 


} 

步驟3: 把你manifest.xml

<receiver android:name=".MyReceiver"/> 

更改間隔valuse按您的要求如果需要24小時,然後間隔= 24 * 60 * 60 * 1000。

這是一般解決方案,用於根據您的要求對用戶請求進行修改。

它會工作完美。快樂的編碼。

+0

我試過你的代碼。一旦我安裝它應該自動啓動吧?但它沒有醒來 – SSS

+0

你能告訴我如何設置時間(從現在開始5分鐘)5分鐘?所以我可以檢查它是否醒來? – SSS

+0

根據你的代碼,它會開始現在,並重復每一秒正確? – SSS

相關問題