您的位置:首页 > 移动开发 > Android开发

android中实现暗码启动apk【一】

2017-02-15 15:49 851 查看
相信大家一定接触过手机里面的各种暗码启动app,最熟悉的就是在拨号盘输入”*#06#” 查看手机的IMEI号。

如果我们也想实现一样的功能,自定义暗码启动我们自己的app,该如何实现呢,其实去网上搜索暗码启动apk,有很多相关的栗子,也说的很清楚,我在此记录下我实现此功能的方法。

我实现此功能是在MTK平台上实现的。所以是直接把代码加到源码中编译。

直接贴代码:

SecretSwupReceiver.java

import android.content.ActivityNotFoundException;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;

public class SecretSwupReceiver extends BroadcastReceiver{

public static String SECRET_CODE_ACTION = "android.provider.Telephony.SECRET_CODE";
private final Uri mSwupUri= Uri.parse("android_secret_code://666666");
private static final String TAG = "SecretSwupReceiver";

@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction() == null) {
Log.i(TAG, "Null action");
return;
}
if (intent.getAction().equals(SECRET_CODE_ACTION)) {
Uri uri = intent.getData();
Log.i(TAG, "getIntent success in if");
if (uri.equals(mSwupUri)) {
***Intent mIntent = new Intent(context, MainActivity.class);***
mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
context.startActivity(mIntent);
} catch (ActivityNotFoundException e) {
Log.i(TAG, "startActivity() failed:"+e);
}
}
}

}
}


AndroidMainfest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testbg"
android:versionCode="1"
android:versionName="1.0" >

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.usb.host" />
<uses-permission android:name="android.permission.USB_PERMISSION" />

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.***DEFAULT***" />
</intent-filter>
</activity>

***<receiver
android:name=".SecretSwupReceiver"
android:exported="true" >
<intent-filter>
<action android:name="android.provider.Telephony.SECRET_CODE" />
<data
android:host="666666"
android:scheme="android_secret_code" />
</intent-filter>
</receiver>***
</application>
</manifest>


我只贴了

SecretSwupReceiver.java 和AndroidMainfest.xml的代码,因为关键代码就这两个文件,其他的就不贴了。注意文中加粗红色标注的地方。LAUNCHER需要改成

DEFAULT这样可以隐藏图标。

代码写好了就直接编译一下,编译成功后,在拨号盘输入你自定义的暗码,我定义的“666666”,输入“*#*#666666#*#*”就可以启动apk了。

更多详细内容可参考博客:http://blog.csdn.net/easy_gemini/article/details/8230869

http://blog.csdn.net/qq3162380/article/details/45393335
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 暗码启动APK