Android5.1FM模块学习----向前台发送消息

本文介绍了Android 5.1中FM模块如何在服务监听到广播接收器,如ACTION_AIRPLANE_MODE_CHANGED时,向前台活动更新UI。通过实现监听器和回调机制,实现在手机切换飞行模式时,UI的即时响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.  描述:当FM service监听到某个Receiver,需要前台更新UI时

2.  实例:手机切换飞行模式,

//dongyang.li add for airplane mode.
        if(getAirplaneMode(getApplicationContext())) {
            mIsPowerUping = false;
            bundle = new Bundle(5);
            bundle.putInt(FmRadioListener.CALLBACK_FLAG, FmRadioListener.MSGID_AIRPLANE_MODE);
            bundle.putBoolean(FmRadioListener.FLAG_AIRPLANE_MODE, true);
            notifyActivityStateChanged(bundle);
            return;
        }




private class FmServiceBroadcastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            String command = intent.getStringExtra("command");
            if(Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(action)){
                boolean isAirPlaneMode = intent.getBooleanExtra("state",false);
                if(isAirPlaneMode){
                    if (mIsPowerUp) {
                        powerDown();
                    }
                }
                else{
                  if (!mIsPowerUp) {
                   tuneStation(FmRadioUtils.computeFrequency(getFrequency()));
                  }
                }
            }


3.  ACTION_AIRPLANE_MODE_CHANGED在Intent.java中有定义

public static final String ACTION_AIRPLANE_MODE_CHANGED = "android.intent.action.AIRPLANE_MODE";

4.  notifyActivityStateChanged()

// record the listener list, will notify all listener in list
    private ArrayList<Record> mRecords = new ArrayList<Record>();


/**
     * Register FM Radio listener, activity get service state should call this
     * method register FM Radio listener
     *
     * @param callback FM Radio listener
     */
    public void registerFmRadioListener(FmRadioListener callback) {
        synchronized (mRecords) {
            // register callback in AudioProfileService, if the callback is
            // exist, just replace the event.
            Record record = null;
            int hashCode = callback.hashCode();
            final int n = mRecords.size();
            for (int i = 0; i < n; i++) {
                record = mRecords.get(i);
                if (hashCode == record.mHashCode) {
                    return;
                }
            }
            record = new Record();
            record.mHashCode = hashCode;
            record.mCallback = callback;
            mRecords.add(record);
        }
    }

private void remove(int hashCode) {
        synchronized (mRecords) {
            Iterator<Record> iterator = mRecords.iterator();
            while (iterator.hasNext()) {
                Record record = (Record) iterator.next();
                if (record.mHashCode == hashCode) {
                    iterator.remove();
                }
            }
        }
    }

/**
     * Call back from service to activity
     *
     * @param bundle The message to activity
     */
    private void notifyActivityStateChanged(Bundle bundle) {
        if (!mRecords.isEmpty()) {
            Log.d(TAG, "notifyActivityStatusChanged:clients = " + mRecords.size());
            synchronized (mRecords) {
                Iterator<Record> iterator = mRecords.iterator();
                while (iterator.hasNext()) {
                    Record record = (Record) iterator.next();

                    FmRadioListener listener = record.mCallback;

                    if (listener == null) {
                        iterator.remove();
                        return;
                    }

                    listener.onCallBack(bundle);
                }
            }
        }
    }















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值