近日,写了一个有关设备信息的小应用,要求在launcher中不可显,通过拨号键盘暗码启动该应用。现将心得记录如下:
软件平台:Android4.4.3
首先,要在AndroidManifest.xml中注册接收android暗码广播。
<span style="font-size:18px;"><receiver android:name="InfoSecretCode" >
<intent-filter>
<action android:name="android.provider.Telephony.SECRET_CODE" />
<data
android:host="668866"
android:scheme="android_secret_code" />
</intent-filter>
</receiver></span>
其次,需要实例化对接收广播事件的处理,取名为InfoSecretCode.java,代码如下:
<span style="font-size:18px;">import android.content.Context;
import android.content.Intent;
import android.content.BroadcastReceiver;
import android.util.Config;
import android.util.Log;
import android.view.KeyEvent;
public class InfoSecretCode extends BroadcastReceiver {
private final String SECRET_CODE_ACTION = "android.provider.Telephony.SECRET_CODE";
public InfoSecretCode() {
}
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(SECRET_CODE_ACTION)) {
Intent i = new Intent(Intent.ACTION_MAIN);
i.setClass(context, InfoActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}</span>
如此,在拨号键盘输入“*#*#668866#*#*”即可弹出InfoActivity了,具体activity自己添加。