定义一个新的protected-broadcast广播


定义一个新的广播:
1,在frameworks\base\core\res中的AndroidManifest.xml
<protected-broadcast android:name="android.intent.action.POWER_BUTTON_LONG_PRESS" />
2,被定义为protected-broadcast的广播--受保护的广播,只能是拥有系统权限的APP,其他APP没有权限发送该类广播
一、android:sharedUserId=”android.uid.system”系统中所有使用android.uid.system作为共享UID的APK,都会首先在manifest节点中增加 android:sharedUserId=”android.uid.system”,然后在Android.mk中增加 LOCAL_CERTIFICATE := platform
二、AMS对系统应用发出的广播进行安全检查
3,发送广播:
                    Intent intent2 = new Intent("android.intent.action.POWER_BUTTON_XXX_PRESS");
                    mContext.sendBroadcast(intent2);
注册:
APP manifest.xml:
注册1::
        <receiver android:name=".receiver.DevicePowerButtonReceiver">
            <intent-filter>
                <action android:name="android.intent.action.POWER_BUTTON_XXX_PRESS" />
            </intent-filter>
        </receiver>
        
注册2:
    public void register(Context context) {
        IntentFilter filter = new IntentFilter();
        filter.addAction(PowerButton.ACTION_POWER_BUTTON_SHORT_PRESS);
        filter.addAction(PowerButton.ACTION_POWER_BUTTON_LONG_PRESS);
        context.registerReceiver(this, filter);
    }
    
接收:
public abstract class PowerButtonReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();
        if (PowerButton.ACTION_POWER_BUTTON_SHORT_PRESS.equals(action)) {
            handleShortPress(context);
        } else if (PowerButton.ACTION_POWER_BUTTON_LONG_PRESS.equals(action)) {
            handleLongPress(context);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值