设备重新启动后启动一个Phonegap插件 [英] Initiating a Phonegap plugin after device restart

查看:139
本文介绍了设备重新启动后启动一个Phonegap插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发面向Android的混合Phonegap应用程序。该应用只使用我正在开发的一个插件。插件做了三件事:
$ b


  • 观看地理位置变化(前景和背景)

  • 设置半小时报警以执行某些定期任务

  • 侦听推送消息。我使用 pushy.me 服务,我使用的代码如下他们的文档


我已经实现了代码以使应用程序调入设备重启但是事实证明这很容易(感谢我在SO的其他主题中发现的信息)

  package com.example.plugin ; 

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

导入android.content.Intent;

public class Rebooter extends BroadcastReceiver
{
@Override
public void onReceive(Context context,Intent intent)
{
Intent i =新的意图(上下文,MyAppCordovaPlugin.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);


我注册了重新启动接收器,因此

 < receiver android:enabled =trueandroid:name =。Rebooter
android:permission =android.permission。项值>
< intent-filter>
< category android:name =android.intent.category.DEFAULT/>
< / intent-filter>
< / receiver>

MyAppCordovaPlugin 是我的应用的入口点/ plugin - 扩展 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();
//设置pushy.me广播接收器
//设置地理定位更改接收器
//设置广播接收器用于半小时报警
}

@Override
public void onResume(boolean multitasking)
{
super.onResume(multitasking);
//取消注册背景位置更改接收器(如果存在)
//将地理位置切换为前景模式。即使用
//FusedLocationApi.requestLocationUpdates
}
$ b $ @Override
public void onPause(boolean multitasking)
{
super.onPause(多任务);
//停止前台位置更新请求(如果存在)
//将地理位置切换到后台模式,即
//注册监听位置更改的广播接收器
//广播
}

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


  • 检测到地理位置变化:无论是在前台还是后台
  • 收到。再次以f / g和b / g计算

  • 半小时报警有效>当我检查正在运行的应用程序时,我发现它包含一个服务 PushySocketService 和主进程 com.example.app 标记为正在使用中。内存使用情况相当可观。



    当我重新启动手机时,我仍然发现运行相同的服务和主进程。但是,为主进程报告的内存使用情况要低得多。



    最重要的是 - 应用程序不会收到推送消息,也不会响应地理位置更改。这只会在我通过主要活动启动应用后才会发生。



    我必须在这里丢失一些东西 - 所以重新启动的应用程序不会自动启动它的主要活动?如果是这样的话,我的 Rebooter.onReceive 代码必须有问题吗?为了完整性,我应该提及




    • 只有Pushy.me和Rebooter广播接收器在plugin.xml文件中静态声明。地理定位和警报广播接收器是从我的插件代码中动态注册的。

    • 我使用Phonegap CLI v 6.4.2和JDK 7构建应用程序



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

    解决方案

    如果你不想使用任何第三方插件来实现此功能,那么您可以借用 cordova auto start插件< a>。



    你可以看看 BootCompletedReceiver 类。它调用每次当该装置成功地重新启动,其依次调用 AppStarter 助手类来启动相应的应用程序。你也可以在你的插件中实现相同的逻辑。



    希望它有帮助。干杯。


    I am in the process of developing a hybrid Phonegap app for Android. The app uses just the one plugin which I am developing as well. The plugin does three things

    • Watches for geo-location changes (both foreground and background)
    • Sets up a half-hourly alarm to perform certain periodic tasks
    • Listens for push messages. I use the pushy.me service and the code I use follows their documentation.

    I had implemented the code to get the application to tune in to device reboots with some trepidation but it turned out to be easy (thanks to information I found in other threads on 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);  
     }
    }
    

    I register the reboot receiver thus

    <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 is the entry point to my app/plugin - the one that extends the CordovaPlugin class. Here is what I do there

    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
     } 
    

    When I start up the app manually on my Android 4.4.2 test device everything works perfectly. i.e.

    • Geo location changes are detected:both in the foreground and in the background
    • push messages are received. Once again both in f/g and in b/g
    • The half hourly alarm works

    When I examine the running app I find that it consists of one service PushySocketService and the main process, com.example.app which is marked as being in use. Memory usage is considerable.

    When I restart the phone I still find the same service and "main process" running. However, the memory usage reported for the main process is significantly lower.

    Most importantly - the app does not recieve push messages and does not respond to geo location changes. This only starts happening after I have launched the app by main activity.

    I must be missing something here - so the rebooted app does not automatically start its main activity? If so there must be something wrong with my Rebooter.onReceive code?

    For completeness I should mention

    • Only the Pushy.me and Rebooter broadcast receivers are declared statically in the plugin.xml file. The Geo location and alarm broadcast receivers are registered dynamically from within my plugin code.
    • I am building the app using Phonegap CLI v 6.4.2 and JDK 7

    I am clearly doing something wrong here. I'd be most grateful to anyone who might be able to put me on the right track.

    解决方案

    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.

    这篇关于设备重新启动后启动一个Phonegap插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆