如何在Android中自定义共享意图Onclick事件

如何为Facebook App自定义Android Share Intent。 当我使用共享Intent时,我得到以下对话框。

分享意图对话

但我使用Facebook sdk发布图像和文本。 以及如何定制,当我们点击上面对话框中的Facebook图标时,它将导航到我的自定义Facebook对话框…

通过使用以下代码,您可以获得安装在移动设备中的社交媒体网络应用列表列表。

Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND); sendIntent.setType("text/plain"); List activities = ShareList.this.getPackageManager().queryIntentActivities(sendIntent, 0); 

将此列表发送到Adapter类:

 ListView lv=(ListView)findViewById(R.id.listView1); final ShareAdapter adapter=new ShareAdapter(ShareList.this,activities.toArray()); lv.setAdapter(adapter); 

这是Adapter类代码:

 public class ShareAdapter extends BaseAdapter { Object[] items; private LayoutInflater mInflater; Context context; public ShareAdapter(Context context, Object[] items) { this.mInflater = LayoutInflater.from(context); this.items = items; this.context = context; } public int getCount() { return items.length; } public Object getItem(int position) { return items[position]; } public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { convertView = mInflater.inflate(R.layout.singleitem, null); holder = new ViewHolder(); holder.name = (TextView) convertView.findViewById(R.id.textView1); holder.logo = (ImageView) convertView.findViewById(R.id.imageView1); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.name .setText(((ResolveInfo) items[position]).activityInfo.applicationInfo .loadLabel(context.getPackageManager()).toString()); holder.logo .setImageDrawable(((ResolveInfo) items[position]).activityInfo.applicationInfo .loadIcon(context.getPackageManager())); return convertView; } static class ViewHolder { TextView name; ImageView logo; }} 

使用以下代码处理列表视图中的特定社交媒体网络:

 lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub ResolveInfo info = (ResolveInfo) adapter.getItem(arg2); if(info.activityInfo.packageName.contains("facebook")) { new PostToFacebookDialog(context, body).show(); //here u can write your own code to handle the particular social media networking apps. Toast.makeText(getApplicationContext(), "FaceBook", Toast.LENGTH_LONG).show(); } else { Intent intent = new Intent(android.content.Intent.ACTION_SEND); intent.setClassName(info.activityInfo.packageName, info.activityInfo.name); intent.setType("text/plain"); intent.putExtra(Intent.EXTRA_SUBJECT, "subject"); intent.putExtra(Intent.EXTRA_TEXT, "body"); ((Activity)ShareList.this).startActivity(intent); } } }); 

我使用Venu的解决方案来实现“自定义共享意图”。 我只是通过创建xml来解决这个问题。所以在这里我想向其他Android初学者展示如何添加custom_share_list_white .xml。 也许它会帮助别人让它运转起来。

           

在ShareAdapter.java中:

  public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { convertView = mInflater.inflate(R.layout.custom_share_list_white, null); holder = new ViewHolder(); holder.name = (TextView) convertView.findViewById(R.id.tv_share_adapter); holder.logo = (ImageView) convertView.findViewById(R.id.imageView); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.name.setText(((ResolveInfo) items[position]).activityInfo .applicationInfo.loadLabel(context.getPackageManager()).toString()); holder.logo.setImageDrawable(((ResolveInfo) items[position]).activityInfo .applicationInfo.loadIcon(context.getPackageManager())); return convertView; }