rk3288 添加系统广播
生活随笔
收集整理的這篇文章主要介紹了
rk3288 添加系统广播
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
可在自己需要的位置注冊接受廣播的action,這邊是注冊在Intent上。
1.添加action字符串
修改位置
2.注冊該廣播
修改位置
在需要的地方發送廣播
+ 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.可在系統中寫個接收的廣播,此例中,添加一個服務,在服務中注冊接收廣播
新建服務:
在frameworks/base/packages/SystemUI/src/com/android/systemui/文件夾新建一個自己的目錄,新建一個服務。
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.在合適的地方開啟服務
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);}總結
以上是生活随笔為你收集整理的rk3288 添加系统广播的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android Studio Flavo
- 下一篇: windows桌面怎么添加计算机,Win