android - 收不到ACTION_TIME_TICK的广播
阿神
阿神 2017-04-17 13:58:56
[Android讨论组]

已解决……
问题不在于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似乎永远都达不到,哪有问题呢。。。

阿神
阿神

闭关修行中......

全部回复(1)
伊谢尔伦

已解决……
问题不在于ACTION_TIME_TICK,而是我中途调用的方法会在特定情况下stopself
我本以为在service中加的那个notification会随着stopself消失,结果它没消失……于是我以为是广播出了问题……

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号