场景: vue写的H5网页, cordova把它打包成Android的APK, 现在要给它配置开机自启动功能
1. 给config.xml增加<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
原本的config.xml
<?xml version='1.0' encoding='utf-8'?>
<widget
id="io.cordova.hellocordova"
version="4.8.6"
xmlns:android="http://schemas.android.com/apk/res/android"
android-versionCode="106"
xmlns="http://www.w3.org/ns/widgets"
>
<name>xx</name>
<description>
xxx有限公司提供技术支持
</description>
<author email="support@xx.com" href="https://www.xxx.com">
xxx Team
</author>
<content src="index.html" />
<plugin name="cordova-plugin-whitelist" spec="1" />
<access origin="*" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
<allow-intent href="tel:*" />
<allow-intent href="sms:*" />
<allow-intent href="mailto:*" />
<allow-intent href="geo:*" />
<platform name="android">
<allow-intent href="market:*" />
<config-file target="AndroidManifest.xml" parent="/*">
<uses-permission android:name="android.permission.MANAGE_USB" />
</config-file>
</platform>
<platform name="ios">
<allow-intent href="itms:*" />
<allow-intent href="itms-apps:*" />
</platform>
</widget>
参考这个: https://stackoverflow.com/questions/1056570/how-to-auto-start-an-android-application
和这个: https://blog.csdn.net/u012611644/article/details/80542119
$ cat config.xml
<?xml version='1.0' encoding='utf-8'?>
<widget
id="io.cordova.hellocordova"
version="4.8.6"
xmlns:android="http://schemas.android.com/apk/res/android"
android-versionCode="106"
xmlns="http://www.w3.org/ns/widgets"
>
<name>xxx</name>
<description>
xxx有限公司提供技术支持
</description>
<author email="support@xxx.com" href="https://www.xxx.com">
xxx Team
</author>
<content src="index.html" />
<plugin name="cordova-plugin-whitelist" spec="1" />
<access origin="*" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
<allow-intent href="tel:*" />
<allow-intent href="sms:*" />
<allow-intent href="mailto:*" />
<allow-intent href="geo:*" />
<platform name="android">
<allow-intent href="market:*" />
<config-file target="AndroidManifest.xml" parent="/*">
<uses-permission android:name="android.permission.MANAGE_USB" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
</config-file>
</platform>
<platform name="ios">
<allow-intent href="itms:*" />
<allow-intent href="itms-apps:*" />
</platform>
</widget>
PS: 只新增了第30行
2. 给AndroidManifest.xml增加receiver
$ cd platforms/android/app/src/main/
$ cat AndroidManifest.xml
<?xml version='1.0' encoding='utf-8'?>
<manifest android:hardwareAccelerated="true" android:versionCode="106" android:versionName="4.8.6" package="io.cordova.hellocordova" xmlns:android="http://schemas.android.com/apk/res/android">
<supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
<uses-permission android:name="android.permission.INTERNET" />
<application android:hardwareAccelerated="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:largeHeap="true" android:supportsRtl="true">
<activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|smallestScreenSize|screenLayout|uiMode" android:label="@string/activity_name" android:launchMode="singleTop" android:name="MainActivity" android:theme="@android:style/Theme.DeviceDefault.NoActionBar" android:windowSoftInputMode="adjustResize">
<intent-filter android:label="@string/launcher_name">
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:enabled="true" android:name="BootUpReceiver" android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
<uses-feature android:name="android.hardware.usb.host" android:required="true" />
<uses-permission android:name="android.permission.SHUTDOWN" />
<uses-permission android:name="android.permission.MANAGE_USB" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
</manifest>
其中<receiver ...></receiver>标签(第12-16行)为新增的内容
3. 新建BootUpReceiver.java文件
$ touch BootUpReceiver.java
$ vi BootUpReceiver.java
$ cat BootUpReceiver.java
package io.cordova.hellocordova;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import org.apache.cordova.*;
public class BootUpReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
附: MainActivity.java文件内容(该文件无需修改)
$ cat MainActivity.java
package io.cordova.hellocordova;
import android.os.Bundle;
import org.apache.cordova.*;
public class MainActivity extends CordovaActivity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// enable Cordova apps to be started in the background
Bundle extras = getIntent().getExtras();
if (extras != null && extras.getBoolean("cdvStartInBackground", false)) {
moveTaskToBack(true);
}
// Set by <content src="index.html" /> in config.xml
loadUrl(launchUrl);
}
}
- cordova build打包
$ cd -&&cd ../../../../.. # cd到config.xml所在目录
$ cordova build
$ cp platforms/android/app/build/outputs/apk/debug/app-debug.apk ~/auto-startup-xxx.apk
- 安装
直接用U盘把apk拷贝到机器上, 然后双击打开即可