瑞芯微Rockchip开发者社区
直播中

张龙祥

8年用户 1269经验值
擅长:连接器
私信 关注
[经验]

RK3288添加系统广播的步骤

可在自己需要的位置注册接受广播的action,这边是注册在Intent上。

1.添加action字符串

修改位置

frameworks/base/core/java/android/content/Intent.java

/**

*@hide

@hide必须要加

**/
@SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)

public static final String ACTION_demo="android.intent.action.demo";

2 注册该广播

修改位置

frameworks/base/core/res/AndroidManifest.xml

在需要的地方发送广播

  • 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

+package com.android.systemui.test;
+
+import android.app.Service;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.os.IBinder;
+import android.util.Log;
+
+
+public class TestService extends Service {
+

  • private static final String TAG = "TestService";
  • private SwitchAppReceiver receiver;
  • @Override
  • public void onCreate() {
  • super.onCreate();
    
  • Log.i(TAG,"TestService oncreate");
    
  • }
  • @Override
  • public int onStartCommand(Intent intent, int flags, int startId) {
  • Log.i(TAG,"TestService onStartCommand");
    
  • registerReceiver();
    
  • return super.onStartCommand(intent, flags, startId);
    
  • }
  • @Override
  • public IBinder onBind(Intent intent) {
  • return null;
    
  • }
  • @Override
  • public void onDestroy() {
  • Log.i(TAG,"TestService onDestroy");
    
  • unRegistReceiver();
    
  • super.onDestroy();
    
  • }
  • private void registerReceiver() {
  • receiver = new SwitchAppReceiver();
    
  • IntentFilter filter = new IntentFilter();
    
  • filter.addAction(Intent.ACTION_TES_SWITCH_APP);
    
  • registerReceiver(receiver, filter);
    
  • }
  • private void unRegistReceiver() {
  • try {
    
  • this.unregisterReceiver(receiver);
    
  • } catch (Exception e) {
    
  • }
    
  • }
    +}

新建广播

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.java

public PhoneStatusBarPolicy(Context context, StatusBarIconController iconController) {
/* start test switch app */
Log.d("TestService","##################");
Intent mIntent = new Intent(mContext,TesService.class);
mContext.startService(mIntent);
}

原作者:hxy_sakura

更多回帖

发帖
×
20
完善资料,
赚取积分