Android 使用暗码启动App

本文介绍了如何在Android系统中通过暗码启动无界面应用。详细讲解了Dialer源码中暗码的发送过程,并给出了接收暗码广播并启动App的实现方法,包括静态注册广播接收器和相应代码示例。

Android 使用暗码启动App


     暗码,比如*#06#查看手机IMEI号。*#06#就是一个暗码。

  我们可能有时会有这样的需求,比如我想通过手机拨号来启动一个内置无界面的app(假设拨*#*#6636#*#*),那该如何实现?


  既然暗码是点击拨号按钮启动的,那么肯定是在源码Dialer 中通过全局广播形式发给其他内置app的,废话不多说,进入Dialer 源码看看就 明白了

系统源码中可以体现出来:
   \android\packages\apps\Dialer\ src\com\android\dialer\dialpad\DialpadFragment.java
    a .拨号盘的输入内容处理是在 DialpadFragment.java public void afterTextChanged(Editable input) {}方法中

    b.fterTextChanged(Editable input) {}方法中 会调用 
        SpecialCharSequenceMgr.handleChars(getActivity(), input.toString(), mDigits)) {}

    c.handleChars方法中会对暗码及PIN码进行处理
        代码如下:

      public static boolean handleChars(Context context, String input, EditText textField) {
        //get rid of the separators so that the string gets parsed correctly
        String dialString = PhoneNumberUtils.stripSeparators(input);
        if (handleDeviceIdDisplay(context, dialString)
        || handleRegulatoryInfoDisplay(context, dialString)
        || handlePinEntry(context, dialString)
        || handleSecretCode(context, dialString)) {
        return true;
        }
        return false;
        }


    d. \android\packages\apps\Dialer\src\com\android\dialer\SpecialCharSequenceMgr.java
        看到 SECRET_CODE_ACTION 是从handleSecretCode这里发出来的

/**
* 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;
}

暗码的发送已讲完,下来看看接收。其实很简单就是静态注册广播,接收,启动App主界面即可

2. 需要启动APP操作:

a. (AndroidManifest.xml里面静态注册就是)。

   <receiver android:name=".MainActivityReceiver" android:exported="true">
       <intent-filter>
            <action android:name="android.provider.Telephony.SECRET_CODE"/>
            <data android:host="6636" android:scheme="android_secret_code"/>
        </intent-filter>
    </receiver>

b. 新增加TestSercretReceiver类,代码如下:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;

public class TestSecretReceiver extends BroadcastReceiver {
    
    private String TAG = "RuntimeSecretReceiver";
    private static final String SECRET_CODE_ACTION = "android.provider.Telephony.SECRET_CODE";
    private final Uri mUri = Uri.parse("android_secret_code://6636");
    
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "on receive secret code" + " action is " + intent.getAction());
        if (intent.getAction() == null) {
            Log.i(TAG, "Null action");
            return;
        }
        if (intent.getAction().equals(SECRET_CODE_ACTION)) {
            Uri uri = intent.getData();
            if (uri.equals(mUri)) {
                //注意启动自己需要的APP主界面即可
                Intent mIntent = new Intent(context, MainActivity.class);
                mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(mIntent);
            } else {
                Log.i(TAG, "No matched URI!");
            }
        } else {
            Log.i(TAG, "Not SECRET_CODE_ACTION!");
        }
    }
}


  如果本篇文章帮到了您,或者觉得不错,请帮忙点赞,你的关注是我分享者前进的动力,有不对的,或者好方法请

指出一起学习,转载请注明出处

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值