獲取

2012-11-30 40 views
1

我使用下面的代碼通過Gmail一些文本所選共享意圖,Facebook,微博等:獲取

Intent intent = new Intent(Intent.ACTION_SEND); 
intent.setType("text/plain"); 
intent.putExtra(Intent.EXTRA_TEXT, "The status update text"); 
startActivity(Intent.createChooser(intent, "Dialog title text")); 

的事情是,我要檢查,如果選擇的意圖是Facebook和如果是這樣,我會做一些其他編碼,而不是照常提示分享意圖。

可能嗎?

+0

據我所知,這是不可能的。你不能接收任何與Facebook,Twitter的對話框...一個想法是當單擊「共享」時啓動一個CountDownTimer,例如10秒,然後用ActivityManager檢查哪一個Activity在你的頂部。 –

+0

你可以使用PackageManager做一個這樣的事情,你可以在startActivity(Intent.createChooser(intent,「對話框標題文本」))後檢查Activity Stack中的Current top Activity。 –

+0

@imrankhan我該怎麼做? – idish

回答

3

我只是發現了以下鏈接:http://clickclickclack.wordpress.com/2012/01/03/intercepting-androids-action_send-intents/

它介紹瞭如何執行這樣的任務,所有你需要做的就是創建行佈局,就大功告成了。 我試過了,它效果很棒!

編輯: 有一個在show功能在文章中的錯誤,它應該是以下幾點:

final ShareIntentListAdapter adapter = new ShareIntentListAdapter((Activity)context, R.layout.basiclistview, activities.toArray());

感謝@imram汗的一提的是。

如果有人想用我的XML列的佈局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 


    <TextView android:id="@+id/text1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="10dp" 
    android:textSize="20sp" 
    android:gravity="center" 
    android:layout_alignParentRight="true" 
    android:layout_marginRight="50dp" 
/> 


    <ImageView android:id="@+id/logo2" 
    android:layout_width="70dp" 
    android:layout_height="70dp" 
    android:padding="10dp" 
    android:src="@drawable/number1" 
    android:layout_gravity="center_vertical" 
    android:layout_alignParentRight="true"/> 

</RelativeLayout> 
+0

+ 1vote您也可以接受它作爲回答自己和xml佈局或'最終ShareIntentListAdapter適配器=新ShareIntentListAdapter((Activity)上下文,R.layout.basiclistview,activities.toArray());'作爲當前代碼 –

+0

@imrankhan好的編輯,我只是​​試圖接受我的答案,但我只能在短短2天內。無論如何,再次感謝你。 – idish

1

有同樣的確切問題,想要檢測的Facebook共享選項,採取了不同的邏輯路徑(主要是因爲通過普通的android共享Facebook的分享是有限的)

我的溶液

public void shareIt(View view){ 
    //sharing implementation 
    List<Intent> targetedShareIntents = new ArrayList<Intent>(); 
    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); 
    sharingIntent.setType("text/plain"); 
    String shareBody = "string of text " + txt_var + " more text! Get the app at http://someapp.com"; 

    PackageManager pm = view.getContext().getPackageManager(); 
    List<ResolveInfo> activityList = pm.queryIntentActivities(sharingIntent, 0); 
    for(final ResolveInfo app : activityList) { 

     String packageName = app.activityInfo.packageName; 
     Intent targetedShareIntent = new Intent(android.content.Intent.ACTION_SEND); 
     targetedShareIntent.setType("text/plain"); 
     targetedShareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "share"); 
     if(TextUtils.equals(packageName, "com.facebook.katana")){ 
      targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "http://someurl.com"); 
     } else { 
      targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody); 
     } 

     targetedShareIntent.setPackage(packageName); 
     targetedShareIntents.add(targetedShareIntent); 

    } 

    Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "Share Idea"); 

    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{})); 
    startActivity(chooserIntent); 

} 

其上這種問答列出和A Branching the Android Share Intent extras depending on which method they choose to share

+0

謝謝,它對我有用,我用它在用戶選擇Twitter時剪切文本,謝謝 –