7

I have a problem in twitter share via intent.

I opened a twitter app via intent,once i click the login in button gets force closed.I cant find the error report.

Is Twitter strict the default share in android? or need to use their SDK.

This is my code for default twitter share.can u guys any idea let me know...

try

{

   Intent shareIntent = ShareCompat.IntentBuilder
   .from(Activity.this).setType("text/plain")
   .setText("Shopup" + review).getIntent()
   .setPackage("com.twitter.android");
   startActivity(shareIntent);

   } catch (Exception e) 
     { 
     // TODO: handle exception
        Toast.makeText(Activity.this, "Need twitter application",
                                            Toast.LENGTH_SHORT).show();

     }
4

1 回答 1

2

This is what I do to share stuff with twitter:

tweetButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            initShareIntentTwi("twi");
        }
    });

private void initShareIntentTwi(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) {
            if (info.activityInfo.packageName.toLowerCase().contains(type)
                    || info.activityInfo.name.toLowerCase().contains(type)) {
                share.putExtra(Intent.EXTRA_TEXT, title + " "
                        + shorturl);
                share.setPackage(info.activityInfo.packageName);
                found = true;
                break;
            }
        }
        if (!found) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Twitter");

            LinearLayout wrapper = new LinearLayout(this);
            WebView webView = new WebView(this);
            EditText keyboardHack = new EditText(this);

            keyboardHack.setVisibility(View.GONE);

            webView.loadUrl("https://twitter.com/intent/tweet?status="
                    + titulo + "%20" + shorturl);

            webView.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view,
                        String url) {
                    view.loadUrl(url);
                    return true;
                }
            });

            wrapper.setOrientation(LinearLayout.VERTICAL);
            wrapper.addView(webView,
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT);
            wrapper.addView(keyboardHack,
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);

            builder.setView(wrapper);

            builder.setNegativeButton("Close",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int id) {
                        }
                    });

            builder.create().show();

            return;
        }
        startActivity(Intent.createChooser(share, "Select"));
    }
}

If the user has the Twitter apps, this will open it and if doesn't it will open a alert dialog with a webview containing this :" https://twitter.com/intent/tweet?status=" plus the text that you want to share. You can ignore this part if you want and just show a alert dialog asking for the app or something like this.

于 2013-06-20T14:56:34.983 回答