Android分享功能
16lz
2021-12-04
package com.ghstudio.sharetest; import java.util.List; import android.app.Activity; import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; public class Main extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } /* 创建菜单 */ public boolean onCreateOptionsMenu(Menu menu) { menu.add(0,0,0,"分享"); return true; } public boolean onOptionsItemSelected(MenuItem item){ switch (item.getItemId()){ case 0: Intent intent=new Intent(Intent.ACTION_SEND); //intent.setType("text/plain"); //纯文本 /*图片分享 it.setType("image/png"); //添加图片 File f = new File(Environment.getExternalStorageDirectory() +"/Pictures/2.png"); Uri u = Uri.fromFile(f); it.putExtra(Intent.EXTRA_STREAM, u); */ intent.putExtra(Intent.EXTRA_SUBJECT, "分享"); intent.putExtra(Intent.EXTRA_TEXT, "I would like to share this with you..."); startActivity(Intent.createChooser(intent, getTitle())); return true; } return false; } }
Android分享功能的代码解析:首先创建一个Options菜单,该菜单只有一个项“分享”。当点击菜单项时,创建一个Intent。该Intent设置为发送给支持ACTION_SEND的Activity。用两个putExtra给Intent设置了SUBJECT和TEXT的数据,再用startActivity方法让系统调用适当的Activity执行之。createChooser方法接受Intent做参数,也同时接纳了Intent里面要求的filter(ACTION_SEND),只有支持ACTION_SEND的Activity才会被列入可选列表。
Share via Facebook
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND); shareIntent.setType("text/plain"); shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Content to share"); PackageManager pm = v.getContext().getPackageManager(); List activityList = pm.queryIntentActivities(shareIntent, 0); for (final ResolveInfo app : activityList) { if ((app.activityInfo.name).contain("facebook")) { final ActivityInfo activity = app.activityInfo; final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name); shareIntent.addCategory(Intent.CATEGORY_LAUNCHER); shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); shareIntent.setComponent(name); v.getContext().startActivity(shareIntent); break; } }
Bonus - Share via Twitter
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND); shareIntent.setType("text/plain"); shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Content to share"); PackageManager pm = v.getContext().getPackageManager(); List activityList = pm.queryIntentActivities(shareIntent, 0); for (final ResolveInfo app : activityList) { if ("com.twitter.android.PostActivity".equals(app.activityInfo.name)) { final ActivityInfo activity = app.activityInfo; final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name); shareIntent.addCategory(Intent.CATEGORY_LAUNCHER); shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); shareIntent.setComponent(name); v.getContext().startActivity(shareIntent); break; } }
更多相关文章
- Android(安卓)Studio报错:Unsupported method: AndroidProject.ge
- android QQ好友分享
- android(6) 扇形菜单实现
- Android(安卓)ontouch 手速
- Android常用的第三方开源库和框架
- android使用web加载网页的js问题
- Android(安卓)Adapter
- 2011.09.23——— android sample之Notepad(context menu)
- 程序小白----AndroidStudio之飞机大战