Android 11.0 长按按键切换SIM卡默认移动数据

Android 11.0 长按按键切换SIM卡默认移动数据

近来收到客户需求想要通过长按按键实现切换SIM卡默认移动数据的功能,该功能主要通过长按按键发送广播来实现,具体修改参照如下:

首先创建广播,具体修改参照如下:

/vendor/mediatek/proprietary/packages/apps/MtkSettings/AndroidManifest.xml
 

+       
+            
+                
+            
+        

其次合入具体功能实现的SimReceiver.java文件:

/vendor/mediatek/proprietary/packages/apps/MtkSettings/src/com/android/settings/sim/SimReceiver.java

package com.android.settings.sim;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.RingtoneManager;
import android.net.Uri;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.util.Log;
import java.util.List;

public class SimReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("android.intent.action.sim.longpress")) {

	    SubscriptionManager subscriptionManager = SubscriptionManager.from(context);
	    List subscriptionInfoList = subscriptionManager.getActiveSubscriptionInfoList();
	    if (subscriptionInfoList != null && subscriptionInfoList.size() > 1) {
    		int subId1 = subscriptionInfoList.get(0).getSubscriptionId();
    		int subId2 = subscriptionInfoList.get(1).getSubscriptionId();
	        if (SubscriptionManager.getDefaultDataSubscriptionId() == subId1){
    	            subscriptionManager.setDefaultDataSubId(subId2);
	        } else if (SubscriptionManager.getDefaultDataSubscriptionId() == subId2){
    	            subscriptionManager.setDefaultDataSubId(subId1);
	        }
	    }
        }
    }
}

然后配置对应按键长按发送广播,具体修改参照如下:

/frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java

+            case KeyEvent.KEYCODE: {
+                if (down) {
+		    Handler mHandler = new Handler();
+                    mHandler.postDelayed(new Runnable() {
+                        public void run() {
+                            sendBC("android.intent.action.sim.longpress");
+                        }
+                    }, 2 * 1000);
+                }

+    private void sendBC(String action) {
+        Intent intent = new Intent();
+        intent.setAction(action);
+        intent.addFlags(0x01000000);
+        mContext.sendBroadcast(intent);
+    }

最后注册接收广播:

/vendor/mediatek/proprietary/packages/apps/MtkSettings/src/com/android/settings/sim/SimDialogActivity.java

+import android.content.IntentFilter;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        showOrUpdateDialog();
+	SimReceiver aSimReceiver = new SimReceiver();
+       IntentFilter intentFilter = new IntentFilter();
+       intentFilter.addAction("android.intent.action.sim.longpress");
+       registerReceiver(aSimReceiver ,intentFilter);
    }

重新编译验证,修改生效,已实现通过长按按键切换SIM卡默认移动数据功能

你可能感兴趣的:(framework,Android,android)