在Android Phonegap中手动暂停应用程序

时间:2012-05-11 10:14:05

标签: android cordova

是否可以手动暂停Android PhoneGap应用程序?当有人点击按钮时,我需要暂停应用程序并转到后台。我使用navigator.app.exitApp();但它完全关闭了应用程序。我不想像卸载本机后退按钮一样关闭应用程序。请帮助,谢谢。

2 个答案:

答案 0 :(得分:3)

这是一个类似于Joram Teusink的解决方案的解决方案,但更简单,因为您不需要手动编辑Java代码 - 只需使用phonegap / cordova CLI安装插件即可。

插件一个函数:goHome()。如果你打电话给这个,应用程序将暂停 - 就像按下主页按钮一样。

用法:

navigator.Backbutton.goHome(function() {
  console.log('success - the app will now pause')
}, function() {
  console.log('fail')
});

安装:

phonegap本地插件添加https://github.com/mohamed-salah/phonegap-backbutton-plugin.git

这是github页面:

https://github.com/mohamed-salah/phonegap-backbutton-plugin.git

答案 1 :(得分:1)

在您的Javascript中:

// HomeButton
cordova.define("cordova/plugin/homebutton", function (require, exports, module) {
    var exec = require("cordova/exec");
    module.exports = {
        show: function (win, fail) {
            exec(win, fail, "HomeButton", "show", []);
        }
    };
});

// HomeButton
function homeButton() {
    var home = cordova.require("cordova/plugin/homebutton");
    home.show(
        function () {
            console.info("PhoneGap Plugin: HomeButton: callback success");
        },
        function () {
            console.error("PhoneGap Plugin: HomeButton: callback error");
        }
    );
}

在Android Native上的Java中:

package org.apache.cordova.plugins;

import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.apache.cordova.api.PluginResult;
import org.json.JSONArray;

import android.content.Intent;
import android.util.Log;

public class HomeButton extends CordovaPlugin {

    public static final String LOG_PROV = "PhoneGapLog";
    public static final String LOG_NAME = "HomeButton Plugin";

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
        Log.d(LOG_PROV, LOG_NAME + ": Simulated home button.");
        Intent i = new Intent(Intent.ACTION_MAIN);
        i.addCategory(Intent.CATEGORY_HOME);
        this.cordova.startActivityForResult(this, i, 0);
        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK));
        return true;
    }

}

用以下方式调用:

homeButton();

它有效,并且是我的回购的一部分:https://github.com/teusinkorg/jpHolo/