Android获取分享应用列表详解及实例
程序员文章站
2022-06-20 13:14:01
android获取分享应用列表详解及实例
如果在应用的androidmanifest.xml中含有 action_send 属性,那就证明该应用可以供第三方应用进行调用分...
android获取分享应用列表详解及实例
如果在应用的androidmanifest.xml中含有 action_send 属性,那就证明该应用可以供第三方应用进行调用分享,那怎么获取函数该属性的分享列表了,这对我们做应用的非常有用;最近在做该功能,自己也做了下自定义的分享列表,用popupwindow的方式弹出。
1、布局:
popup_share.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" > <listview android:id="@+id/share_list" android:background="#2f4f4f" android:fadingedge="none" android:layout_width="fill_parent" android:layout_height="wrap_content" android:cachecolorhint="#00000000" android:divider="#e2dd75" android:dividerheight="1.0dip" android:headerdividersenabled="true" android:footerdividersenabled="false" /> </linearlayout>
popup_share_item.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:gravity="center_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="2.0dip" > <imageview android:id="@+id/share_item_icon" android:layout_width="32.0dip" android:layout_height="32.0dip" android:layout_marginleft="3.0dip" android:scaletype="fitxy" /> <textview android:id="@+id/share_item_name" android:gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="分享" android:textcolor="@color/white" android:singleline="true" android:textsize="@dimen/s_size" android:layout_marginleft="3.0dip" android:layout_marginright="3.0dip" /> </linearlayout>
2、查询手机内所有支持分享的应用列表
public list<resolveinfo> getshareapps(context context) { list<resolveinfo> mapps = new arraylist<resolveinfo>(); intent intent = new intent(intent.action_send, null); intent.addcategory(intent.category_default); intent.settype("text/plain"); // intent.settype("*/*"); packagemanager pmanager = context.getpackagemanager(); mapps = pmanager.queryintentactivities(intent, packagemanager.component_enabled_state_default); return mapps; }
注:applicationinfo是从一个特定的应用得到的信息。这些信息是从相对应的androdimanifest.xml的< application>标签中收集到的。
resolveinfo这个类是通过解析一个与intentfilter相对应的intent得到的信息。它部分地对应于从androidmanifest.xml的< intent>标签收集到的信息。
得到list列表,我自建的appinfo类,自己建一个就行
private list<appinfo> getshareapplist() { list<appinfo> shareappinfos = new arraylist<appinfo>(); packagemanager packagemanager = getpackagemanager(); list<resolveinfo> resolveinfos = getshareapps(mcontext); if (null == resolveinfos) { return null; } else { for (resolveinfo resolveinfo : resolveinfos) { appinfo appinfo = new appinfo(); appinfo.setapppkgname(resolveinfo.activityinfo.packagename); // showlog_i(tag, "pkg>" + resolveinfo.activityinfo.packagename + ";name>" + resolveinfo.activityinfo.name); appinfo.setapplauncherclassname(resolveinfo.activityinfo.name); appinfo.setappname(resolveinfo.loadlabel(packagemanager).tostring()); appinfo.setappicon(resolveinfo.loadicon(packagemanager)); shareappinfos.add(appinfo); } } return shareappinfos; }
3、弹出popupwindow的实现
private void initsharepopupwindow(view parent) { popupwindow sharepopupwindow = null; view view = null; listview sharelist = null; if(null == sharepopupwindow) { //加载布局文件 view = layoutinflater.from(detailexchangeactivity.this).inflate(r.layout.popup_share, null); sharelist = (listview) view.findviewbyid(r.id.share_list); list<appinfo> shareappinfos = getshareapplist(); final sharecustomadapter adapter = new sharecustomadapter(mcontext, shareappinfos); sharelist.setadapter(adapter); sharelist.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { // todo auto-generated method stub intent shareintent = new intent(intent.action_send); appinfo appinfo = (appinfo) adapter.getitem(position); shareintent.setcomponent(new componentname(appinfo.getapppkgname(), appinfo.getapplauncherclassname())); shareintent.settype("text/plain"); // shareintent.settype("*/*"); //这里就是组织内容了, shareintent.putextra(intent.extra_text, "test"); shareintent.setflags(intent.flag_activity_new_task); detailexchangeactivity.this.startactivity(shareintent); } }); sharepopupwindow = new popupwindow(view, (int)(160 * density), linearlayout.layoutparams.wrap_content); } //使其聚焦 sharepopupwindow.setfocusable(true); //设置允许在外点击消失 sharepopupwindow.setoutsidetouchable(true); // 这个是为了点击“返回back”也能使其消失,并且并不会影响你的背景 sharepopupwindow.setbackgrounddrawable(new bitmapdrawable()); //xoff,yoff基于anchor的左下角进行偏移。正数表示下方右边,负数表示(上方左边) //showasdropdown(parent, xpos, ypos); sharepopupwindow.showasdropdown(parent, -5, 5); }
注:sharecustomadapter自己建一个就行了。(显示会有一个图标和一个分享的名字)
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
上一篇: Rewrite基本概述