最近做一个出厂测试的程序,因为不能直接显示到程序列表中,因此需要将程序隐藏,再通过拨打*#*#xxxx#*#*来启动程序。在这里和大家分享分享。
/** * 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>#*#* 时,程序会发送广播,那么我们只需要接收这个广播再去做相应处理就OK了。
package com.xxx.ftest; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.net.Uri; public class SecretLaunchReceiver extends BroadcastReceiver { public static final String SECRET_CODE_ACTION = "android.provider.Telephony.SECRET_CODE"; /** * the uri is like : android_secret_code://1234 */ @Override public void onReceive(final Context context, Intent intent) { if (intent.getAction().equals(SECRET_CODE_ACTION)) { Uri uri = intent.getData(); System.out.println("SecretLaunchReceiver onReceive: " + uri); if(uri != null){ String content = uri.getSchemeSpecificPart().substring(2); if("0755".equals(content)){ Intent i = new Intent(context, ModeChooserActivity.class); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i); } } } } }
<receiver android:name="com.xxx.ftest.SecretLaunchReceiver" > <intent-filter> <action android:name="android.provider.Telephony.SECRET_CODE" /> <data android:scheme="android_secret_code" /> </intent-filter> </receiver>
OK,打完收工,这样只要在拨号中输入 *#*#0755#*#*就可以启动自己的程序了。
原文地址:http://blog.csdn.net/ville_zeng/article/details/19496569, 欢迎转载!