Initiating a Phonegap plugin after device restart

天大地大妈咪最大 提交于 2019-12-04 03:19:26

If you dont want to use any third party plugin for this functionality, then you can borrow the logic from cordova auto start plugin.

You can have a look at BootCompletedReceiver class in the plugin. It invokes everytime when the device reboots successfully which in turn invokes AppStarter helper class to start the respective app. You can implement the same logic in your plugin too.

Hope it helps. Cheers.

Cordova Plugin

Let's look at this piece of code:

Intent i = new Intent(context, MyAppCordovaPlugin.class); 
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
  • CordovaPlugin is not an activity, so you cannot start it as if it were.
  • Cordova Android application are is a single Activity application with a WebView, which loads the plugins only after the WebView is created. You don't really want to launch the application / start app UI in order to listen to GCM messages or geofencing events.

Pushy.me

Pushy.me documentation recommend to declare the BroadcastReceiver in the app's manifest. Seems also that Pushy also take care of boot event for you. Why not use their recommended approach?

Running stuff in the background after boot

As mentioned above, the code in initialize() callback will run only when the application's activity has been started. But you want to invoke some of the code there in the background after boot-complete event.

What I recommend is moving the 'start listening to background events' logic to an IntentService, which you can start both from your plugin's initialize(), and your boot complete receiver.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!