有些日子在这里找到答案,但没有。
public class myReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
}
}
的活动:
public class TestActivity extends Activity {
private myReceiver rec;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
rec = new myReceiver(); // do I really need this?
AlarmManager am=(AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, myReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 5 , pi);
}
}
XML
<application ...>
...
<receiver android:name="com.myprj.myReceiver" android:enabled="true"></receiver>
</application>
它并非总是如此,但在“它想要”时:(
注意!:我开始在Eclipse中按Ctrl + F11并使用后退按钮关闭应用程序并再次按Ctrl-F11!因此,在第7次迭代中,应用程序工作(onReceive中的toast)。我停止了它,删除数据并再次按下Ctrl-F11,在第3个迭代器上工作,第5次迭代也在工作......它是什么?
答案 0 :(得分:0)
您可能尝试的变化很少(并非所有变化都是必要的)
将myReceiver更改为MyReceiver(:-),这是一个Java命名约定)
删除
rec = new myReceiver();
在millis中提供一些未来时间而不是当前时间
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 1000 * 5, 1000 * 5 , pi);
将调试消息放入onReceive()
Log.d("MyReceiver", "onReceive");