我想使用Android Intent.ACTION_SEND來快速分享內容。所以我有一個共享列表如下: 如何知道在Intent.ACTION_SEND中選擇了哪個意圖?
但我想每個動作分享不同的內容,如:
如果共享通過電子郵件/ Gmail的,內容應該是「通過電子郵件共享」。
如果通過Facebook共享,內容應該是「通過Facebook共享」。
那麼,有沒有可能這樣做?
我想使用Android Intent.ACTION_SEND來快速分享內容。所以我有一個共享列表如下: 如何知道在Intent.ACTION_SEND中選擇了哪個意圖?
但我想每個動作分享不同的內容,如:
如果共享通過電子郵件/ Gmail的,內容應該是「通過電子郵件共享」。
如果通過Facebook共享,內容應該是「通過Facebook共享」。
那麼,有沒有可能這樣做?
您無法獲取此類信息。
除非您爲活動選擇創建了自己的對話框實現。
要創建這樣的對話框,您需要使用PackageManager
及其queryIntentActivities()
函數。該函數返回List<ResolveInfo>
。
ResolveInfo
包含有關的活動(resolveInfo.activityInfo.packageName
)的一些信息,並與PackageManager
的幫助下,你可以得到其他的信息(顯示在對話框中的活動很有用) - 應用程序圖標繪製,應用標籤,...。
將結果顯示在對話框的列表中(或稱爲對話框的活動)。單擊某個項目時,創建新的Intent.ACTION_SEND,添加所需內容並添加所選活動的包裝(intent.setPackage(pkgName)
)。
不確定您是否仍在尋找答案,但ClickClickClack有一個示例實現,說明如何攔截ACTION_SEND意圖並根據包名和某些特徵選擇最終發生的結果。涉及到Tomik提到的大部分步驟。這個實施
http://clickclickclack.wordpress.com/2012/01/03/intercepting-androids-action_send-intents/
一個強大的方面是可以添加的分析您的來電。
這是一個輝煌的實現。 – TomaszRykala
沒有訪問此類信息的直接方法....
第1步:在內部代碼首先需要聲明其中將包含列表的自定義視圖要共享上的適配器的...
//sharing implementation
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
// what type of data needs to be send by sharing
sharingIntent.setType("text/plain");
// package names
PackageManager pm = getPackageManager();
// list package
List<ResolveInfo> activityList = pm.queryIntentActivities(sharingIntent, 0);
objShareIntentListAdapter = new ShareIntentListAdapter(CouponView.this,activityList.toArray());
// Create alert dialog box
AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
builder.setTitle("Share via");
builder.setAdapter(objShareIntentListAdapter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
ResolveInfo info = (ResolveInfo) objShareIntentListAdapter.getItem(item);
// if email shared by user
if(info.activityInfo.packageName.contains("Email")
|| info.activityInfo.packageName.contains("Gmail")
|| info.activityInfo.packageName.contains("Y! Mail")) {
PullShare.makeRequestEmail(COUPONID,CouponType);
}
// start respective activity
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setClassName(info.activityInfo.packageName, info.activityInfo.name);
intent.setType("text/plain");
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, ShortDesc+" from "+BusinessName);
intent.putExtra(android.content.Intent.EXTRA_TEXT, ShortDesc+" "+shortURL);
intent.putExtra(Intent.EXTRA_TITLE, ShortDesc+" "+shortURL);
((Activity)context).startActivity(intent);
}// end onClick
});
AlertDialog alert = builder.create();
alert.show();
第2步:現在你有你的適配器創建佈局吹氣(ShareIntentListAdapter.java)
package com.android;
import android.app.Activity;
import android.content.pm.ResolveInfo;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class ShareIntentListAdapter extends ArrayAdapter{
private final Activity context;
Object[] items;
public ShareIntentListAdapter(Activity context,Object[] items) {
super(context, R.layout.social_share, items);
this.context = context;
this.items = items;
}// end HomeListViewPrototype
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.social_share, null, true);
// set share name
TextView shareName = (TextView) rowView.findViewById(R.id.shareName);
// Set share image
ImageView imageShare = (ImageView) rowView.findViewById(R.id.shareImage);
// set native name of App to share
shareName.setText(((ResolveInfo)items[position]).activityInfo.applicationInfo.loadLabel(context.getPackageManager()).toString());
// share native image of the App to share
imageShare.setImageDrawable(((ResolveInfo)items[position]).activityInfo.applicationInfo.loadIcon(context.getPackageManager()));
return rowView;
}// end getView
}// end main onCreate
第3步:創建XML佈局TY pe在對話框中顯示列表視圖(social_share。XML)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/categoryCell"
android:layout_width="match_parent"
android:layout_height="30dip"
android:background="@android:color/white" >
<TextView
android:id="@+id/shareName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginBottom="15dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="15dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
android:textStyle="bold" />
<ImageView
android:id="@+id/shareImage"
android:layout_width="35dip"
android:layout_height="35dip"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:contentDescription="@string/image_view" />
</RelativeLayout>
// vKj
我還建議將alert.show()更改爲由IF包圍以防止多次點擊。如果(!alert.isShowing()){alert.show(); } – jmnwong
使用Tomik偉大回答我能夠生產使用PackageManager loadLabel和LoadIcon自己的自定義共享列表:
public class MainActivity extends FragmentActivity
{
ArrayList<Drawable> icons;
ArrayList<String> labels;
@Override
protected void onCreate(Bundle arg0) {
// TODO Auto-generated method stub
super.onCreate(arg0);
setContentView(R.layout.activity_main);
icons=new ArrayList<Drawable>();
labels=new ArrayList<String>();
PackageManager manager=getPackageManager();
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
List<ResolveInfo> activities=manager.
queryIntentActivities(intent,0);
for(int i=0;i<activities.size();i++)
{
ApplicationInfo appInfo=null;
try {
appInfo=manager.getApplicationInfo(activities.get(i).activityInfo.packageName,0);
labels.add(name=appInfo.loadLabel(getPackageManager()).toString());
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
icons.add(appInfo.loadIcon(getPackageManager()));
}
}
}
問題出在哪裏?你發送的意圖取決於點擊的項目?!問題在哪裏 –
啊,我的意思是我怎麼知道選擇哪個Intent來執行共享操作? – anticafe
這麼糟糕,根據http://stackoverflow.com/questions/6137592/how-to-know-the-action-choosed-in-a-intent-createchooser和http://stackoverflow.com/questions/4417019/如何從用戶選擇startactivityforresultintent-createchooserfi,我無法找到用戶選擇什麼意圖。 – anticafe