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

应用场景

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

你不知道的android黑科技:暗码启动应用程序_第1张图片
暗码启动示例

原理

如果你详细研究过android源码(当然本人看的是低版本的你懂的),在联系人模块中有一个文件:
packages/apps/Contacts/src/com/android/contacts/SpecialCharSequenceMgr.java
他里面有一段关键代码是这样写的:

/** 
     * Handles secret codes to launch arbitrary activities in the form of *#*##*#*. 
     * If a secret code is encountered an Intent is started with the android_secret_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 *#*##*#*  
        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 >#*#* 这样的指令时,程序会对外发送广播。这就意味着我们都能够接收这个广播然后做自己想做的事情了。接下来我们看看代码怎么写:

暗码启动代码

  • 1.自己设置一个key准备作为启动的指令

          public interface Constants {
           //通过暗码拨号进入应用的key 需要与Manifest中注册broadcast时host节点的值对应
              String SECRET_CODE = "123";
          }
    
  • 2.在AndroidManifest文件中注册广播接收器

        
              
                  
                  
              
          
    
  • 3.接收广播启动应用

    public class PhoneBroadcastReceiver extends BroadcastReceiver {

    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以上的系统未经测试如果感兴趣的可以自己去试试)

继续探索

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

你不知道的android黑科技:暗码启动应用程序_第2张图片
拨号启动示例

拨号启动代码

  • 1.自己设置一个key准备作为拨号启动的指令

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

    public interface Constants {
        //拨号进入应用的指令
        String DIALING_INSTRUCTIONS = "110";
    }
  • 2.在AndroidManifest文件中添加权限

      
    
  • 3.在AndroidManifest文件中注册广播接收者

      
                  
                      
                  
              
    
  • 4.接收广播启动应用

      public class PhoneBroadcastReceiver extends BroadcastReceiver {
      
          @Override
          public void onReceive(Context context, Intent intent) {
      
              String action = intent.getAction();
              switch (action) {
                  case Intent.ACTION_NEW_OUTGOING_CALL:
    
                      //取得播出电话的号码
                      String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
    
                      //匹配拨号指令
                      if (Constants.DIALING_INSTRUCTIONS.equals(phoneNumber)) {
                      
                          //跳转到应用
                          Intent mIntent = new Intent(context, LoginActivity.class);
                          mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                          context.startActivity(mIntent);
                      
                          // 掐断广播
                          setResultData(null);
                       }
                      break;
              }
      
          }
    
  • 5.注意事项
    本人使用5.1系统的手机测试,使用这个方法启动应用需要手机在安装了手机卡的情况下,不然拨号不会成功,会弹出没有手机卡的提示。 需要注意的是一定要在广播接收到以后掐断广播,即:setResultData(null); 不然界面跳转以后拨号还在继续。

结语

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

你可能感兴趣的:(你不知道的android黑科技:暗码启动应用程序)