2014-01-21 91 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