如何使用Android在Facebook上分享我们自己的静态状态

时间:2014-04-22 06:06:40

标签: android facebook twitter share socialauth

我正在开发一个应用程序。 在那里,当我点击按钮时,我试图将我的应用程序的信息分享为Facebook和Twitter的状态。

我点击推特按钮时,我想分享的状态会自动转换,但是在Facebook中,我无法在显示facebook共享状态窗口的空白文本框中共享信息。< /强>

我错了,请告诉我?

这是我的代码。

    final String msg="Hie - " +
            "Message to share";

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        .....................
        .....................
    static SocialAuthAdapter adapter;
    adapter = new SocialAuthAdapter(new ResponseListener());
        try 
        {   
            adapter.addConfig(Provider.TWITTER, "key", "BsiY4LH5Y4naSmb.............",null);

        }
        catch (Exception e) 
        {
            e.printStackTrace();
        }

        imgtellfriend.setOnClickListener(new OnClickListener() 
        {
            @Override
            public void onClick(View arg0) 
            {
                anim = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.sequence);  
                imgtellfriend.startAnimation(anim);

                Intent sendIntent = new Intent();
                sendIntent.setAction(Intent.ACTION_SEND);
                sendIntent.putExtra(Intent.EXTRA_TEXT, msg);
                sendIntent.setType("text/plain");
                startActivity(sendIntent);
            }
        });
        imgFb.setOnClickListener(new OnClickListener() 
        {
            @Override
            public void onClick(View arg0) 
            {
                anim = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.sequence);
                imgFb.startAnimation(anim);

                adapter.authorize(Catagories.this, Provider.FACEBOOK);
                initShareIntent("com.facebook.katana");
            }
        });

        imgtwiter.setOnClickListener(new OnClickListener() 
        {
            @Override
            public void onClick(View v) 
            {
                anim = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.sequence);  
                imgtwiter.startAnimation(anim);
                adapter.authorize(Catagories.this, Provider.TWITTER);
            }
        });
    }


    private final class ResponseListener implements DialogListener 
       {
        @Override
        public void onComplete(Bundle values) 
        {
            Log.d("Share-Bar", "Authentication Successful");

            final String providerName = values.getString(SocialAuthAdapter.PROVIDER);

            Log.d("Share-Bar", "Provider Name = " + providerName);

            Toast.makeText(Catagories.this, providerName + " connected", Toast.LENGTH_SHORT).show();

            adapter.updateStatus(msg, new MessageListener(), true); 

        }
        @Override
        public void onCancel() 
        {
            Log.d("Share-Bar", "Authentication Cancelled");
        }
        @Override
        public void onBack() 
        {
            Log.d("Share-Bar", "Dialog Closed by pressing Back Key");
        }
        @Override
        public void onError(SocialAuthError error)
        {
            initShareIntent("com.facebook.katana");
            error.printStackTrace();            
            Log.d("Share-Bar 1", error.toString());
            Log.d("Share-Bar 2", error.getMessage());

        }
    }

    @SuppressLint("DefaultLocale")
    private void initShareIntent(String type) 
       {
        boolean found = false;
        Intent share = new Intent(android.content.Intent.ACTION_SEND);
        share.setType("text/plain");
        // gets the list of intents that can be loaded.
        List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(share, 0);
        if (!resInfo.isEmpty()){
            for (ResolveInfo info : resInfo) {

                Log.d("MEssage FB","--------- App :: "+info.activityInfo.packageName.toLowerCase());

                if (info.activityInfo.packageName.toLowerCase().contains(type) || 
                        info.activityInfo.name.toLowerCase().contains(type) ) 
                {
                    share.putExtra(Intent.EXTRA_SUBJECT,  "ECard");
                    share.putExtra(Intent.EXTRA_TEXT, msg);
                    share.setPackage(info.activityInfo.packageName);
                    found = true;
                    break;
                }
            }
            if (!found)
                return;
            startActivity(Intent.createChooser(share, "Select"));
        }
    }

    private class MessageListener implements SocialAuthListener<Integer>
    {
        @Override
        public void onExecute(String provider, Integer t) 
        {
            Integer status = t;
            if (status.intValue() == 200 || status.intValue() == 201 || status.intValue() == 204)
                Toast.makeText(Catagories.this, "Message posted on " + provider, Toast.LENGTH_LONG).show();
            else
                Toast.makeText(Catagories.this, "Message not posted on " + provider, Toast.LENGTH_LONG).show();
        }

        @Override
        public void onError(SocialAuthError e) {
    }

我正在使用socialauth库。

2 个答案:

答案 0 :(得分:2)

SocialAUth Android新版本正在为facebook工作。唯一需要的是使用native facebook android login

答案 1 :(得分:1)

socialauth库使用旧的facebook sdk。 不要用那个。 您可以直接使用 intent filter 在社交网络上分享。

[更新了一个]

检查

Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
 shareIntent.setType("image/png");
 shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"YOUR TEXT HERE");
 shareIntent.putExtra(android.content.Intent.EXTRA_TEXT,"YOUR TEXT HERE");
 shareIntent.putExtra(android.content.Intent.EXTRA_STREAM,pngUri);
 PackageManager pm = getApplicationContext().getPackageManager();
 List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList) {
    if ((app.activityInfo.name).contains("facebook")) {
        final ActivityInfo activity = app.activityInfo;
        final ComponentName name = new ComponentName(
                    activity.applicationInfo.packageName,
                    activity.name);
        shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                            | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        shareIntent.setComponent(name);
        startActivity(shareIntent);
            }
        }
    }