1

AlarmManager 和 BroadcastReceiver 的简单实现,我就是无法让它运行。由于很多代码无法用日志检查(这是我通常做的),我想我会问你们。我的主要活动中的 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>
4

2 回答 2

3

尝试修改您的清单:

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

或使用android:name="your.app.package.BackupReceiver"
希望有帮助

于 2013-10-28T20:23:48.370 回答
1

如果您的计划是使用 启动服务,则AlarmManager可以完全跳过广播接收器而直接使用pIntent.getService.

关于它的教程: http ://www.vogella.com/articles/AndroidServices/article.html#scheduleservice_scheduling

于 2013-10-28T20:32:27.667 回答