原文地址
感觉自己的技术太渣了,所以想通过CSDN微博学习,想撸一遍任主席的微博。这里只做学习记载,并无其他想法。
在今天知道了分享存在形式:
1.使用官方的SDK工具进行集成分享(在我不知道还存在第二种方式的时候我用的一直是这种)
优点:功能强大
缺点:需要在官方网站中注册自己APK的相关信息,需要官方进行审核。
2.直接跳转到相应的分享平台的应用上
优点:无需集成,不需要官方审核
缺点:分享平台的相关apk必须安装在手机上,需要知道分享平台应用的包名
这里学习的是第二种方法:
public void onSharedNoKey(View view) {
//不申請key分享
String packName="";
//1.設置Intent action
Intent intent=new Intent();
intent.setAction(Intent.ACTION_SEND);
//2.发送类型
intent.setType("text/plain");
Random random=new Random();
switch (random.nextInt(2)){
case 0:
//qq空間
packName="com.qzone";
break;
case 1:
//騰訊微博
packName="com.tencent.WBlog";
break;
case 2:
//微信
packName="com.tencent.mm";
break;
}
//3.设置包名
intent.setPackage(packName);
intent.putExtra(Intent.EXTRA_TITLE,"這裡是标题");
intent.putExtra(Intent.EXTRA_SUBJECT,"這是分享主題");
intent.putExtra(Intent.EXTRA_TEXT,"這是分享內容");
startActivity(Intent.createChooser(intent,"愛分享"));
}
这里用到了Intent.createChooser这个方法我从来都没有过,这个是创建一个选择应用的类似于PopuWindow、Dialog的弹窗(其实是一个activity通过查看源码发现的)
源码:
public static Intent createChooser(Intent target, CharSequence title, IntentSender sender) {
Intent intent = new Intent(ACTION_CHOOSER);
intent.putExtra(EXTRA_INTENT, target);
if (title != null) {
intent.putExtra(EXTRA_TITLE, title);
}
if (sender != null) {
intent.putExtra(EXTRA_CHOSEN_COMPONENT_INTENT_SENDER, sender);
}// Migrate any clip data and flags from target.
int permFlags = target.getFlags() & (FLAG_GRANT_READ_URI_PERMISSION
| FLAG_GRANT_WRITE_URI_PERMISSION | FLAG_GRANT_PERSISTABLE_URI_PERMISSION
| FLAG_GRANT_PREFIX_URI_PERMISSION);
if (permFlags != 0) {
ClipData targetClipData = target.getClipData();
if (targetClipData == null && target.getData() != null) {
ClipData.Item item = new ClipData.Item(target.getData());
String[] mimeTypes;
if (target.getType() != null) {
mimeTypes = new String[] { target.getType() };
} else {
mimeTypes = new String[] { };
}
targetClipData = new ClipData(null, mimeTypes, item);
}
if (targetClipData != null) {
intent.setClipData(targetClipData);
intent.addFlags(permFlags);
}
}
return intent;
}
通过查看源码发现此方法返回的是一个Intent,再源码AndroidManifest.xml,通过该Intent的action搜索得到我们现在知道的结果。
<activity android:name="com.android.internal.app.ChooserActivity"
android:theme="@style/Theme.Holo.Dialog.Alert"
android:finishOnCloseSystemDialogs="true"
android:excludeFromRecents="true"
android:multiprocess="true">
<intent-filter>
<action android:name="android.intent.action.CHOOSER"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
为什么会出现这样的问题呢?
很明显的问题就是我的手机没有安装任何分享平台的软件,那这里是弹出这个框好些还是Toast好一些呢?我想若没有能打开的应用在采用Taost的方式提示。修改代码:
List<ResolveInfo> infoList = getPackageManager().queryIntentActivities(intent, 0);
int time = 0;
for (ResolveInfo resolveInfo : infoList) {
if (resolveInfo.activityInfo.packageName.equals(packName)) {
time++;
}
}
Log.e("TAG", time + "");
if (time>0)
startActivity(Intent.createChooser(intent, "愛分享"));
else
Toast.makeText(this, "请安装分享的APP", Toast.LENGTH_SHORT).show();
运行结果:
不要在意自己现在有多么的差劲,只要你不放弃你自己,你也会变成优秀的人,努力的去学习,去改变自己,这也是我时刻告诉自己句子,共勉。