2012-12-25 44 views
4

我想分享按鈕添加到我的應用程序,和我做了唯一的Facebook和Twitter的應用程序如下:分享按鈕,在對話框

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和推特。我的手機中都安裝了這兩種應用程序。所以,first問題是爲什麼它不顯示它們?如果以後我會讓它們以某種方式出現在手機中,如何讓該對話框只顯示臉書和推特,如果用戶沒有,就讓用戶通過提供官方應用的鏈接來安裝它。

+0

嗨,你可以使用Facebook和Twitter的API做 – Talha

+0

@talhakosen,我不想安裝他們的軟件開發工具包,並做更多工作。我認爲有可能以某種方式過濾對話框...如果它是 – Daler

+0

我看,請看看我的回答 – Talha

回答

5

您可以通過下面的代碼檢查他們,

How to customize share intent in Android?

Android Intent for Twitter application

我已經看到了很多有關修改應用程序選擇器的問題,他們 似乎都狀態不,您不能更改內置的應用選擇器 ,但是您可以使用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; 
} 
+0

因此,在這種情況下,我不能使用意圖。我必須拿出我自己的對話框,然後以某種方式將它們發送到應用程序。仍然困惑,但一些想法+1) – Daler

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