在Android分享知道有一个更方便的方法。调用的共享面板来分享我们的应用程序的系统。主要实现例如,下面的:

	public Intent getShareIntent(){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, "这是測试分享面板, http://www.baidu.comss");
intent.setType("text/plain");
return intent;
}

另一种是实如今ActionBar上加入分享列表,实现代码例如以下:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_item_share"
android:showAsAction="ifRoom"
android:title="Share"
android:actionProviderClass="android.widget.ShareActionProvider" />
</menu>

	@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.actionbar_menu, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
shareActionProvider = (ShareActionProvider) item.getActionProvider();
Intent shareIntent = getShareIntent();
shareActionProvider.setShareIntent(shareIntent);
return true;
} public Intent getShareIntent(){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, "这是測试分享面板, http://www.baidu.comss");
intent.setType("text/plain");
return intent;
}

系统默认会为我们找出全部支持seteType中类型的应用。相同我们能够实现自己定义分享的平台。

	private void initShareIntent() {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(
intent, 0);
if (!resInfo.isEmpty()) {
List<Intent> targetedShareIntents = new ArrayList<Intent>();
for (ResolveInfo info : resInfo) {
Intent targeted = new Intent(Intent.ACTION_SEND);
targeted.setType("text/plain");
ActivityInfo activityInfo = info.activityInfo;
//在这里能够加入对应的平台。用 || 连接
if (activityInfo.packageName.contains("com.tencent.mm")) {
targeted.putExtra(Intent.EXTRA_TEXT, "分享内容");
targeted.setPackage(activityInfo.packageName);
targetedShareIntents.add(targeted);
}
}
Intent chooserIntent = Intent.createChooser(
targetedShareIntents.remove(0), "Select app to share");
if (chooserIntent == null) {
return;
}
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
targetedShareIntents.toArray(new Parcelable[] {}));
try {
startActivity(chooserIntent);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(this, "Can't find sharecomponent to share",
Toast.LENGTH_SHORT).show();
}
}
}

系统的分享面板存在一些缺陷,比方每一个手机显示的面板的样式不同,不同手机上显示的分享平台种类和数目不同,会出现一些杂乱的应用。我们能够给上面的方法加入參数,让仅仅能分享到一个平台就能够解决问题。这样我们就能够自己定义一个分享面板。来加入我们想要的应用,代码例如以下:

	private void initShareIntent(String type) {
boolean found = false;
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("image/*");
// gets the list of intentsthat can be loaded.
List<ResolveInfo> resInfo =getPackageManager().queryIntentActivities(
share, 0);
if (!resInfo.isEmpty()) {
for (ResolveInfo info : resInfo) {
if (info.activityInfo.packageName.toLowerCase().contains(type)
|| info.activityInfo.name.toLowerCase().contains(type)) {
share.putExtra(Intent.EXTRA_SUBJECT, "subject");
share.putExtra(Intent.EXTRA_TEXT, "your text"); //share.putExtra(Intent.EXTRA_STREAM,
// Uri.fromFile(newFile(myPath))); // Optional, just
// // if you wanna
// // share an
// // image.
share.setPackage(info.activityInfo.packageName);
found = true;
break;
}
}
if (!found)
return;
startActivity(Intent.createChooser(share, "Select"));
}
}

Android定义自己的面板共享系统的更多相关文章

  1. android 21 隐式意图启动系统预定义activity

    Intent intent=new Intent(LoginActivity.this, MainActivity.class);//显示意图启动,显示从一个activity到另一个activity, ...

  2. Android融合推送MixPush SDK集成多家推送平台,共享系统级推送,杀死APP也能收到推送

    消息推送是App运营的重要一环,为了优化消息推送成功率,降低电量和流量消耗,系统级的推送服务显得尤为重要.小米和魅族由此推出了自家的推送平台,在MIUI和Flyme上共享系统级推送服务,让APP在被杀 ...

  3. Android手机如何通过USB共享网络给Mac?

    最近网络挂了,mac不能上网查资料,心情非常毛躁,急切寻求用mac蹭WiFi的方法. 没有找到电脑端破解WiFi密码的软件,手头的Android手机没有root,也无法查看WiFi密码--->破 ...

  4. Android应用程序框架层和系统运行库层日志系统源代码分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6598703 在开发Android应用程序时,少 ...

  5. Android震动vibrator(马达)--系统到驱动的流程【转】

    本文转载自:https://blog.csdn.net/tianshiyalin/article/details/17136723 一.前言 本人刚学习安卓驱动开发,水平不能说菜,是根本没有水平,在这 ...

  6. Android自定义日历控件(继承系统控件实现)

    Android自定义日历控件(继承系统控件实现) 主要步骤 编写布局 继承LinearLayout设置子控件 设置数据 继承TextView实现有圆圈背景的TextView 添加Attribute 添 ...

  7. Android定义的路径全局变量

    Android定义的路径全局变量 ifeq (,$(strip $(OUT_DIR))) OUT_DIR := $(TOPDIR)out endif DEBUG_OUT_DIR := $(OUT_DI ...

  8. Android 通过应用程序来设置系统的日期和时间中的

    Android 通过应用程序来设置系统的日期和时间中的 android 2.3 android 4.0 测试可行,刚需ROOT权限. import java.io.DataOutputStream; ...

  9. Android模拟、实现、触发系统按键事件的方法

     Android模拟.实现.触发系统按键事件的方法 /** * 模拟系统按键. * * @param keyCode */ public static void onKeyEvent(final ...

随机推荐

  1. MySQL内存表的特性与使用介绍 -- 简明现代魔法

    MySQL内存表的特性与使用介绍 -- 简明现代魔法 MySQL内存表的特性与使用介绍

  2. 设计模式(三)-- 适配器模式(Adapter)

    适配器模式(Adapter) 考虑一个记录日志的应用,由于用户对日志记录的要求很高,使得开发人员不能简单地采用一些已有的日志工具或日志框架来满足用户的要求,而需要按照用户的要求重新开发新的日志管理系统 ...

  3. 多线程——实现Callable接口

    前两篇博客(多线程--继承Thread类.多线程--实现Runnable接口 )介绍了java使用线程的两种方法.这篇博客继续介绍第三种方法--实现Callable接口. 先说一下Runnable和C ...

  4. hdu1573-X问题

    http://acm.hdu.edu.cn/showproblem.php?pid=1573 中国剩余定理 #include<iostream> #include<cstdio> ...

  5. (一)----使用HttpClient发送HTTP请求(通过get方法获取数据)

    (一)----使用HttpClient发送HTTP请求(通过get方法获取数据) 一.HTTP协议初探: HTTP(Hypertext Transfer Protocol)中文 “超文本传输协议”,是 ...

  6. 旧发票要保留SIRET等信息,或者整个PDF

    查看旧发票时,每次都实时生成发票是不行的,因为公司的SIRET居然会是变的!!

  7. Types of Windows

    The Window's Real Estate   Application's Instance   A window is referred to as parent when it can be ...

  8. 《转》在win7,boa-constructor 0.6.1 的palette面板中没有控件图标的解决方法

    原地址:http://blog.csdn.net/rickleo/article/details/6532595 在win7-64bit环境下,boa-constructor 0.6.1 的palet ...

  9. JS的类型比较与转换图

    完整比较图:红色:===橙色:==黄色:<= 和 >= 同时成立,== 不成立蓝色:只有 >=绿色:只有 <= https://www.zhihu.com/question/3 ...

  10. 使用ffmpeg 对视频截图,和视频转换格式

    //执行CMD命令方法 public static void CmdProcess(string command)//调用CMD        {            //实例化一个进程类      ...