是否有示例如何使用 Phonegap 框架对功能进行编程以共享电子邮件、Twitter 和 Facebook 的 URL?例如,在 Android 中,90% 的应用程序都具有此功能。在 iPhone 中,它位于任何应用程序中。在 iPhone 的 techcrunch 应用程序中,当您打开一篇文章时,您可以看到它。是否也可以使用 Phonegap 创建这个?
最佳答案
您可以在 Android 中使用以下插件代码来执行此操作。我尚未在其他地方发布过此内容,但最终我希望将其作为插件添加到 Android 的 PhoneGap 插件存储库中。
JavaScript:
var Share = function() {};
Share.prototype.show = function(content) {
return PhoneGap.exec(
function(args) {
console.log("phonegap share plugin - success!")
}, function(args) {
console.log("phonegap share plugin - failed")
}, 'Share', '', content);
};
PhoneGap.addConstructor(function() {
PhoneGap.addPlugin('share', new Share());
PluginManager.addService("Share","com.COMPANYNAME(CHANGEME).android.plugins.Share");
});
Android 中的 JAVA:
package com.COMPANYNAME(CHANGEME).android.plugins;
import org.json.JSONArray;
import org.json.JSONException;
import android.content.Intent;
import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;
public class Share extends Plugin {
private String callback;
@Override
public PluginResult execute(String action, JSONArray args, String callbackId) {
PluginResult mPlugin = null;
try {
mPlugin = activateSharing(args.getString(0), args.getString(1));
} catch (JSONException e) {
Log.e("JSON Exception", e.toString());
}
mPlugin.setKeepCallback(true);
this.callback = callbackId;
return mPlugin;
}
private PluginResult activateSharing(String title, String body) {
final Intent shareIntent = new Intent(
android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, title);
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, body);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(Intent.createChooser(shareIntent, "Share"));
return new PluginResult(PluginResult.Status.OK);
}
}
关于twitter - Phonegap - 将功能分享到电子邮件、Twitter 和 Facebook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4570701/