androidのphonegapでwebintentを使用してメールを送信する必要があります。次のコードでエラーが発生します。index.htmlファイルのスクリプトで関数を呼び出す方法がわかりません。json例外が発生します。これ??
WebIntent.java
public class WebIntent extends Plugin {
private String onNewIntentCallback = null;
private static final String TAG = "Webintent";
public PluginResult execute(String action, JSONArray args, String callbackId) {
Log.d(TAG, "WebintentPlugin Called");
try {
System.out.println("JSON11"+args);
if (action.equals("startActivity")) {
if (args.length() != 1) {
return new PluginResult(PluginResult.Status.INVALID_ACTION);
}
// Parse the arguments
JSONObject obj = args.getJSONObject(0);
String type = obj.has("type") ? obj.getString("type") : null;
Uri uri = obj.has("url") ? Uri.parse(obj.getString("url")) : null;
JSONObject extras = obj.has("extras") ? obj.getJSONObject("extras") : null;
Map<String, String> extrasMap = new HashMap<String, String>();
// Populate the extras if any exist
if (extras != null) {
JSONArray extraNames = extras.names();
for (int i = 0; i < extraNames.length(); i++) {
String key = extraNames.getString(i);
String value = extras.getString(key);
extrasMap.put(key, value);
}
}
startActivity(obj.getString("action"), uri, type, extrasMap);
return new PluginResult(PluginResult.Status.OK);
} else if (action.equals("hasExtra")) {
if (args.length() != 1) {
return new PluginResult(PluginResult.Status.INVALID_ACTION);
}
Intent i = ((DroidGap) this.ctx).getIntent();
String extraName = args.getString(0);
return new PluginResult(PluginResult.Status.OK, i.hasExtra(extraName));
} else if (action.equals("getExtra")) {
if (args.length() != 1) {
return new PluginResult(PluginResult.Status.INVALID_ACTION);
}
Intent i = ((DroidGap) this.ctx).getIntent();
String extraName = args.getString(0);
if (i.hasExtra(extraName)) {
return new PluginResult(PluginResult.Status.OK, i.getStringExtra(extraName));
} else {
return new PluginResult(PluginResult.Status.ERROR);
}
} else if (action.equals("getUri")) {
if (args.length() != 0) {
return new PluginResult(PluginResult.Status.INVALID_ACTION);
}
Intent i = ((DroidGap) this.ctx).getIntent();
String uri = i.getDataString();
return new PluginResult(PluginResult.Status.OK, uri);
} else if (action.equals("onNewIntent")) {
if (args.length() != 0) {
return new PluginResult(PluginResult.Status.INVALID_ACTION);
}
this.onNewIntentCallback = callbackId;
PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT);
result.setKeepCallback(true);
return result;
} else if (action.equals("sendBroadcast"))
{
if (args.length() != 1) {
return new PluginResult(PluginResult.Status.INVALID_ACTION);
}
// Parse the arguments
JSONObject obj = args.getJSONObject(0);
System.out.println("JSON"+obj);
JSONObject extras = obj.has("extras") ? obj.getJSONObject("extras") : null;
Map<String, String> extrasMap = new HashMap<String, String>();
// Populate the extras if any exist
if (extras != null) {
JSONArray extraNames = extras.names();
for (int i = 0; i < extraNames.length(); i++) {
String key = extraNames.getString(i);
String value = extras.getString(key);
extrasMap.put(key, value);
}
}
sendBroadcast(obj.getString("action"), extrasMap);
return new PluginResult(PluginResult.Status.OK);
}
return new PluginResult(PluginResult.Status.INVALID_ACTION);
} catch (JSONException e) {
e.printStackTrace();
return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
}
}
@Override
public void onNewIntent(Intent intent) {
if (this.onNewIntentCallback != null) {
PluginResult result = new PluginResult(PluginResult.Status.OK, intent.getDataString());
result.setKeepCallback(true);
this.success(result, this.onNewIntentCallback);
}
}
void startActivity(String action, Uri uri, String type, Map<String, String> extras) {
Intent i = (uri != null ? new Intent(action, uri) : new Intent(action));
if (type != null && uri != null) {
i.setDataAndType(uri, type); //Fix the crash problem with android 2.3.6
} else {
if (type != null) {
i.setType(type);
}
}
for (String key : extras.keySet()) {
String value = extras.get(key);
// If type is text html, the extra text must sent as HTML
if (key.equals(Intent.EXTRA_TEXT) && type.equals("text/html")) {
i.putExtra(key, Html.fromHtml(value));
} else if (key.equals(Intent.EXTRA_STREAM)) {
// allowes sharing of images as attachments.
// value in this case should be a URI of a file
i.putExtra(key, Uri.parse(value));
} else if (key.equals(Intent.EXTRA_EMAIL)) {
// allows to add the email address of the receiver
i.putExtra(Intent.EXTRA_EMAIL, new String[] {"myemail.com"});
} else {
i.putExtra(key, value);
}
}
this.ctx.startActivity(i);
}
void sendBroadcast(String action, Map<String, String> extras) {
Intent intent = new Intent();
intent.setAction(action);
for (String key : extras.keySet()) {
String value = extras.get(key);
intent.putExtra(key, value);
}
((DroidGap) this.ctx).sendBroadcast(intent);
}
}
webintent.js
var WebIntent = function() {
};
WebIntent.ACTION_SEND = "android.intent.action.SEND";
WebIntent.ACTION_VIEW= "android.intent.action.VIEW";
WebIntent.EXTRA_TEXT = "android.intent.extra.TEXT";
WebIntent.EXTRA_SUBJECT = "android.intent.extra.SUBJECT";
WebIntent.EXTRA_STREAM = "android.intent.extra.STREAM";
WebIntent.EXTRA_EMAIL = "android.intent.extra.EMAIL";
WebIntent.prototype.startActivity = function(params, success, fail) {
return cordova.exec(function(args) {
success(args);
}, function(args) {
fail(args);
}, 'WebIntent', 'startActivity', [params]);
};
WebIntent.prototype.hasExtra = function(params, success, fail) {
return cordova.exec(function(args) {
success(args);
}, function(args) {
fail(args);
}, 'WebIntent', 'hasExtra', [params]);
};
WebIntent.prototype.getUri = function(success, fail) {
return cordova.exec(function(args) {
success(args);
}, function(args) {
fail(args);
}, 'WebIntent', 'getUri', []);
};
WebIntent.prototype.getExtra = function(params, success, fail) {
return cordova.exec(function(args) {
success(args);
}, function(args) {
fail(args);
}, 'WebIntent', 'getExtra', [params]);
};
WebIntent.prototype.onNewIntent = function(callback) {
return cordova.exec(function(args) {
callback(args);
}, function(args) {
}, 'WebIntent', 'onNewIntent', []);
};
WebIntent.prototype.sendBroadcast = function(params, success, fail) {
return cordova.exec(function(args) {
success(args);
}, function(args) {
fail(args);
}, 'WebIntent', 'sendBroadcast', [params]);
};
cordova.addConstructor(function() {
cordova.addPlugin('WebIntent', new WebIntent());
});
index.html
<html>
<head>
<meta name="viewport" content="target-densitydpi=low-dpi; user-scalable=no" />
<title>PhoneGap Events Example</title>
<script type="text/javascript" charset="utf-8" src="cordova-1.8.0.js"></script>
<script type="text/javascript" charset="utf-8" src="webintent.js"></script>
<script type="text/javascript" charset="utf-8">
function onBodyLoad()
{
document.addEventListener("deviceready",onDeviceReady,false);
}
function onDeviceReady() {
window.plugins.WebIntent.startActivity('WebIntent', success, fail);
}
function success(e) {
console.log("Success");
}
function fail(f) {
console.log("Failure");
}
</script>
</head>
<body onload="onBodyLoad();">
<h2>IMEI v1.5</h2>
IMEI: <span id="imei"></span><br/>
</body>
</html>
My error is-
07-10 10:23:12.180: W/System.err(536): org.json.JSONException: Value WebIntent at 0 of type java.lang.String cannot be converted to JSONObject
07-10 10:23:12.200: W/System.err(536): at org.json.JSON.typeMismatch(JSON.java:100)
07-10 10:23:12.200: W/System.err(536): at org.json.JSONArray.getJSONObject(JSONArray.java:484)
07-10 10:23:12.230: W/System.err(536): at com.gsr.imei.WebIntent.execute(WebIntent.java:48)
07-10 10:23:12.240: W/System.err(536): at org.apache.cordova.api.PluginManager$1.run(PluginManager.java:186)
07-10 10:23:12.240: W/System.err(536): at java.lang.Thread.run(Thread.java:856)
can anyone help me??Thanks for any help..