4

我想在我的应用程序中添加共享按钮,并且我已经完成了以下操作:

final Intent shareIntent = new Intent(Intent.ACTION_SEND);              
            /* Fill it with Data */
            shareIntent.setType("plain/text");
            shareIntent.putExtra(Intent.EXTRA_TEXT, "www.somesite.com");    


            /* Send it off to the Activity-Chooser */
            startActivity(Intent.createChooser(shareIntent, "Share..."));

它显示了一个对话框,我在这个对话框 facebook 和 twitter 中看不到。我的手机中确实安装了这两个应用程序。那么,first问题是为什么它不显示它们?如果second以后我会让它们以某种方式出现在手机中,如何使该对话框仅显示 facebook 和 twitter,如果用户没有它们,请用户通过提供官方应用程序的链接来安装它。

4

2 回答 2

5

您可以使用以下代码检查它们,

如何在 Android 中自定义分享意图?

Twitter应用程序的Android Intent

我已经看到很多关于修改应用程序选择器的问题,他们似乎都说不,您不能更改内置应用程序选择器,但您可以使用 PackageManager 类中的 queryIntentActivities() 创建自定义应用程序选择器。

try{
    ApplicationInfo info = getPackageManager().getApplicationInfo("com.facebook.katana", 0 );
    return true;
} catch( PackageManager.NameNotFoundException e ){
    return false;
}


try{
    ApplicationInfo info = getPackageManager().getApplicationInfo("com.twitter.android", 0 );
    return true;
} catch( PackageManager.NameNotFoundException e ){
    return false;
}
于 2012-12-25T15:07:08.460 回答
1

成功登录完成后。

public class ShareOnTwitterTrophy extends AsyncTask<String, Integer, Long> {
    private Activity mActivity;
    private Bitmap bitmap;
    public ShareOnTwitterTrophy(Activity mActivity,Bitmap bitmap) {
        this.mActivity=mActivity;
        this.bitmap=bitmap;
    }

    protected void onPreExecute() {
    }

    @Override
    protected Long doInBackground(String... arg0) {

        long result = 0;
        // TwitterSession twitterSession = new TwitterSession(activity);
        // AccessToken accessToken = twitterSession.getAccessToken();
        AccessToken accessToken = new UserSharedPreference(mActivity).getTwitterAccessToken();
        if (accessToken != null) {
            Configuration conf = new ConfigurationBuilder()
                    .setOAuthConsumerKey("your key")
                    .setOAuthConsumerSecret(
                            "your secret")
                    .setOAuthAccessToken(accessToken.getToken())
                    .setOAuthAccessTokenSecret(accessToken.getTokenSecret())
                    .build();

            ImageUploadFactory factory = new ImageUploadFactory(conf);
            ImageUpload upload = factory.getInstance();
            Log.d("", "Start sending image...");
            try {
                ByteArrayOutputStream bytes = new ByteArrayOutputStream();

                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);

                // you can create a new file name "test.jpg" in sdcard
                // folder.
                String imagePath = Environment
                        .getExternalStorageDirectory()
                        + File.separator
                        + "test.jpg";
                File f = new File(imagePath);
                f.createNewFile();
                // write the bytes in file
                FileOutputStream fo = new FileOutputStream(f);
                fo.write(bytes.toByteArray());

                // remember close de FileOutput
                fo.close();
                upload.upload(f, "");
                Log.e("Image Uploaded", "yayeeeee");
                result = 1;
            } catch (Exception e) {
                Log.e("image upload failed", "awwwww :(");
                e.printStackTrace();
            }

            return result;
        }
        return result;
    }

    @Override
    protected void onPostExecute(Long result) {
        if (result == 1)
            Toast.makeText(
                    mActivity,
                    mActivity
                            .getString(R.string.twitter_shared_successfully),
                    Toast.LENGTH_LONG).show();
    }
于 2013-10-21T05:55:17.437 回答