Share via facebook/twitter application in android , don't open browser if facebook/twitter is installed on the device

匆匆过客 提交于 2019-12-04 22:02:08

To post something using Facebook than integrate Facebook android sdk

Getting Started with the Facebook SDK for Android

Use Feed Dialog to post on Facebook. It automatically open fb app if it is installed otherwise open in browser

Post to wall

public class PhotoViewer extends Activity implements StatusCallback{

    Button btnShare;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo_viewer);

        btnShare = (Button) findViewById(R.id.btnShare);

        btnShare.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Session.openActiveSession(PhotoViewer.this, true, PhotoViewer.this);

            }
        });

    }

    @Override
    public void call(Session session, SessionState state, Exception exception) {

        if (session.isOpened()) {
            publishFeedDialog();
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
    }

    private void publishFeedDialog() {

    Bundle params = new Bundle();
    params.putString("name", "Facebook SDK for Android");
    params.putString("caption", "Build great social apps and get more installs.");
    params.putString("description", "The Facebook SDK for Android makes it easier and faster to develop Facebook integrated Android apps.");
    params.putString("link", "https://developers.facebook.com/android");
    params.putString("picture", "https://raw.github.com/fbsamples/ios-3.x-howtos/master/Images/iossdk_logo.png");


        WebDialog feedDialog = (
                new WebDialog.FeedDialogBuilder(PhotoViewer.this,
                        Session.getActiveSession(),
                        params))
                        .setOnCompleteListener(new OnCompleteListener() {

                            @Override
                            public void onComplete(Bundle values,FacebookException error) {

                                if (error == null) {
                                    // When the story is posted, echo the success
                                    // and the post Id.
                                    final String postId = values.getString("post_id");
                                    if (postId != null) {
                                        //Toast.makeText(PhotoViewer.this,"Posted story, id: "+postId,Toast.LENGTH_SHORT).show();
                                        Toast.makeText(getApplicationContext(), "Publish Successfully!", Toast.LENGTH_SHORT).show();
                                    } else {
                                        // User clicked the Cancel button
                                        Toast.makeText(getApplicationContext(), "Publish cancelled", Toast.LENGTH_SHORT).show();
                                    }
                                } else if (error instanceof FacebookOperationCanceledException) {
                                    // User clicked the "x" button
                                    Toast.makeText(getApplicationContext(), "Publish cancelled", Toast.LENGTH_SHORT).show();
                                } else {
                                    // Generic, ex: network error
                                    Toast.makeText(getApplicationContext(),"Error posting story",Toast.LENGTH_SHORT).show();
                                }
                            }

                        })
                        .build();
        feedDialog.show();
    }

}

If you are not using a Fragment than use you Context like ClassName.this instead of getActivity()

EDIT

please add this code in onCreate() to print Hash key

// Add code to print out the key hash
PackageInfo info = getPackageManager().getPackageInfo("com.your.package", // replace with your package name 
PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
        MessageDigest md = MessageDigest.getInstance("SHA");
        md.update(signature.toByteArray());

        Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));

    }

by this code you can check if facebook or twitter application is installed on the device. rest you can put your logic if condition is false

final PackageManager pm = getPackageManager();
List<ApplicationInfo> packages = pm
            .getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo packageInfo : packages) {
    Log.d(TAG, "Installed package : " + packageInfo.packageName);
}

Then you can check if packageInfo.packageName is equal to some string which contains the package name of that application.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!