设备重启后启动 Phonegap 插件

Initiating a Phonegap plugin after device restart

我正在为 Android 开发混合 Phonegap 应用程序。该应用程序仅使用我正在开发的一个插件。该插件做了三件事

我已经实现了让应用程序调整到设备重启的代码,但结果很容易(感谢我在 SO 上的其他线程中找到的信息)

package com.example.plugin;

import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaWebView;
import android.content.Context;
import android.content.BroadcastReceiver;
import android.content.pm.PackageManager;
import android.app.Activity;

import android.content.Intent;

public class Rebooter extends BroadcastReceiver
{
 @Override
 public void onReceive(Context context, Intent intent) 
 {
  Intent i = new Intent(context, MyAppCordovaPlugin.class); 
  i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  context.startActivity(i);  
 }
}

我这样注册重启接收器

<receiver android:enabled="true" android:name=".Rebooter" 
 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>

MyAppCordovaPlugin 是我的 app/plugin 的入口点 - 扩展 CordovaPlugin class 的入口点。这是我在那里所做的

public class MyAppCordovaPlugin extends CordovaPlugin
{
 private Context context;

 public void initialize(CordovaInterface cordova, CordovaWebView webView) 
 {
  super.initialize(cordova, webView);
  this.context = cordova.getActivity().getApplicationContext();
  //setup pushy.me broadcast receiver
  //setup geolocation changes receiver
  //setup broadcast receiver for a half-hourly alarm
 } 

 @Override
 public void onResume(boolean multitasking) 
 {
  super.onResume(multitasking);
  //unregister background location change receiver, if present
  //switch geolocation to foreground mode. i.e. using
  //FusedLocationApi.requestLocationUpdates
 }

 @Override
 public void onPause(boolean multitasking) 
 {
  super.onPause(multitasking);
  //stop request for foreground location updates, if present
  //switch geolocation to background mode, i.e by
  //registering a broadcast receiver that listens for location change 
  //broadcasts
 } 

当我在 Android 4.4.2 测试设备上手动启动应用程序时,一切正常。即

当我检查 运行 应用程序时,我发现它包含一项服务 PushySocketService 和标记为正在使用的主进程 com.example.app。内存占用相当可观。

当我重新启动 phone 时,我仍然找到与 "main process" 运行 相同的服务。但是,报告的主进程内存使用率明显较低。

最重要的是 - 该应用程序不接收推送消息,也不响应地理位置变化。这只会在我通过 main activity.

启动应用程序后才开始发生

我一定是遗漏了什么 - 所以重新启动的应用程序不会自动启动它的 main activity?如果是这样,我的 Rebooter.onReceive 代码一定有问题吗?

为了完整起见,我应该提到

我这里显然做错了什么。如果有人能帮助我走上正轨,我将不胜感激。

如果您不想为此功能使用任何第三方插件,那么您可以借用 cordova auto start plugin 的逻辑。

您可以查看 BootCompletedReceiver class in the plugin. It invokes everytime when the device reboots successfully which in turn invokes AppStarter 帮助程序 class 以启动相应的应用程序。您也可以在您的插件中实现相同的逻辑。

希望对您有所帮助。干杯。

Cordova 插件

我们来看这段代码:

Intent i = new Intent(context, MyAppCordovaPlugin.class); 
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
  • CordovaPlugin 不是 activity,因此您无法启动它。
  • Cordova Android 应用程序是带有 WebView 的单个 Activity 应用程序,仅在创建 WebView 后才加载插件。您真的不想启动应用程序/启动应用程序 UI 以收听 GCM 消息或地理围栏事件。

Pushy.me

Pushy.me documentation 建议在应用的清单中声明 BroadcastReceiver。似乎 Pushy 也为您处理启动事件。为什么不使用他们推荐的方法?

运行 启动后后台的东西

如上所述,initialize() 回调中的代码将运行 只有在应用程序的activity 已启动时。但是您想在 boot-complete 事件后在后台调用一些代码。

我建议将 'start listening to background events' 逻辑移动到 IntentService,您可以从插件的 initialize() 和启动完成接收器启动。