AlarmManager和BroadcastReceiver的简单实现,我无法让它运行。由于很多代码无法使用Logs检查(这是我通常所做的),我想我会问你们。我的主要活动中的onCreate()方法调用:
private void setupBackupService() {
// TODO Auto-generated method stub
alarmMgr = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, BackupReceiver.class);
PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
alarmMgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() +
10 * 1000, 100, pIntent);
Log.i("main","hello");
}
BacupReceiver看起来像:
public class BackupReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.i("BroadcastReceiver","hello");
}
}
和清单文件的一部分:
<receiver android:name="BackupReceiver" >
</receiver>
答案 0 :(得分:2)
尝试修改您的清单:
<receiver android:name=".BackupReceiver"/>
或使用android:name="your.app.package.BackupReceiver"
希望有所帮助
答案 1 :(得分:1)
如果您的计划是使用AlarmManager
启动服务,则可以完全跳过广播接收器并使用pIntent.getService
。
关于它的教程: http://www.vogella.com/articles/AndroidServices/article.html#scheduleservice_scheduling