Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
517 views
in Technique[技术] by (71.8m points)

cordova - Phonegap - Share functionality to Email, Twitter and Facebook

Is there an example how to program the functionality with the Phonegap Framework to share a URL to email, twitter and Facebook? For Example in Android this functionality is in 90% of the apps. In Iphone it is in any Apps. In the app of techcrunch for Iphone you can see it, when You open an article. Is it possible to create this with Phonegap too?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can do this in Android with the following code for a plugin. I haven't published this anywhere else yet, but eventually I hope to add it as a plugin in the phonegap plugin repository for Android.

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");
});

JAVA IN ANDROID:

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);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...