2014-01-21 89 views
0

編輯:任何人在這個絆倒可能想知道,重啓後發出警報,你需要註冊它在清單,而不是運行時。重新啓動後Android報警不會啓動:

我有以下類我設計從Here使用指南:

public class MainActivity extends Activity { 
    private AlarmManager alarmMgr; 
    private PendingIntent alarmIntent; 
    BroadcastReceiver br; 
    TextView t; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     setup(); 
     t = (TextView)findViewById(R.id.textView1); 


     ComponentName receiver = new ComponentName(getApplicationContext(), SampleBootReceiver.class); 
     PackageManager pm = getApplicationContext().getPackageManager(); 

     pm.setComponentEnabledSetting(receiver, 
       PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 
       PackageManager.DONT_KILL_APP); 


     Calendar calendar = Calendar.getInstance(); 
     calendar.setTimeInMillis(System.currentTimeMillis()); 
     calendar.set(Calendar.HOUR_OF_DAY, 10); 
     calendar.set(Calendar.MINUTE, 22); // Particular minute 
     calendar.set(Calendar.SECOND, 0); 
     alarmMgr = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE); 
     alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 
       1000*60*60*24, alarmIntent); 
    } 


    public void setup() { 
     br = new BroadcastReceiver() { 
      @Override 
      public void onReceive(Context c, Intent i) { 
       Toast.makeText(c, "Rise and Shine!", Toast.LENGTH_LONG).show(); 
       //Invoke the service here Put the wake lock and initiate bind service 
       t.setText("Hello Alarm set"); 
      } 
     }; 
     registerReceiver(br, new IntentFilter("com.testrtc")); 
     alarmIntent = PendingIntent.getBroadcast(this, 0, new Intent("com.testrtc"), 
       0); 
     alarmMgr = (AlarmManager)(this.getSystemService(Context.ALARM_SERVICE)); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

} 

我有一個SampleBootReceiver類是:

public class SampleBootReceiver extends BroadcastReceiver { 
    private AlarmManager alarmMgr; 
    private PendingIntent alarmIntent; 
    BroadcastReceiver br; 
    TextView t; 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) { 
      Toast.makeText(context, "Hello from Bootloader", 10000).show(); 
      Calendar calendar = Calendar.getInstance(); 
      calendar.setTimeInMillis(System.currentTimeMillis()); 
      calendar.set(Calendar.HOUR_OF_DAY, 10); 
      calendar.set(Calendar.MINUTE, 22); // Particular minute 
      calendar.set(Calendar.SECOND, 0); 
      alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
      alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 
         1000*60*60*24, alarmIntent); 


     } 
    } 
} 

這裏是我的清單:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.testrtc" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="18" /> 

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

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.testrtc.MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <receiver 
      android:name=".SampleBootReceiver" 
      android:enabled="false" > 
      <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED" > 
       </action> 
      </intent-filter> 
     </receiver> 
    </application> 

</manifest> 

鬧鐘工作正常在重新啓動之前,重新啓動後,我也從BootReceiver類獲得Toast消息。但警報不會重置。這裏我想澄清一點,Docs聲明警報不會被重置,除非應用程序至少由用戶啓動一次:Set the RECEIVE_BOOT_COMPLETED permission in your application's manifest. This allows your app to receive the ACTION_BOOT_COMPLETED that is broadcast after the system finishes booting (this only works if the app has already been launched by the user at least once):此聲明的內容是什麼?如果用戶必須重新啓動應用程序,無論如何,onCreate將被調用並且鬧鐘將被重新設置。或者這個聲明是否意味着在整個生命週期中,應用程序必須至少運行一次電話?

+0

對於出現此問題的所有API級別..? – SilentKiller

+0

我沒有在API級別19上進行測試,但是這個問題在Jelly Bean中。 – Skynet

+0

那件事我以爲我曾經在JellyBean面臨同樣的事情,我發現BroadcastListener在啓動後不能自動工作在4.0以上,所以我創建了一個服務來運行我的廣播。 – SilentKiller

回答

1

我想,你的報警工作正常,但是當你的SampleBootReceiver在啓動完成後再次啓動時,我不會調用setup()方法。

SampleBootReceiver再次添加此行得到alarmIntent

registerReceiver(br, new IntentFilter("com.testrtc")); 
     alarmIntent = PendingIntent.getBroadcast(this, 0, new Intent("com.testrtc"), 
       0); 
     alarmMgr = (AlarmManager)(this.getSystemService(Context.ALARM_SERVICE)); 

感謝

+0

但我想我只能在擴展Activity的類中定義一個onReceive(用於鬧鐘)。 – Skynet

+0

你使用新的IntentFilter(「com.testrtc」)來啓動一些事情,你需要再次調用來完成這項工作。 –

+0

如何做到這一點,你能給我一個詳細的例子嗎? – Skynet