2013-04-26 103 views
2

這是我的第一個問題。我正在製作一個Android應用程序,它會計算自動生成日期,以便通過生成日期來喚醒鬧鐘。每個預定日期我都有一個指揮官報警時間。調度程序與警報管理器...重新啓動後無法工作

c8.add(Calendar.MONTH, 18); 
    sdf = new SimpleDateFormat("yyyy-dd-MM"); 
    //String output = sdf.format(c.getTime()); 
    Date eighteendtmonth= new Date(c8.getTimeInMillis()); 
    dteighteenmonth = sdf.format(eighteendtmonth); 
    System.out.println("Eighteen Month date is--->"+dteighteenmonth); 

    c8.set(Calendar.HOUR_OF_DAY, bhour); 
    c8.set(Calendar.MINUTE, bminute); 

    try { 
     Date date1 = df.parse(dteighteenmonth);  //birthdate 
     Date date2 = df.parse(currentdate);  // 
     if(date1.equals(date2) || date1.after(date2)) 
     { 
      setAlarm(c8, eighteenmonth, dteighteenmonth, alarmtime, name); 
     } 

    } catch (ParseException e) { 
     e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. 
    } 

    private void setAlarm(Calendar targetCal, String detail, String vdate, String vtime, String childname){ 

     alarmid =(int) System.currentTimeMillis(); 

     System.out.println("vdate is--->"+vdate); 
     System.out.println("alarm time is--->"+vtime); 
     System.out.println("Alarm is set----->"); 
     Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class); 
     intent.setAction("android.intent.action.MAIN"); 
     intent.putExtra("detail", detail); 
     intent.putExtra("childname", name); 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), alarmid, intent, PendingIntent.FLAG_ONE_SHOT); 
     AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE); 

     alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent); 



} 

的BroadcastReceiver ...

 public void onReceive(Context context, Intent intent) { 

     Intent pushIntent = new Intent(context, Childlist.class); 
     pushIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     context.startService(pushIntent); 

Toast.makeText(上下文中,詳細地說,Toast.LENGTH_LONG).show();

AndroidManifest.xml中

<uses-permission android:name='android.permission.WAKE_LOCK'/> 
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> 
<receiver 
       android:name=".AlarmReceiver" 
       android:enabled="true" 
       android:exported="true" 
       android:label="AlarmReceiver"> 
      <intent-filter> 
        <action android:name="android.intent.action.MAIN"/> 
       <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      </intent-filter> 
     </receiver> 
<receiver android:name=".AlarmReceiver" android:process=":remote" /> 

所以我的問題是,我的調度工作完美。但重啓設備後沒有一個警報工作。幫助meee :))

+0

請刪除'android:process =:remote',因爲它沒有必要,並且浪費資源。 – CommonsWare 2013-04-26 21:57:55

回答

8

下面的內容已經從博客Android Boot Reciever

對於這種採取我們需要一個應該接受「BOOT_COMPLETED」廣播的廣播接收機。我們必須知道,當一個設備完成啓動時,Android系統發送「BOOT_COMPLTED」廣播。

在Android清單文件

   <receiver android:name=".BootReceiver"> 
        <intent-filter> 
          <action android:name="android.intent.action.BOOT_COMPLETED" /> 
          <category android:name="android.intent.category.HOME" /> 
        </intent-filter> 
      </receiver> 

,不要忘記添加以下權限清單中註冊的BootReciever。

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

此權限需要聽/遐想BOOT_COMPLETED行動

BootReceiver.java

public class BootReceiver extends BroadcastReceiver 
    { 

     public void onReceive(Context context, Intent intent) 
     { 

       // Your code to execute when Boot Completd 
       **Schedule your Alarm Here** 
       Toast.makeText(context, "Booting Completed", Toast.LENGTH_LONG).show(); 

     } 

    } 

BootReceiver的onRecieve()方法啓動完成時將執行,所以凌晨必要寫onreceive()方法中的代碼

[

0

重新啓動時會清除警報。您可以在啓動時將它們重新計劃,方法是將它們存儲在數據庫中或以某種方式將它們序列化,然後使用帶有<action android:name="android.intent.action.BOOT_COMPLETED" />操作的BroadcastReceiver啓動在啓動完成時設置警報的服務。

+0

我已經在BroadcastReceiver中放入了。如何從數據庫啓動警報......任何提示? – 2013-04-26 21:38:44

+0

@ user2226571:「如何從數據庫啓動警報......任何提示?」 - 您的工作方式與您首先設置鬧鐘的方式相同。 – CommonsWare 2013-04-26 21:57:33

+0

@CommonsWare是的,我知道我有這個項目的數據庫類。但是如何在手機重啓之後啓動服務?我的代碼工作正常,直到設備重新啓動.... – 2013-04-27 03:29:13

相關問題