定义一个新的广播:
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);
}
}
}