2012-08-24 42 views
2
刪除選項
String message = "Text I want to share"; 
Intent share = new Intent(Intent.ACTION_SEND); 
share.setType("text/plain"); 
share.putExtra(Intent.EXTRA_TEXT, message); 
startActivity(Intent.createChooser(share,"Share on")); 

現在它顯示像默認選項:Bluetooth, Email, Facebook, Gmail, LinkedIn, Messaging, Share Via Barcode.添加或createChooser

或者是這些已安裝的應用程序?

我想知道的是,我怎麼能從這個列表中刪除少數。就像我想刪除Share Via Barcode

並添加其他內容?

謝謝

回答

8

使用以下代碼將新項目添加到選配器屏幕。

Intent share = new Intent(Intent.ACTION_SEND); 
share.setType("text/plain");  
share.putExtra(Intent.EXTRA_TEXT, message); 

Intent addIntent = ;//whatever you want 

Intent chooser = new Intent(Intent.ACTION_CHOOSER); 
chooser.putExtra(Intent.EXTRA_INTENT, share);  
chooser.putExtra(Intent.EXTRA_TITLE, "title"); 

Intent[] intentArray = {addIntent }; 
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray); 
startActivity(chooser); 

但是刪除特定項目是不可能的。所以,你可以使用Packagemanager.resolveActivity解決的意圖,並創建自己的自定義列表視圖

+0

哪裏是消息走了?? –

+0

你能詳細說一下嗎? – nandeesh

+0

我的意思是你沒有在代碼中加入:「chooser.putExtra(Intent.EXTRA_TEXT,message);」我只是想知道你是否忘記了或者它不可能以這種方式 –

2

我用下面的代碼來獲取安裝在設備上的所有電子郵件和短信應用程序的列表:

Intent shareSMS = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("smsto", "12346556", null)); 
shareSMS.addCategory(Intent.CATEGORY_DEFAULT); 
shareSMS.putExtra("sms_body", message); 

Intent shareEmail = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", "", null)); 
PackageManager pm = getPackageManager(); 
List<ResolveInfo> mailActivityList = pm.queryIntentActivities(shareEmail, PackageManager.MATCH_DEFAULT_ONLY); 

List<Intent> mailIntents = new ArrayList<Intent>(); 
for (ResolveInfo resInfo : mailActivityList) { 
    Intent targetedOpenIntent = new Intent(android.content.Intent.ACTION_SENDTO, Uri.fromParts("mailto", "", null)) 
      .setPackage(resInfo.activityInfo.packageName) 
      .putExtra(Intent.EXTRA_EMAIL, emails) 
      .putExtra(Intent.EXTRA_SUBJECT, subject) 
      .putExtra(Intent.EXTRA_TEXT, message); 
    mailIntents.add(targetedOpenIntent); 
} 

Intent chooser = new Intent(Intent.ACTION_CHOOSER); 
chooser.putExtra(Intent.EXTRA_INTENT, shareSMS); 
chooser.putExtra(Intent.EXTRA_TITLE, "Send request"); 
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, mailIntents.toArray(new Parcelable[] { })); 

startActivity(chooser);