Android分享中,如何过滤指定的应用,并且对不同的分享方式发送不同的内容?
来源:互联网 发布:modbus仿真软件 编辑:程序博客网 时间:2024/09/21 09:02
场景:
页面上有一个分享按钮,通过各种分享方式,分享不同的内容。
一般的方式:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TITLE, title);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, content);
Intent chooserIntent = Intent.createChooser(intent, "Select app to share");
if (chooserIntent == null) {
return;
}
try {
startActivity(chooserIntent);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(this, "Can't find share component to share", Toast.LENGTH_SHORT).show();
}
问题:
- 一般,通过上面的代码,提供的分享方式有各种应用:邮件,信息,蓝牙,微博,Twitter,二维码扫描器等。
- 但是,第一:我想过滤掉蓝牙,
- 其次:我想对邮件分享详细的内容,对信息和微博等分享较简短的内容,对二维码扫描器只分享URL。
- 请问有什么好的方法达到上述目的?
终于找到解决方法了。
String contentDetails = "";String contentBrief = "";
String shareUrl = "";
Intent it = new Intent(Intent.ACTION_SEND);
it.setType("text/plain");
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(it, 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;
// judgments : activityInfo.packageName, activityInfo.name, etc.
if (activityInfo.packageName.contains("bluetooth") || activityInfo.name.contains("bluetooth")) {
continue;
}
if (activityInfo.packageName.contains("gm") || activityInfo.name.contains("mail")) {
targeted.putExtra(Intent.EXTRA_TEXT, contentDetails);
} else if (activityInfo.packageName.contains("zxing")) {
targeted.putExtra(Intent.EXTRA_TEXT, shareUrl);
} else {
targeted.putExtra(Intent.EXTRA_TEXT, contentBrief);
}
targeted.setPackage(activityInfo.packageName);
targetedShareIntents.add(targeted);
}
Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "Select app to share");
if (chooserIntent == null) {
return;
}
// A Parcelable[] of Intent or LabeledIntent objects as set with
// putExtra(String, Parcelable[]) of additional activities to place
// a the front of the list of choices, when shown to the user with a
// ACTION_CHOOSER.
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[] {}));
try {
startActivity(chooserIntent);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(this, "Can't find share component to share", Toast.LENGTH_SHORT).show();
}
}
过滤掉蓝牙的问题,通过createchooser方式是没有过滤控制权的。只要设置了ACTION_SEND和text/plain的type,那么系统所有支持这两个元素的应用的会被createchooser收集。因此只能自己过滤,使用packagemanager的queryIntentActivities
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
List pkgAppsList = context.getPackageManager().queryIntentActivities( sendIntent, 0);
看一个自己使用listview弹出的例子,在此基础上加上过滤的功能
package com.commonsware.android.launchalot;
import android.app.ListActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Launchalot extends ListActivity {
AppAdapter adapter=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
PackageManager pm=getPackageManager();
Intent main=new Intent(Intent.ACTION_MAIN, null);
main.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> launchables=pm.queryIntentActivities(main, 0);
Collections.sort(launchables,
new ResolveInfo.DisplayNameComparator(pm));
adapter=new AppAdapter(pm, launchables);
setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v,
int position, long id) {
ResolveInfo launchable=adapter.getItem(position);
ActivityInfo activity=launchable.activityInfo;
ComponentName name=new ComponentName(activity.applicationInfo.packageName,
activity.name);
Intent i=new Intent(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_LAUNCHER);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
i.setComponent(name);
startActivity(i);
}
class AppAdapter extends ArrayAdapter<ResolveInfo> {
private PackageManager pm=null;
AppAdapter(PackageManager pm, List<ResolveInfo> apps) {
super(Launchalot.this, R.layout.row, apps);
this.pm=pm;
}
@Override
public View getView(int position, View convertView,
ViewGroup parent) {
if (convertView==null) {
convertView=newView(parent);
}
bindView(position, convertView);
return(convertView);
}
private View newView(ViewGroup parent) {
return(getLayoutInflater().inflate(R.layout.row, parent, false));
}
private void bindView(int position, View row) {
TextView label=(TextView)row.findViewById(R.id.label);
label.setText(getItem(position).loadLabel(pm));
ImageView icon=(ImageView)row.findViewById(R.id.icon);
icon.setImageDrawable(getItem(position).loadIcon(pm));
}
}
}
原文链接:http://www.dewen.org/q/1742#ans3804
- Android分享中,如何过滤指定的应用,并且对不同的分享方式发送不同的内容?
- Android分享中,如何过滤指定的应用,并且对不同的分享方式发送不同的内容?
- Android分享中,如何过滤指定的应用,并且对不同的分享方式发送不同的内容?
- Android分享中,如何过滤指定的应用,并且对不同的分享方式发送不同的内容?
- Android分享中,如何过滤指定的应用,并且对不同的分享方式发送不同的内容?
- 友盟分享:针对不同平台分享不同的内容。
- Android 不同平台分享内容的详细说明
- android 分享到不同的开放平台
- Android 分享实现,微信、微博、腾讯 不同分享方式不同内容
- 3种不同方式的焦点图轮播特效分享
- Android UI控件应用分享:如何实现“带分割线并且Item为正方形”的GridView
- php连接不同数据库的方法分享
- iOS不同传值方法的分享
- ShareSdk中分享失败时,根据不同的平台,提示不同的失败信息
- Android中实现APP文本内容的分享发送与接收方法简述
- 分享17个微信创新应用案例 应用场景的不同应用
- blackberry中两种不同的方式发送短信
- 【Android Training - 06】分享数据内容 [Lesson 1 - 发送分享的数据到其他App]
- Eclipse 安装 Mercurial 插件
- SendMailUtil工具类及应用
- SQL Server 标识列的修改
- 在IDE(VC++)下开发Qt程序
- 在游标中调用存储过程,出错信息写入一张日志表中
- Android分享中,如何过滤指定的应用,并且对不同的分享方式发送不同的内容?
- Android下如何通过JNI方法向上提供接口总结
- ASP.NET中ViewState
- log4z 快速使用教程
- the usage of sscanf()
- 当inline-block和text-indent遇到IE6,IE7
- Android中HAL如何向上层提供接口总结
- [JNI]用JAVA实现全局快捷键
- 12款免费素材图标系列二