2012-05-11 14 views
5

是否可以手動暫停Android PhoneGap應用程序?當有人點擊一個按鈕時,我需要暫停應用程序並轉到後臺。我使用navigator.app.exitApp();,但它完全關閉了應用程序。我不想關閉應用程序,就像使用本地後退按鈕一樣卸載應用程序。請幫忙,謝謝。在Android手動暫停應用程序Phonepotgap

+0

你是什麼意思與暫停?也許看看Activites Lifecircle的原因我不知道你試圖達到什麼:) http://developer.android.com/guide/topics/fundamentals/activities.html – yoshi

+0

我需要暫停/停止活動(該應用程序不應該在forground中可見),應用程序不應該像我們點擊本地後退按鈕時那樣可見,它會隱藏應用程序。 –

+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"); 
     } 
    ); 
} 

在Java上的Android原生:

homeButton(); 

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; 
    } 

} 

與說它

它的工作原理,是我的一個回購協議的一部分:https://github.com/teusinkorg/jpHolo/

+0

謝謝,它像魔術一樣工作 –

3

這裏有一個解決方案,它類似於蘭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