原文地址:https://blog.csdn.net/zhenbohuang/article/details/76138790
手机上通常都有一些暗码来启动一些隐藏的功能。最常见的就是在拨号盘输入*#06#来查看imei号。
那么自己开发的应用是否也能通过这样的方式来启动呢?刚好最近一个项目刚好有这个需求,网上查了下资料。
答案是可以的,方法也很简单。
1:注册一个receiver, action支持android.provider.Telephony.SECRET_CODE,并写入你的启动暗码,如下
<receiver
android:name=".App.SecretCodeReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.provider.Telephony.SECRET_CODE" />
<data android:scheme="android_secret_code" android:host="5858" />
</intent-filter>
</receiver>
2:实现这个 receiver,并在onReceive做你想要的操作,比如启动一个activity之类的
package utils.bobo.com.boboutils.App;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import utils.bobo.com.boboutils.DemosActivity;
public class SecretCodeReceiver extends BroadcastReceiver {
public SecretCodeReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
Intent activityIntent = new Intent(context, DemosActivity.class);
activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(activityIntent);
}
}
然后在拨号盘输入*#*#5858#*#*,就能启动对应的activity了
结束,完整代码请访问
https://github.com/bobohuang1985/android-utils-api
也可以注册receiver 时不写host,然后在receiver实现里执行host值的判断
<receiver
android:name=".App.SecretCodeReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.provider.Telephony.SECRET_CODE" />
<data android:scheme="android_secret_code" />
</intent-filter>
</receiver>
package utils.bobo.com.boboutils.App;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import utils.bobo.com.boboutils.DemosActivity;
public class SecretCodeReceiver extends BroadcastReceiver {
public SecretCodeReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
String intentAction = intent.getAction();
if(intentAction.equals("android.provider.Telephony.SECRET_CODE")){
String number = intent.getData().getHost();
if (number.equals("5858") {
Intent activityIntent = new Intent(context, DemosActivity.class);
activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(activityIntent);
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)