已解决……
问题不在于ACTION_TIME_TICK,而是我中途调用的方法会在特定情况下stopself
我本以为在service中加的那个notification会随着stopself消失,结果它没消失……于是我以为是广播出了问题……
以下是原题……
——————————————————————————————————————————————————————————————————————
我看google是这么说的
Broadcast Action: The current time has changed. Sent every minute. You
cannot receive this through components declared in manifests, only by
explicitly registering for it with Context.registerReceiver().
于是我的代码:
public class AlarmService extends Service {
......
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.alarm_notify);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(0, builder.build());
// alarm = getNextAlarm(); update: 这行居然是元凶!我稍微看看这个方法再更新一下题目。。。
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_TIME_TICK);
timeTickReceiver = new TimeTickReceiver();
registerReceiver(timeTickReceiver, intentFilter);
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(timeTickReceiver);
}
class TimeTickReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "received", Toast.LENGTH_SHORT).show();
}
}
}
debug能走到registerReceiver(timeTickReceiver, intentFilter);
这一行,但后面的onReceive似乎永远都达不到,哪有问题呢。。。
已解决……
问题不在于ACTION_TIME_TICK,而是我中途调用的方法会在特定情况下stopself
我本以为在service中加的那个notification会随着stopself消失,结果它没消失……于是我以为是广播出了问题……