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

Mr.*_*Rao 5 android android-intent android-event android-facebook

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

分享意图对话

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

小智 11

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

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

将此列表发送到Adapter类:

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

这是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;
}}
Run Code Online (Sandbox Code Playgroud)

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

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

        }
    });
Run Code Online (Sandbox Code Playgroud)