NDK-baozi / blog_20140228_10_8801476

来自CSDN博客:android phonegap插件开发方法 plugin http://blog.csdn.net/aaawqqq/article/details/20401111#

  android phonegap 插件 plugins Cordova 最后更新时间 2014-03-06 22:50:29
blog_20140228_1_1736025 3行 Java
Raw
 1
 2
 3
phonegap create my-app
cd my-app
phonegap run android
blog_20140228_2_450052 58行 html/xml
Raw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
<!DOCTYPE html>
<html>
<head>
<title>Capture Photo</title>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">
var pictureSource; // picture source
var destinationType; // sets the format of returned value
// Wait for device API libraries to load //
document.addEventListener("deviceready",onDeviceReady,false);
// device APIs are available //
function onDeviceReady() { pictureSource=navigator.camera.PictureSourceType; destinationType=navigator.camera.DestinationType; }
// Called when a photo is successfully retrieved //
function onPhotoDataSuccess(imageData) {
// Uncomment to view the base64-encoded image data //
console.log(imageData);
// Get image handle //
var smallImage = document.getElementById('smallImage');
// Unhide image elements //
smallImage.style.display = 'block';
// Show the captured photo // The in-line CSS rules are used to resize the image //
smallImage.src = "data:image/jpeg;base64," + imageData; }
// Called when a photo is successfully retrieved //
function onPhotoURISuccess(imageURI) {
// Uncomment to view the image file URI //
console.log(imageURI);
// Get image handle //
var largeImage = document.getElementById('largeImage');
// Unhide image elements //
largeImage.style.display = 'block';
// Show the captured photo // The in-line CSS rules are used to resize the image //
largeImage.src = imageURI; }
// A button will call this function //
function capturePhoto() {
// Take picture using device camera and retrieve image as base64-encoded string
navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50, destinationType: destinationType.DATA_URL }); }
// A button will call this function //
function capturePhotoEdit() {
// Take picture using device camera, allow edit, and retrieve image as base64-encoded string
navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 20, allowEdit: true, destinationType: destinationType.DATA_URL }); }
// A button will call this function //
function getPhoto(source) {
// Retrieve image file location from specified source
navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50, destinationType: destinationType.FILE_URI, sourceType: source }); }
// Called if something bad happens. //
function onFail(message) { alert('Failed because: ' + message); }
</script>
</head>
<body>
<button onclick="capturePhoto();">Capture Photo</button> <br>
<button onclick="capturePhotoEdit();">Capture Editable Photo</button> <br>
<button onclick="getPhoto(pictureSource.PHOTOLIBRARY);">From Photo Library</button><br>
<button onclick="getPhoto(pictureSource.SAVEDPHOTOALBUM);">From Photo Album</button><br>
<img style="display:none;width:60px;height:60px;" id="smallImage" src="" />
<img style="display:none;" id="largeImage" src="" />

</body>
</html>
blog_20140228_2_5289779 3行 Java
Raw
 1
 2
 3
</div></div></div><p><img alt="" src="http://img.blog.csdn.net/20140228104121078?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYWFhd3FxcQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" /></p><p> </p><p> </p><p><span style="font-size:12px;"><2> 我们从命令行进入 到工程目录下的  plugins文件夹</span></p><p><span style="font-size:12px;"></span> </p><p><pre style="display: none;" class="java" name="code" snippet_file_name="blog_20140228_2_5289779" code_snippet_id="210200">cd my-app

cd plugins
blog_20140228_3_2574564 1行 Java
Raw
 1
cordova plugin add org.apache.cordova.camera
blog_20140228_4_9510684 1行 Java
Raw
 1
cordova platform add android
blog_20140228_5_7111113 1行 Java
Raw
 1
cordova build
blog_20140228_2_6291898 3行 html/xml
Raw
 1
 2
 3
phonegap create my-app
cd my-app
phonegap run android
blog_20140228_4_5147629 3行 html/xml
Raw
 1
 2
 3
cd my-app

cd plugins
blog_20140228_5_4907648 3行 html/xml
Raw
 1
 2
 3
cordova plugin add org.apache.cordova.dialogs

cordova plugin add org.apache.cordova.vibration
blog_20140228_7_8223481 1行 html/xml
Raw
 1
cordova platform add android
blog_20140228_9_2760031 1行 html/xml
Raw
 1
cordova build
blog_20140228_11_7504324 1行 html/xml
Raw
 1
(in app/res/xml/config.xml)
blog_20140228_12_6392292 1行 html/xml
Raw
 1
<feature name="Notification">
blog_20140228_13_6185331 1行 html/xml
Raw
 1
<param name="android-package" value="org.apache.cordova.dialogs.Notification" />
blog_20140228_14_8628329 1行 html/xml
Raw
 1
</feature>
blog_20140228_15_3404859 1行 html/xml
Raw
 1
<feature name="Vibration">
blog_20140228_16_7624981 1行 html/xml
Raw
 1
<param name="android-package" value="org.apache.cordova.vibration.Vibration" />
blog_20140228_17_7418021 1行 html/xml
Raw
 1
</feature>
blog_20140228_19_526111 1行 html/xml
Raw
 1
(in app/AndroidManifest.xml)
blog_20140228_20_526894 1行 html/xml
Raw
 1
<uses-permission android:name="android.permission.VIBRATE" />
blog_20140228_22_5063443 1行 html/xml
Raw
 1
<!DOCTYPE html>
blog_20140228_23_363361 1行 html/xml
Raw
 1
<html>
blog_20140228_24_3711431 1行 html/xml
Raw
 1
<head>
blog_20140228_25_6187449 1行 html/xml
Raw
 1
<title>Notification Example</title>
blog_20140228_26_1836031 1行 html/xml
Raw
 1
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
blog_20140228_27_8248763 1行 html/xml
Raw
 1
<script type="text/javascript" charset="utf-8"> // Wait for device API libraries to load //
blog_20140228_28_8565190 1行 html/xml
Raw
 1
document.addEventListener("deviceready", onDeviceReady, false); // device APIs are available //
blog_20140228_29_5293568 1行 html/xml
Raw
 1
function onDeviceReady() { // Empty } // Show a custom alert //
blog_20140228_30_9198045 1行 html/xml
Raw
 1
function showAlert() { navigator.notification.alert( 'You are the winner!', // message
blog_20140228_31_70881 1行 html/xml
Raw
 1
'Game Over',
blog_20140228_32_7879052 1行 html/xml
Raw
 1
// title
blog_20140228_33_7639072 1行 html/xml
Raw
 1
'Done'
blog_20140228_34_6003652 1行 html/xml
Raw
 1
// buttonName
blog_20140228_35_5796692 1行 html/xml
Raw
 1
); } // Beep three times //
blog_20140228_36_3048457 1行 html/xml
Raw
 1
function playBeep() { navigator.notification.beep(3); } // Vibrate for 2 seconds //
blog_20140228_37_856630 1行 html/xml
Raw
 1
function vibrate() { navigator.notification.vibrate(2000); }
blog_20140228_38_2601517 1行 html/xml
Raw
 1
</script>
blog_20140228_39_9886300 1行 html/xml
Raw
 1
</head>
blog_20140228_40_6299033 1行 html/xml
Raw
 1
<body>
blog_20140228_41_4140226 1行 html/xml
Raw
 1
<p><a href="#" onclick="showAlert(); return false;">Show Alert</a></p>
blog_20140228_42_8916755 1行 html/xml
Raw
 1
<p><a href="#" onclick="playBeep(); return false;">Play Beep</a></p>
blog_20140228_43_8676775 1行 html/xml
Raw
 1
<p><a href="#" onclick="vibrate(); return false;">Vibrate</a></p>
blog_20140228_44_1501458 1行 html/xml
Raw
 1
</body>
blog_20140228_45_5754599 1行 html/xml
Raw
 1
</html>
blog_20140228_1_2397817 3行 Java
Raw
 1
 2
 3
phonegap create my-app
cd my-app
phonegap run android
blog_20140228_3_9301699 1行 Java
Raw
 1
cd my-app cd plugins
blog_20140228_4_4601617 3行 Java
Raw
 1
 2
 3
cordova plugin add org.apache.cordova.dialogs cordova

plugin add org.apache.cordova.vibration
blog_20140228_6_8440838 1行 Java
Raw
 1
cordova platform add android
blog_20140228_8_9978764 1行 Java
Raw
 1
cordova build
blog_20140228_10_8801476 9行 Java
Raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
(in app/res/xml/config.xml)
<feature name="Notification">
<param name="android-package" value="org.apache.cordova.dialogs.Notification" />
</feature>
<feature name="Vibration">
<param name="android-package" value="org.apache.cordova.vibration.Vibration" />
</feature>
(in app/AndroidManifest.xml)
<uses-permission android:name="android.permission.VIBRATE" />
blog_20140303_2_863858 70行 JavaScript
Raw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
cordova.define('cordova/plugin_list', function(require, exports, module) {
module.exports = [
{
"file": "plugins/org.apache.cordova.camera/www/CameraConstants.js",
"id": "org.apache.cordova.camera.Camera",
"clobbers": [
"Camera"
]
},
{
"file": "plugins/org.apache.cordova.camera/www/CameraPopoverOptions.js",
"id": "org.apache.cordova.camera.CameraPopoverOptions",
"clobbers": [
"CameraPopoverOptions"
]
},
{
"file": "plugins/org.apache.cordova.camera/www/Camera.js",
"id": "org.apache.cordova.camera.camera",
"clobbers": [
"navigator.camera"
]
},
{
"file": "plugins/org.apache.cordova.camera/www/CameraPopoverHandle.js",
"id": "org.apache.cordova.camera.CameraPopoverHandle",
"clobbers": [
"CameraPopoverHandle"
]
},
{
"file": "plugins/org.apache.cordova.dialogs/www/notification.js",
"id": "org.apache.cordova.dialogs.notification",
"merges": [
"navigator.notification"
]
},
{
"file": "plugins/org.apache.cordova.dialogs/www/android/notification.js",
"id": "org.apache.cordova.dialogs.notification_android",
"merges": [
"navigator.notification"
]
},
{
"file": "plugins/org.apache.cordova.vibration/www/vibration.js",
"id": "org.apache.cordova.vibration.notification",
"merges": [
"navigator.notification"
]
},
{
"file": "plugins/intent.js",
"id": "org.apache.cordova.intent",
"merges": [
"navigator.intent"
]
},
];
module.exports.metadata =
// TOP OF METADATA
{
"org.apache.cordova.camera": "0.2.7",
"org.apache.cordova.dialogs": "0.2.6",
"org.apache.cordova.vibration": "0.3.7",
"org.apache.cordova.intent" :"0.0.1",

}
// BOTTOM OF METADATA
});
blog_20140303_3_2293100 7行 JavaScript
Raw
 1
 2
 3
 4
 5
 6
 7
{
"file": "plugins/intent.js",
"id": "org.apache.cordova.intent",
"merges": [
"navigator.intent"
]
},
blog_20140303_4_449937 24行 JavaScript
Raw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
cordova.define("org.apache.cordova.intent", function(require, exports, module) {

var exec = require('cordova/exec');



module.exports = {

/**
* 一共5个参数
第一个 :成功会掉
第二个 :失败回调
第三个 :将要调用的类的配置名字(在config.xml中配置 稍后在下面会讲解)
第四个 :调用的方法名(一个类里可能有多个方法 靠这个参数区分)
第五个 :传递的参数 以json的格式
*/
demo: function(mills) {
exec(function(winParam){
alert(winParam);
}, null, "Demo", "intent", [mills]);
},
};

});
blog_20140303_5_3831026 3行 html/xml
Raw
 1
 2
 3
<feature name="Demo">
<param name="android-package" value="plugins.Plugin_intent" />
</feature>
blog_20140303_6_9479608 62行 Java
Raw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
package plugins;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;

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

import com.phonegap.helloworld.DemoActivity;

public class Plugin_intent extends CordovaPlugin {

/**
* 注意 构造方法不能为
*
* Plugin_intent(){}
*
* 可以不写或者 定义为如下
*
*/
public Plugin_intent() {
}

CallbackContext callbackContext;

@Override
public boolean execute(String action, JSONArray args,
CallbackContext callbackContext) throws JSONException {
this.callbackContext = callbackContext;
Log.i("123", action);

if (action.equals("intent")) {
// 获取args的第一个参数
String message = args.getString(0);
this.function();
return true;
}
return false;

}

private void function() {
// cordova.getActivity() 获取当前activity的this
Log.i("123", cordova.getActivity().toString());
Intent intent = new Intent(cordova.getActivity(), DemoActivity.class);
cordova.startActivityForResult((CordovaPlugin) this,intent, 200);

}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
//传递返回值 给js方法
callbackContext.success("activity 跳转成功了");
Toast.makeText(cordova.getActivity(), "123", 0).show();

}
}
blog_20140303_7_2827678 2行 Java
Raw
 1
 2
Intent intent = new Intent(cordova.getActivity(), DemoActivity.class);
cordova.startActivityForResult((CordovaPlugin) this,intent, 200);
blog_20140303_8_7811951 1行 Java
Raw
 1
cordova.getActivity().startActivityForResult(intent,200);
blog_20140303_9_8444023 9行 Java
Raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
/**
* Launch an activity for which you would like a result when it finished. When this activity exits,
* your onActivityResult() method will be called.
*
* @param command The command object
* @param intent The intent to start
* @param requestCode The request code that is passed to callback to identify the activity
*/
abstract public void startActivityForResult(CordovaPlugin command, Intent intent, int requestCode);
blog_20140306_5_303536 1行 JavaScript
Raw
 1
navigator.intent.demo(1);
blog_20140306_6_5527005 41行 JavaScript
Raw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
<!DOCTYPE html>
<html>
<head>
<title>Notification Example</title>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">
// Wait for device API libraries to load
//
document.addEventListener("deviceready", onDeviceReady, false);
// device APIs are available
//
function onDeviceReady() {
// Empty
}
// Show a custom alert
// native的 Dialog 对话框
function showAlert() { navigator.notification.alert( 'You are the winner!', // message
'Game Over',
// title
'Done'
// buttonName
); }
// Beep three times
// 响铃3声
function playBeep() { navigator.notification.beep(3); }
// Vibrate for 2 seconds
// 振动
function vibrate() { navigator.notification.vibrate(100000); }
// 跳转
function intent() { navigator.intent.demo(1); }

</script>
</head>
<body>
<p><a href="#" onclick="showAlert(); return false;">Show Alert</a></p>
<p><a href="#" onclick="playBeep(); return false;">Play Beep</a></p>
<p><a href="#" onclick="vibrate(); return false;">Vibrate</a></p>
<p><a href="#" onclick="intent(); return false;">Html跳转到android界面</a></p>
</body>
</html>
blog_20140306_3_9712231 1行 JavaScript
Raw
 1
navigator.intent.demo(1);
blog_20140306_4_5840773 41行 JavaScript
Raw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
<!DOCTYPE html>
<html>
<head>
<title>Notification Example</title>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">
// Wait for device API libraries to load
//
document.addEventListener("deviceready", onDeviceReady, false);
// device APIs are available
//
function onDeviceReady() {
// Empty
}
// Show a custom alert
// native的 Dialog 对话框
function showAlert() { navigator.notification.alert( 'You are the winner!', // message
'Game Over',
// title
'Done'
// buttonName
); }
// Beep three times
// 响铃3声
function playBeep() { navigator.notification.beep(3); }
// Vibrate for 2 seconds
// 振动
function vibrate() { navigator.notification.vibrate(100000); }
// 跳转
function intent() { navigator.intent.demo(1); }

</script>
</head>
<body>
<p><a href="#" onclick="showAlert(); return false;">Show Alert</a></p>
<p><a href="#" onclick="playBeep(); return false;">Play Beep</a></p>
<p><a href="#" onclick="vibrate(); return false;">Vibrate</a></p>
<p><a href="#" onclick="intent(); return false;">Html跳转到android界面</a></p>
</body>
</html>