可以通过这篇文章熟悉创建phonegap插件来启动另一个应用
转自:http://topmanopensource.iteye.com/blog/1521302
在phonegap中通过二维码实现也是通过扩展phonegap的插件实现。
项目结构如下:
实现如下:
二维码扫码实现的插件类:
- package com.easyway.barcode;
-
-
-
- import org.json.JSONArray;
- import org.json.JSONException;
-
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.ActivityNotFoundException;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.net.Uri;
-
- import com.phonegap.api.PhonegapActivity;
- import com.phonegap.api.Plugin;
- import com.phonegap.api.PluginResult;
-
-
-
-
-
-
-
-
-
-
- public class BarcodeScanner extends Plugin {
- public static final int REQUEST_CODE = 0x0ba7c0de;
-
-
- public static final String defaultInstallTitle = "Install Barcode Scanner?";
- public static final String defaultInstallMessage = "This requires the free Barcode Scanner app. Would you like to install it now?";
- public static final String defaultYesString = "Yes";
- public static final String defaultNoString = "No";
-
- public String callback;
-
-
-
-
- public BarcodeScanner() {
- }
-
-
-
-
-
-
-
-
-
-
-
-
- public PluginResult execute(String action, JSONArray args, String callbackId) {
- this.callback = callbackId;
-
- try {
- if (action.equals("encode")) {
- String type = null;
- if(args.length() > 0) {
- type = args.getString(0);
- }
-
- String data = null;
- if(args.length() > 1) {
- data = args.getString(1);
- }
-
- String installTitle = defaultInstallTitle;
- if(args.length() > 2) {
- installTitle = args.getString(2);
- }
-
- String installMessage = defaultInstallMessage;
- if(args.length() > 3) {
- installMessage = args.getString(3);
- }
-
- String yesString = defaultYesString;
- if(args.length() > 4) {
- yesString = args.getString(4);
- }
-
- String noString = defaultNoString;
- if(args.length() > 5) {
- noString = args.getString(5);
- }
-
-
-
-
-
- this.encode(type, data, installTitle, installMessage, yesString, noString);
- }
- else if (action.equals("scan")) {
- String barcodeTypes = null;
- if(args.length() > 0) {
- barcodeTypes = args.getString(0);
- }
-
- String installTitle = defaultInstallTitle;
- if(args.length() > 1) {
- installTitle = args.getString(1);
- }
-
- String installMessage = defaultInstallMessage;
- if(args.length() > 2) {
- installMessage = args.getString(2);
- }
-
- String yesString = defaultYesString;
- if(args.length() > 3) {
- yesString = args.getString(3);
- }
-
- String noString = defaultNoString;
- if(args.length() > 4) {
- noString = args.getString(4);
- }
-
- scan(barcodeTypes, installTitle, installMessage, yesString, noString);
- } else {
- return new PluginResult(PluginResult.Status.INVALID_ACTION);
- }
- PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT);
- r.setKeepCallback(true);
- return r;
- } catch (JSONException e) {
- e.printStackTrace();
- return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void scan(String barcodeFormats, String installTitle, String installMessage, String yesString, String noString ) {
- Intent intentScan = new Intent("com.google.zxing.client.android.SCAN");
-
-
-
-
-
-
-
- try {
- this.ctx.startActivityForResult((Plugin) this, intentScan, REQUEST_CODE);
- } catch (ActivityNotFoundException e) {
- showDownloadDialog(installTitle, installMessage, yesString, noString);
- }
- }
-
-
-
-
-
-
-
-
-
-
- public void onActivityResult(int requestCode, int resultCode, Intent intent) {
- if (requestCode == REQUEST_CODE) {
- if (resultCode == Activity.RESULT_OK) {
- String contents = intent.getStringExtra("SCAN_RESULT");
- String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
- this.success(new PluginResult(PluginResult.Status.OK, " 条形码为:"+contents+" 条码类型为: "+format), this.callback);
- } else {
- this.error(new PluginResult(PluginResult.Status.ERROR), this.callback);
- }
- }
- }
-
-
-
-
-
-
-
-
-
- private void showDownloadDialog(final String title, final String message, final String yesString, final String noString) {
- final PhonegapActivity context = this.ctx;
- Runnable runnable = new Runnable() {
- public void run() {
-
- AlertDialog.Builder dialog = new AlertDialog.Builder(context);
- dialog.setTitle(title);
- dialog.setMessage(message);
- dialog.setPositiveButton(yesString, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dlg, int i) {
- dlg.dismiss();
- Intent intent = new Intent(Intent.ACTION_VIEW,
- Uri.parse("market://search?q=pname:com.google.zxing.client.android")
- );
- try {
- context.startActivity(intent);
- } catch (ActivityNotFoundException e) {
-
- Intent in = new Intent(Intent.ACTION_VIEW);
- in.setData(Uri.parse(""));
- context.startActivity(in);
-
- }
-
- }
- });
- dialog.setNegativeButton(noString, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dlg, int i) {
- dlg.dismiss();
- }
- });
- dialog.create();
- dialog.show();
- }
- };
- context.runOnUiThread(runnable);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void encode(String type, String data, String installTitle, String installMessage, String yesString, String noString) {
- Intent intentEncode = new Intent("com.google.zxing.client.android.ENCODE");
- intentEncode.putExtra("ENCODE_TYPE", type);
- intentEncode.putExtra("ENCODE_DATA", data);
-
- try {
- this.ctx.startActivity(intentEncode);
- } catch (ActivityNotFoundException e) {
- showDownloadDialog(installTitle, installMessage, yesString, noString);
- }
- }
- }
- package com.easyway.barcode;
-
- import com.phonegap.*;
- import android.os.Bundle;
-
-
-
-
-
-
-
-
-
-
-
- public class PhonegapBarcodeActivity extends DroidGap {
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- super.loadUrl("file:///android_asset/www/index.html");
- }
- }
二维码扫码实现的js:
-
-
-
-
-
- var BarcodeScanner = function() {
-
- }
-
- BarcodeScanner.Type = {
- QR_CODE: "QR_CODE",
- DATA_MATRIX: "DATA_MATRIX",
- UPC_E: "UPC_E",
- UPC_A: "UPC_A",
- EAN_8: "EAN_8",
- EAN_13: "EAN_13",
- CODE_128: "CODE_128",
- CODE_39: "CODE_39",
- CODE_93: "CODE_93",
- CODABAR: "CODABAR",
- ITF: "ITF",
- RSS14: "RSS14",
- PDF417: "PDF417",
- RSS_EXPANDED: "RSS_EXPANDED",
- PRODUCT_CODE_TYPES: "UPC_A,UPC_E,EAN_8,EAN_13",
- ONE_D_CODE_TYPES: "UPC_A,UPC_E,EAN_8,EAN_13,CODE_39,CODE_93,CODE_128",
- QR_CODE_TYPES: "QR_CODE",
- ALL_CODE_TYPES: null
- }
-
- BarcodeScanner.Encode = {
- TEXT_TYPE: "TEXT_TYPE",
- EMAIL_TYPE: "EMAIL_TYPE",
- PHONE_TYPE: "PHONE_TYPE",
- SMS_TYPE: "SMS_TYPE",
-
-
- }
-
- BarcodeScanner.prototype.scan = function(types, success, fail, options) {
-
-
- var installTitle = "Install Barcode Scanner?";
- var installMessage = "This requires the free Barcode Scanner app. Would you like to install it now?";
- var yesString = "Yes";
- var noString = "No";
- if (typeof options != 'undefined') {
- if(typeof options.installTitle != 'undefined') {
- installTitle = options.installTitle;
- }
-
- if(typeof options.installMessage != 'undefined') {
- installMessage = options.installMessage;
- }
-
- if(typeof options.yesString != 'undefined') {
- yesString = options.yesString;
- }
-
- if(typeof options.noString != 'undefined') {
- noString = options.noString;
- }
- }
-
-
- return PhoneGap.exec(function(args) {
- success(args);
- }, function(args) {
- fail(args);
- }, 'BarcodeScanner', 'scan', [types, installTitle, installMessage, yesString, noString]);
- };
- BarcodeScanner.prototype.encode = function(type, data, success, fail, options) {
-
-
- var installTitle = "Install Barcode Scanner?";
- var installMessage = "This requires the free Barcode Scanner app. Would you like to install it now?";
- var yesString = "Yes";
- var noString = "No";
- if (typeof options != 'undefined') {
- if(typeof options.installTitle != 'undefined') {
- installTitle = options.installTitle;
- }
-
- if(typeof options.installMessage != 'undefined') {
- installMessage = options.installMessage;
- }
-
- if(typeof options.yesString != 'undefined') {
- yesString = options.yesString;
- }
-
- if(typeof options.noString != 'undefined') {
- noString = options.noString;
- }
- }
-
- return PhoneGap.exec(function(args) {
- success(args);
- }, function(args) {
- fail(args);
- }, 'BarcodeScanner', 'encode', [type, data, installTitle, installMessage, yesString, noString]);
- };
-
- PhoneGap.addConstructor(function() {
-
- if(!window.plugins){
- window.plugins={};
- }
- window.plugins.barcodeScanner=new BarcodeScanner();
-
- PluginManager.addService("BarcodeScanner","com.easyway.barcode.BarcodeScanner");
- });
二维码调用的页面如下:
- >
- <html>
- <head>
- <meta name="viewport" content="width=320; user-scalable=no" />
- <meta http-equiv="Content-type" content="text/html; charset=utf-8">
- <title>二维码扫描title>
-
-
- <script type="text/javascript" charset="utf-8" src="phonegap-1.4.1.js">script>
-
- <script type="text/javascript" charset="utf-8" src="jquery.mobile/jquery-1.6.4.min">script>
-
- <script type="text/javascript" charset="utf-8" src="jquery.mobile/jquery.mobile-1.0.1.js">script>
-
- <script type="text/javascript" charset="utf-8" src="barcodescanner.js">script>
- <script type="text/javascript" charset="utf-8">
-
- $(function(){
- $("#btnbarcode").click(function(){
- window.plugins.barcodeScanner.scan(
- BarcodeScanner.Type.QR_CODE,
- function(result) {
- $("#barcodediv").html(""+result);
- },
- function(error) {
- $("#barcodediv").html("扫描失败:"+result);
- },
- {
- installTitle: "安装提示",
- installMessage:"是否安装开源免费的ZXing二维码扫描器",
- yesString:"确定",
- noString:"取消"
- }
- );
-
- });
- });
- script>
-
-
- head>
- <body >
- <h2>二维码扫描h2>
-
- <p>二维码信息:p>
- <div id="barcodediv">div>
- <input type="button" id="btnbarcode" value="扫描" />
- body>
- html>
备注附件中phonegap的apk,下载之后需要重新命名为PhonegapBarcode.apk即可使用。
阅读(2364) | 评论(0) | 转发(0) |