我用这个插件来实现fb、sms和e-mail。我用的是cordova2.1.0
这三个都没有工作。所以我正在做出改变。现在,短信和电子邮件正在工作。
爪哇代码
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
public class SocialShare extends Plugin {
private String callback;
public String smsCopy = "";
@Override
public PluginResult execute(String action, JSONArray args, String callbackId) {
try
{
if (action.equals("startSmsActivity")) {
JSONObject obj = args.getJSONObject(0);
String type = obj.has("message") ? obj.getString("message") : "";
startSmsActivity(type );
}
else if( action.equals("startEmailActivity") )
{
JSONObject obj = args.getJSONObject(0);
String msg = obj.has("message") ? obj.getString("message") : "";
String subject = obj.has("subject") ? obj.getString("subject") : "";
startEmailActivity(msg, subject );
}
else if( action.equals("startSocialActivity") )
{
JSONObject obj = args.getJSONObject(0);
String msg = obj.has("message") ? obj.getString("message") : "";
startSocialActivity(msg);
}
}
catch (JSONException e) {
e.printStackTrace();
return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
}
PluginResult mPlugin = new PluginResult(PluginResult.Status.NO_RESULT);
mPlugin.setKeepCallback(true);
this.callback = callbackId;
return mPlugin;
}
public void startSmsActivity( String msg ) {
Uri uri = Uri.parse("smsto:");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
it.putExtra("sms_body",msg );
cordova.getActivity().startActivity(it);
}
public void startEmailActivity ( String msg, String subject )
{
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
// emailIntent.setType("text/plain");
emailIntent.setType("message/rfc822");
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject );
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, msg );
// this.ctx.startActivity(emailIntent);
cordova.getActivity().startActivity(emailIntent);
}
@SuppressWarnings("deprecation")
public void startSocialActivity ( String msg )
{
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("text/plain");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, msg );
cordova.getActivity().getPackageManager();
}
public void onActivityResult(int reqCode, int resultCode, Intent data) {
JSONObject smsObj = new JSONObject();
try {
smsObj.put("msg", "done");
} catch (JSONException e) {
e.printStackTrace();
}
this.success(new PluginResult(PluginResult.Status.OK,smsObj
), this.callback);
}
}
Javascript代码
var SocialShare = function() {};
SocialShare.prototype.sms = function(param, successCallback, failCallback) {
function success(args) {
successCallback(args);
}
function fail(args) {
failCallback(args);
}
return PhoneGap.exec(function(args) {
success(args);
}, function(args) {
fail(args);
}, 'SocialShare', 'startSmsActivity', [param]);
};
SocialShare.prototype.email = function(param, successCallback, failCallback) {
function success(args) {
successCallback(args);
}
function fail(args) {
failCallback(args);
}
return PhoneGap.exec(function(args) {
success(args);
}, function(args) {
fail(args);
}, 'SocialShare', 'startEmailActivity', [param]);
};
SocialShare.prototype.social = function(param, successCallback, failCallback) {
function success(args) {
successCallback(args);
}
function fail(args) {
failCallback(args);
}
return PhoneGap.exec(function(args) {
success(args);
}, function(args) {
fail(args);
}, 'SocialShare', 'startSocialActivity', [param]);
};
html-调用函数
<script type="text/javascript" charset="utf=8" src="cordova-2.1.0.js"></script>
<script type="text/javascript" charset="utf-8" src="social.js"></script>
<script type="text/javascript" charset="utf-8" src="twitter.js"></script>
<script type="text/javascript" charset="utf-8">
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady()
{
window.SocialShare = new SocialShare();
function sendFacebook()
{
window.SocialShare.social({message:''},
function(msg) {
},
function(fail) {
}
);
}
function sendEmail()
{
window.SocialShare.email({message:'Email body', subject: "Email Subject"},
function(msg) {
},
function(fail) {
}
);
}
function sendSms()
{
SocialShare.sms= function(param, successCallback, failCallback) {
function success(args) {
successCallback(args);
}
function fail(args) {
failCallback(args);
}
return PhoneGap.exec(function(args) {
success(args);
}, function(args) {
fail(args);
}, 'SocialShare', 'startSmsActivity', [param]);
};
window.SocialShare.sms({message:'Message '},
function(msg) {
},
function(fail) {
}
);
}
function twitter()
{
window.plugins.twitter.composeTweet(
function(){
console.log("Twitter working!!! yahooooo");
},
function(){
alert("We have a problem with the plugin");
},
"tweet!!"
);
}
我的疑问 - facebook 没有连接的原因是什么。