Android学习趣闻录Android知识

你不知道的android黑科技:暗码启动应用程序

2017-04-14  本文已影响599人  IT魔幻师

应用场景

有一些出厂测试的程序,或者一些隐秘的程序,不能将app的启动图标直接显示到程序列表中,所以我们需要将程序隐藏,通过拨号输入特定的号码或字符来启动程序。

暗码启动示例

原理

如果你详细研究过android源码(当然本人看的是低版本的你懂的),在联系人模块中有一个文件:
packages/apps/Contacts/src/com/android/contacts/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("#*#*")) {  
            Intent intent = new Intent(Intents.SECRET_CODE_ACTION,  
                    Uri.parse("android_secret_code://" + input.substring(4, len - 4)));  
            context.sendBroadcast(intent);  
            return true;  
        }  
  
        return false;  
    }  

可以看到在拨号中当接收到*#*#< code >#*#* 这样的指令时,程序会对外发送广播。这就意味着我们都能够接收这个广播然后做自己想做的事情了。接下来我们看看代码怎么写:

暗码启动代码

    public static final String SECRET_CODE_ACTION = "android.provider.Telephony.SECRET_CODE";

    @Override
    public void onReceive(Context context, Intent intent) {

        switch (intent.getAction()) {

            case SECRET_CODE_ACTION:
                 Uri uri = intent.getData();
                 if(uri != null){
                    String host = uri.getSchemeSpecificPart().substring(2);
        
                    if(Constants.SECRET_CODE.equals(host)){

                        //启动应用
                        Intent intent = new Intent(context, LoginActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(i);
                    }
                }
            break;
        }

    }
}

经过以上3步就能够实现暗码启动了, 只要在拨号中输入*#*#123#*#*就会启动自己的程序。(我是用的5.1的手机测试的能够正常启动5.1以上的系统未经测试如果感兴趣的可以自己去试试)

继续探索

很不幸的是当你去使用的时候会发现输入这个暗码口令也太麻烦了又长又难输,于是又有了新的需求能不能不要输入 *#*# #*#*这条麻烦发口令,于是我换了一种思路来处理这个问题,那就是通过拨号启动。通过监听拨号广播启动应用。效果如下:

拨号启动示例

拨号启动代码

使用这种方式会拦截掉您的拨号,千万不要将key设置为110,120 或者你老婆的电话啥的 (本文只是做一个示例未满18岁请勿模仿)

    public interface Constants {
        //拨号进入应用的指令
        String DIALING_INSTRUCTIONS = "110";
    }

结语

使用以上两种方法启动应用都需要自己建立activity栈对activity的顺序进行处理,不然如果应用还在运行再次拨号启动就会使得activity多重层叠。

上一篇下一篇

猜你喜欢

热点阅读