您的位置:首页 > 移动开发 > Android开发

调用Android系统分享功能(可分享纯文本信息)

2017-09-10 17:32 621 查看
前面有一篇博客说了使用QQ登录并获取用户QQ信息,也实现了分享消息到QQ功能,前面一篇博客使用的是腾讯QQ的开放接口,而且使用腾讯开放接口分享消息到QQ,不能分享纯文本信息,这个看了官方的API就知道了!

下面介绍另一种方法,不用使用什么开放接口,直接使用系统的就可以,并且可以分享纯文本信息!

一、直接将消息分享到QQ:

package com.example.demo3;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

}

public void shareToQQ(View view){
shareQQ(MainActivity.this);
}

public void shareQQ(Context mContext) {
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "这是要分享的消息");
sendIntent.setType("text/plain");
try {
sendIntent.setClassName("com.tencent.mobileqq",
"com.tencent.mobileqq.activity.JumpActivity");
Intent chooserIntent = Intent.createChooser(sendIntent, "选择分享途径");
if (chooserIntent == null) {
return;
}
mContext.startActivity(chooserIntent);
} catch (Exception e) {
mContext.startActivity(sendIntent);
}
}

}


main.xml也就一个按钮,设置了android:onClick="shareToQQ"属性!!!

到此实现了分享纯文本消息到QQ!

二、实现多应用选择分享

public void shareMsg(String activityTitle, String msgTitle, String msgText,
String imgPath) {
Intent intent = new Intent(Intent.ACTION_SEND);
if (imgPath == null || imgPath.equals("")) {
intent.setType("text/plain"); // 纯文本
} else {
File f = new File(imgPath);
if (f != null && f.exists() && f.isFile()) {
intent.setType("image/jpg");
Uri u = Uri.fromFile(f);
intent.putExtra(Intent.EXTRA_STREAM, u);
}
}
intent.putExtra(Intent.EXTRA_SUBJECT, msgTitle);
intent.putExtra(Intent.EXTRA_TEXT, msgText);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(Intent.createChooser(intent, activityTitle));
}


按钮监听直接调用这个方法就可以了!

效果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android qq