cordova配置app开机自启动---测试通过于Android10.1.2
本方法适用于Android PDA开发,通常Android PDA支持开放所用权限给使用者,因此PDA不会限制App开机自动运行。
-
修改config.xml,添加监听开机启动的权限
<platform name="android"> <config-file target="AndroidManifest.xml" parent="/*"> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> </config-file> </platform>
2.修改platforms/android/app/src/main/AndroidManifest.xml,添加广播接收器
<application ... ... <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>
3.新建platforms/android/app/src/main/java/io/cordova/hellocordova/BootUpReceiver.java文件,处理开机响应事件
其中红色部分根据不同app的包名而不同,要求与包名一致。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);
}
}
注意,务必修改包名(io.cordova.hellocordova)为你自己的包名,否则会提示编译不通过。
4.最后直接编译即可
cordova build android --release
posted on 2022-08-14 02:10 zhangzongshan 阅读(413) 评论(1) 收藏 举报
浙公网安备 33010602011771号