Android隐藏菜单修改usb模式

需要通过播隐藏码66336来调用隐藏菜单,在其中控制手机USB口的模式。

步骤是:

1、注册监听隐藏码

在AndroidManifest.xml里注册隐藏码的监听

<receiver android:name="ModemPortBroadcastReceiver">
      <intent-filter>
        <action android:name="android.provider.Telephony.SECRET_CODE" />
        <data android:scheme="android_secret_code" android:host="66336" />
      </intent-filter>
      </receiver>

对应的监听类:

import static com.android.internal.telephony.TelephonyIntents.SECRET_CODE_ACTION;

import android.content.Context;
import android.content.Intent;
import android.content.BroadcastReceiver;
import android.util.Log;
import android.view.KeyEvent;


public class ModemPortBroadcastReceiver extends BroadcastReceiver {
  
    public ModemPortBroadcastReceiver() {
    }
    
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(SECRET_CODE_ACTION)) {
            Intent i = new Intent(Intent.ACTION_MAIN);
            i.setClass(context, ModemActivity.class);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
        }
    }
}

监听到隐藏码请求后调用主界面出来。

2、界面控件

界面没有可发挥的地方,只是要求通过滑动来控制就可以,所以直接用一个switch控件就好。

xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".ModemActivity" >

    <TextView
        android:id="@+id/WelcomeView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/welcome" />

    <Switch
        android:id="@+id/ChangeSwitch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_below="@+id/WelcomeView"
        android:layout_marginTop="14dp"
        android:textOff="Close"
        android:textOn="On"
         />

</RelativeLayout>

3、保存问题

  功能实现方面读取"persist.sys.usb.config"属性值判断修改即可(我这边是这个值)。但是不能简单滑动更改完属性就行。比如通过switch把原来为mtp,adb改成acm后如果用户退出后再重新进入时必须能退回到原来的mtp,adb,反之亦然。为了实现这个效果不能是保存成程序里的变量,因为退出程序后变量也不存在只好恢复成原来的值来保证原来的值不丢失,而对方调出隐藏功能后,滑动修改后可能是直接退出然后继续自己的操作而手机恢复成原来状态的话就没用了,那就不得不一直开着这个界面。

  所以我是新建一个属性用来保存修改之前的值,这样退出界面状态值仍为修改值而后重新通过隐藏菜单进入恢复时可以通过这个新建属性里保存的旧值来恢复为原值,期间关机重启仍然会恢复原值所以也不用担心。

Activity主要代码:

String value = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_modem);
		Switch sw = (Switch)findViewById(R.id.ChangeSwitch);
		value = SystemProperties.get("persist.sys.usb.config", "");
		if(value.equals("acm"))
		{
			sw.setChecked(true);
		}
		sw.setOnCheckedChangeListener(new OnCheckedChangeListener() {  
            @Override  
            public void onCheckedChanged(CompoundButton buttonView,  
                    boolean isChecked) {   
                if (isChecked) {
                	value = SystemProperties.get("persist.sys.usb.config", null);
                	SystemProperties.set("persist.sys.usb.oldconfig",value);
                	Log.i(TAG,"checked:"+value);
                	SystemProperties.set("persist.sys.usb.config", "acm");
                } else {  
                	  value = SystemProperties.get("persist.sys.usb.oldconfig", null);
                	  if(value != null){
                      SystemProperties.set("persist.sys.usb.config", value);

                	  }
                }  
            }
		});
	}
persist.sys.usb.oldconfig属性是自己新定义的属性,用来保存调用前的状态值。

为了使用SystemProperties,需要系统权限,好在不是在做第三方APP,所以只需要在Android.mk和AndroidMenifest.xml里添加权限就好:

AndroidMenifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.modemdiagport"
    android:sharedUserId="android.uid.system">
Android.mk:

LOCAL_CERTIFICATE := platform


暂时没想到不用保存到新属性的方法,先记录一下。




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值