花了几天时间在这里找到答案,但一无所获。
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 中祝酒)。我停止了它,删除数据并再次开始按 Ctrl-F11,在第 3 次迭代 - 工作,第 5 次迭代也在工作......它是什么?