rk3288 添加系统广播
生活随笔
收集整理的這篇文章主要介紹了
rk3288 添加系统广播
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
可在自己需要的位置注冊(cè)接受廣播的action,這邊是注冊(cè)在Intent上。
1.添加action字符串
修改位置
2.注冊(cè)該廣播
修改位置
在需要的地方發(fā)送廣播
+ Intent intent = new Intent(Intent.ACTION_demo); + intent.putExtra("state",state); + sendBroadcastToAll(intent);+ private void sendBroadcastToAll(Intent intent) { + intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); + intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); + final long ident = Binder.clearCallingIdentity(); + try { + mContext.sendBroadcastAsUser(intent, UserHandle.ALL); + } finally { + Binder.restoreCallingIdentity(ident); + } + }3.可在系統(tǒng)中寫個(gè)接收的廣播,此例中,添加一個(gè)服務(wù),在服務(wù)中注冊(cè)接收廣播
新建服務(wù):
在frameworks/base/packages/SystemUI/src/com/android/systemui/文件夾新建一個(gè)自己的目錄,新建一個(gè)服務(wù)。
eg:frameworks/base/packages/SystemUI/src/com/android/systemui/test/TestService.java
新建廣播
eg:frameworks/base/packages/SystemUI/src/com/android/systemui/test/SwitchAppReceiver.java
+package com.android.systemui.test; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; +import android.content.pm.PackageManager; +import android.util.Log; + +public class SwitchAppReceiver extends BroadcastReceiver { + + public static final String TAG = "swith_app"; + + @Override + public void onReceive(Context context, Intent intent) { + Log.i(TAG, "SwitchAppReceiver onReceive=="); + //do something+ } + +}4.在合適的地方開啟服務(wù)
frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBarPolicy.javapublic PhoneStatusBarPolicy(Context context, StatusBarIconController iconController) {/* start test switch app */Log.d("TestService","##################");Intent mIntent = new Intent(mContext,TesService.class); mContext.startService(mIntent);}總結(jié)
以上是生活随笔為你收集整理的rk3288 添加系统广播的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android Studio Flavo
- 下一篇: 卡巴斯基离线升级方法图解