Android通过第三方应用将自己的应用信息分享出去

     通过第三方应用将自己应用信息分享出去。贴代码

    1, //点击分享之后弹出的对话框,这个对话框是我自己定义的    

Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");//分享的类型
        AlertDialog.Builder customDia = new AlertDialog.Builder(activity, R.style.share_dialog);//自己定义的对话框
        final View viewDia = LayoutInflater.from(activity).inflate(R.layout.dialog, null);//布局里面是gridview
//        ImageView shareClose = (ImageView)viewDia.findViewById(R.id.share_close);
        GridView listView =(GridView)viewDia.findViewById(R.id.grid_view);
        final ShareAdapter adapter = new ShareAdapter(activity, intent);
        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView arg0, View view, int position,long id) {
                Intent intent = adapter.getIntentForPosition(position);
                SharedPreferences share = activity.getSharedPreferences("service_data", Context.MODE_PRIVATE);
                String url = /*share.getString("shareUrl", "")*/"";
                String local = activity.getResources().getConfiguration().locale.getCountry();
                String des = "";
                if("".equals(url) || url == null){
                    if ("CN".equals(local)) {
                        url = "http://www.wanyiwan.com";//分享的信息区分了语言
                    } else {
                        url = "https://wanyiwan.com";
                    }
                    if("".equals(des) || des == null){
                        des =activity.getResources().getString(R.string.des) ;
                    }
                }
                intent.putExtra(Intent.EXTRA_TEXT, des + "\n "+ url);
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                activity.startActivity(intent);
     2,适配器  



    public ShareAdapter(Context context, Intent intent) {
        mContext = context;
        mPackageManager = mContext.getPackageManager();
        mIntent = new Intent(intent);
        mInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        packageManager = mContext.getPackageManager();//拿到包管理器,它负责拿到图标
        mList = packageManager.queryIntentActivities(intent, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);//负责通过意图拿到能分享信息的应用

        mDisplayResolveInfoList = new ArrayList();
        if (mList == null || mList.isEmpty()) {
            mList = new ArrayList();
        }

        final int N = mList.size();
        for (int i = 0; i < N; i++) {
            ResolveInfo ri = mList.get(i);
            CharSequence label = ri.loadLabel(mPackageManager);
            DisplayResolveInfo d = new DisplayResolveInfo(ri, null, null, label, null, new ComponentName(ri.activityInfo.packageName, ri.activityInfo.name));
            if (!mDisplayResolveInfoList.contains(d)) {
                mDisplayResolveInfoList.add(d);
            }
        }

//        comparison();
        Collections.sort(mDisplayResolveInfoList, new SharedAppComparator());
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if(convertView == null) {
            convertView = mInflater.inflate(R.layout.share_item, null);
            viewHolder = new ViewHolder();
            viewHolder.imageView = (ImageView)convertView.findViewById(R.id.icon_share);
            viewHolder.textView = (TextView)convertView.findViewById(R.id.title_share);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder)convertView.getTag();
        }
        DisplayResolveInfo info = mDisplayResolveInfoList.get(position);
        viewHolder.imageView.setImageDrawable(info.mResoleInfo.loadIcon(packageManager));//拿到图标
        Log.e("share", "share icon==== " + info.mResoleInfo.loadIcon(packageManager) + " share title = " + info.mLabel);
        viewHolder.textView.setText(info.mLabel);//拿到标题
        return convertView;
    }

    public ResolveInfo getResolveInfo(int index){//封装了一层
        if(mDisplayResolveInfoList == null){
            return null;
        }
        DisplayResolveInfo d = mDisplayResolveInfoList.get(index);
        if(d.mResoleInfo == null){
            return null;
        }
        return d.mResoleInfo;
    }

    //返回跳转intent
    public Intent getIntentForPosition(int index) {
        if(mDisplayResolveInfoList == null){
            return null;
        }
        DisplayResolveInfo d = mDisplayResolveInfoList.get(index);
        Intent i = new Intent(d.mIntent == null ? mIntent : d.mIntent);
        i.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
        if(d.mResoleInfo != null){
            ActivityInfo a = d.mResoleInfo.activityInfo;
            i.setComponent(new ComponentName(a.applicationInfo.packageName, a.name));
        }
        return i;
    }



    /**
     * 打包数据 vo
     * @author
     */
    class DisplayResolveInfo {//封装
        private Intent mIntent;
        private ResolveInfo mResoleInfo;
        private CharSequence mLabel;
        private Drawable mDrawable;
        private ComponentName cn;

        private int position = Integer.MAX_VALUE;

        DisplayResolveInfo(ResolveInfo resolveInfo, Intent intent,
                           CharSequence info, CharSequence label, Drawable d,
                           ComponentName cn) {
            this.mIntent = intent;
            this.mResoleInfo = resolveInfo;
            this.mLabel = label;
            this.mDrawable = d;
            this.cn = cn;
        }


  


}
   如果想把自己的应用设定为能发送分享信息的应用,可以将activity的

    android:name="android.intent.action.SEND" />

    android:mimeType="text/plain" />

    android:name="android.intent.category.DEFAULT" />
,Android就可以通过这个类型将应用分为能发送信息的应用

              

 


你可能感兴趣的:(Android通过第三方应用将自己的应用信息分享出去)