至少在API=8,android还是支持无界面app的,比如一个app只有一个receiver,实现特定的功能;但到至多android 4.0已经默认不支持这样只有一个receiver的app了,即app必须具有activity,至于系统内置的无activity的app如何配置的,我还不清楚,但大部分情况下,android应该不允许无activity视图的app的receiver运行的,像接下来要介绍的秘密app(通常用于工厂指令调试),在android 4.0及以上平台是无法工作的;这类外部app用户通常看不到入口图标,就有可能成为病毒app之类的,为了安全,还是需要一个activity让用户看看的。
刚发现在<receiver>中添加<category android:name="android.intent.category.HOME" />可以在高版本API,实现无界面(activity)启动receiver
如:
<receiver
android:name=".AlarmReceiver"
android:exported="false" >
<intent-filter>
<action android:name="android.alarm.action.send" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.TIME_SET" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
言归正传,仅以下代码生成的app在android 4.0无法工作,app需要至少一个activity,或者如上添加
<category
>,而在android2.2可以工作。
1.声明在menifest.xml里面声明receiver:
<receiver android:name=".startSettings">
<intent-filter>
<action android:name="android.provider.Telephony.SECRET_CODE" />
<data android:host="0556" android:scheme="android_secret_code" />
<data android:host="0557" android:scheme="android_secret_code" /> //可以有多个data
</intent-filter>
</receiver>
2.java 代码
public class startSettings extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String host;
if (intent.getData() != null) {
host = intent.getData().getHost();
} else return;
if (host.equals("0556")) {
//TODO,启动一个程序,如下启动RootExplorer
Intent it = new Intent("android.intent.action.MAIN");
it.setClassName("com.speedsoftware.rootexplorer","com.speedsoftware.rootexplorer.RootExplorer");
context.startActivity(it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));//必须要FLAG_ACTIVITY_NEW_TASK
} else {
//TODO
}
}
}
3.键盘拨号启动:*#*#0557#*#* , 0556就是menifest.xml里面的 android:host="0556"