Skip to content
This repository has been archived by the owner on Aug 18, 2019. It is now read-only.

Commit

Permalink
Init commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
darkgeek committed Dec 4, 2014
1 parent 24b7102 commit 81c2a72
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
Phonegap-BackHome-Plugin
========================

A cordova/phonegap plugin to send your app to background by starting Home activity
## What's this?

It's a cordova/phonegap plugin to send your app to background by starting Home activity. Tested on `cordova` 4.1.2 .

## Installation

cordova plugin add https://github.com/darkgeek/Phonegap-BackHome-Plugin.git

## Usage

This plugin provides one method called `goHome(successCallback, errorCallback)` to start Home activity.

window.BackHome.goHome(successCallback, errorCallback);

## History

Originally developed by [Dpa99c](http://stackoverflow.com/users/777265/dpa99c) in [stackoverflow](http://stackoverflow.com/questions/17826122/send-application-to-background-mode-when-back-button-is-pressed-in-phonegap/17828434#17828434), and updated by me to make it work with `cordova` 4.0 and later.
18 changes: 18 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<plugin id="edu.hdu.darkgeek.cordova.backhome" version="0.0.1"
xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android">
<name>BackHome</name>
<description>Start the home activity to send your app to background</description>
<js-module name="BackHome" src="www/BackHome.js">
<clobbers target="cordova.plugins.BackHome"/>
</js-module>
<platform name="android">
<config-file parent="/*" target="res/xml/config.xml">
<feature name="BackHome">
<param name="android-package" value="edu.hdu.darkgeek.cordova.backhome.BackHome"/>
</feature>
</config-file>
<source-file src="src/android/BackHome.java" target-dir="src/edu/hdu/darkgeek/cordova/backhome/"/>
</platform>
</plugin>
41 changes: 41 additions & 0 deletions src/android/BackHome.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package edu.hdu.darkgeek.cordova.backhome;

import org.json.JSONArray;
import org.json.JSONException;

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

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;

/**
* A phonegap plugin to send your app to background by starting home activity.
* Adapted from http://stackoverflow.com/questions/17826122/send-application-to-background-mode-when-back-button-is-pressed-in-phonegap
*
* @Author http://stackoverflow.com/users/777265/dpa99c (Original version, Thanks!)
* @Author https://github.com/darkgeek (This version, with a bit tweeks for cordova 4.1.2)
*/
public class BackHome extends CordovaPlugin {

private static final String LOG_TAG = "BackHomePlugin";

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
if ("goHome".equals(action)) {
try {
Intent i = new Intent(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
this.cordova.getActivity().startActivity(i);

} catch (Exception e) {
Log.e(LOG_TAG, "Exception occurred: ".concat(e.getMessage()));
return false;
}
callbackContext.success();
return true;
}
Log.e(LOG_TAG, "Called invalid action: "+action);
return false;
}
}
7 changes: 7 additions & 0 deletions www/BackHome.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
window.BackHome = {
goHome: function(success, error) {
cordova.exec(success, error, "BackHome", "goHome", []);
},
}

module.exports = BackHome;

0 comments on commit 81c2a72

Please sign in to comment.