gpt4 book ai didi

java - 设备重启后启动 Phonegap 插件

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:10:47 26 4
gpt4 key购买 nike

我正在为 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。内存使用量很大。

当我重新启动手机时,我仍然发现相同的服务和“主进程”在运行。但是,报告的主进程内存使用率明显较低。

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

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

为了完整起见,我应该提一下

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

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

最佳答案

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

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

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

关于java - 设备重启后启动 Phonegap 插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41689316/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com