设备重启后启动Phonegap插件

时间:2017-01-17 04:55:01

标签: java android cordova geolocation reboot

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

  • 观看地理位置变化(前景和背景)
  • 设置半小时警报以执行某些定期任务
  • 收听推送消息。我使用pushy.me服务,我使用的代码遵循their documentation

我已经实现了代码以使应用程序调整到设备重新启动时有些惶恐,但结果很简单(感谢我在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是我的应用/插件的入口点 - 扩展CordovaPlugin类的入口点。这就是我在那里做的事情

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测试设备上手动启动应用程序时,一切都运行良好。即。

  • 检测到地理位置更改:前景和后台
  • 收到推送消息。再次以f / g和b / g
  • 表示
  • 半小时报警

当我检查正在运行的应用程序时,我发现它包含一个服务PushySocketService和主要进程com.example.app,它被标记为正在使用中。内存使用量很大。

当我重新启动手机时,我仍然可以找到相同的服务和&#34;主要流程&#34;运行。但是,主进程报告的内存使用量明显较低。

最重要的是 - 该应用不会收到推送消息,也不会响应地理位置更改。这只是在我main activity启动应用后才开始发生的。

我必须在这里遗漏一些东西 - 所以重新启动的应用程序不会自动启动它的main activity?如果是这样,我的Rebooter.onReceive代码一定有问题吗?

为了完整性,我应该提到

  • 只在plugin.xml文件中静态声明了Pushy.me和Rebooter广播接收器。 Geo位置和警报广播接收器是从我的插件代码中动态注册的。
  • 我正在使用Phonegap CLI v 6.4.2和JDK 7
  • 构建应用程序

我在这里显然做错了。我非常感谢任何能够让我走上正轨的人。

2 个答案:

答案 0 :(得分:3)

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

您可以查看插件中的BootCompletedReceiver类。每次设备重新启动成功时都会调用它,然后调用AppStarter帮助程序类来启动相应的应用程序。您也可以在插件中实现相同的逻辑。

希望它有所帮助。欢呼声。

答案 1 :(得分:1)

Cordova Plugin

让我们看一下这段代码:

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

Pushy.me

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

启动后在后台运行东西

如上所述,initialize()回调中的代码仅在应用程序的活动开始时才会运行。但是你想在boot-complete事件之后在后台调用一些代码。

我建议的是开始聆听背景事件&#39; IntentService的逻辑,您可以从插件的initialize()和启动完成接收器启动。