赞
踩
当我想做一个智能的记事本的时候,我就在尝试自己写一组分享功能。后来才知道,原来每个社交软件中都有自己的分享接口。
这就大大减少了我们的代码量了。
第一种方法:特点--简单
package com.example.share;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/* 创建菜单 */
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 0, 0, "分享");
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 0:
// intent.setType("text/plain"); //纯文本
/*
* 图片分享 it.setType("image/png"); //添加图片 File f = new
* File(Environment.getExternalStorageDirectory()+"/name.png");
*
* Uri uri = Uri.fromFile(f); intent.putExtra(Intent.EXTRA_STREAM,
* uri);
*/
Intent intent=new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_SUBJECT, "Share");
intent.putExtra(Intent.EXTRA_TEXT, "I have successfully share my message through my app");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(Intent.createChooser(intent, getTitle()));
return true;
}
return false;
}
}
效果图:
这种方式仅仅是调用了系统中自带的分享功能。如果希望分享功能更加的抢到,涉及到的范围更加的广的话,那不得不说一说ShareSDK包了
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。