通过Dialer拨号盘输暗码启动某个apk

来源:互联网 发布:系统垃圾清理软件 编辑:程序博客网 时间:2024/05/21 05:21

通过拨号暗码 启动某个apk

首先\packages\apps\Dialer\src\com\android\dialer\SpecialCharSequenceMgr.java 定义了这个方法

/**
     * Handles secret codes to launch arbitrary activities in the form of *#*#<code>#*#*.
     * If a secret code is encountered an Intent is started with the android_secret_code://<code>
     * URI.
     *
     * @param context the context to use
     * @param input the text to check for a secret code in
     * @return true if a secret code was encountered
     */
    static boolean handleSecretCode(Context context, String input) {
        // Secret codes are in the form *#*#<code>#*#*
        int len = input.length();
        if (len > 8 && input.startsWith("*#*#") && input.endsWith("#*#*")) {
            final Intent intent = new Intent(SECRET_CODE_ACTION,
                    Uri.parse("android_secret_code://" + input.substring(4, len - 4)));
            context.sendBroadcast(intent);
            return true;
        }


        return false;
    }

构建一个EMStartReceiver 在AndroidManifest.xml中定义 <intent-filter> 对应暗码

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


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


</receiver>

EMStartReceiver 代码如下 放在源码中编译

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import java.lang.String;
import com.android.internal.telephony.TelephonyIntents;
import android.os.SystemProperties;


public class  EMStartReceiver extends BroadcastReceiver {


    private static final String TAG = "EMStartReceiver";


    public EMStartReceiver() {
    }


    @Override
    public void onReceive(Context context, Intent intent) {
        String host = null;
        Uri uri = intent.getData();
        if (uri != null) {
            host = uri.getHost();
        } else {
            Log.d(TAG,"uri is null");
            return;
        }
        Intent i = new Intent(Intent.ACTION_MAIN);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);


        if("xxxxx".equals(host) || "xxxxx".equals(host)){
            i.setClass(context, EngineerModeActivity.class);
            context.startActivity(i);
        } 
    }
}



原创粉丝点击