学习cordova-plugin-autostart插件经验

1078

cordova-plugin-autostart项目地址:

https://github.com/ToniKorin/cordova-plugin-autostart

一开始没细看readme,直接用enable(),自启动是生效了,但打开的是app。

如果是开发一些基于android专用设备,用这种自启动方式很有效。如,银行排号机、自动售货机、早教机等等,可以开机直接启动,再加了无法退出,就可以满足多种使用场景了。

我只想启动后就在后台运行,不让用户察觉,尝试了多种方案均无法达到理想效果。

 

遂决定先读懂插件,尝试自己写。

下面这篇文章写的很好,给出了多种自启动并保持方案:

https://blog.csdn.net/crissjs/article/details/38293441

阅读源码过程中,发现了autostart插件中居然还有个enableService()函数。由于我使用的ionic,其

@ionic-native/autostart 中并没有提供enableService()接口,这个很坑。
原来插件本身是可以自启动服务的,但怎么用还是个问题,readme没有写明,还是要自己阅读源代码才行。

分析程序从main入手,分析插件也是如此,先找到其入口AutoStart.java。其中主要是使能了启动的信号接收器,没毛病。

再看config.xml,其中有其第二入口,这里第二入口自启动的入口:

        <config-file target="AndroidManifest.xml" parent="/manifest/application">
            <receiver
                android:name="com.tonikorin.cordova.plugin.autostart.BootCompletedReceiver" android:enabled="false">
                <intent-filter>
                    <action android:name="android.intent.action.BOOT_COMPLETED" />
                </intent-filter>
            </receiver>
            <receiver
                android:name="com.tonikorin.cordova.plugin.autostart.UserPresentReceiver" android:enabled="false">
                <intent-filter>
                    <action android:name="android.intent.action.USER_PRESENT" />
                </intent-filter>
            </receiver>
            <receiver
                android:name="com.tonikorin.cordova.plugin.autostart.PackageReplacedReceiver" android:enabled="true">
                <intent-filter>
                    <action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
                </intent-filter>
            </receiver>
        </config-file>

其plugin中注册了几个receiver,分别是:

android.intent.action.BOOT_COMPLETED//启动完成
android.intent.action.USER_PRESENT//解锁屏幕
android.intent.action.MY_PACKAGE_REPLACED//包更新

这些动作可以触发对应函数执行

具体怎么调用的可以看cordova文档,也不深究了。

启动完成后执行的文件实际上就是BootCompletedReceiver.java中的:

AppStarter appStarter = new AppStarter();
appStarter.run(context, intent, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, true);

再看appStarter.java,看注释就可以找到最后启动服务的代码。

其中使用到IntentService,关于IntentService可见:

https://blog.csdn.net/p106786860/article/details/17885115

这篇文章开头就很清晰的说了IntentService和普通服务的区别。

所以关键代码:

            Intent serviceIntent = new Intent();
            serviceIntent.setClassName(context, serviceClassName);
            if ( onAutostart ) {
                serviceIntent.putExtra(CORDOVA_AUTOSTART, true);
            }
            context.startService(serviceIntent);

参数context为当前包名,serviceClassName为要启动的服务类名。

真相大白了,cordova-plugin-autostart可以启动服务,只要指定服务类名即可,当然你要另外写服务,这个插件里没法写。
现在的问题核心落到了,如何写一个android原生服务上了。
也许可用的资源:
https://github.com/Red-Folder/bgs-core

2 意见

  1. 你好,这个插件是开机就会启动app吗?

    cordova.plugins.autoStart.enable(); 这代码是卸载什么地方呢?

    是这样吗:

    document.addEventListener(“deviceready”, function () {
    cordova.plugins.autoStart.enable();
    }, false);

留下一个答复

Please enter your comment!
Please enter your name here