这个类不是我的
工具栏来源
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.Toast;
import java.io.IOException;
import okhttp3.MediaType;
import okhttp3.Request;
import okio.Buffer;
/**
* Created by wanglei on 2016/12/11.
*/
public class AppKit {
public static void copyToClipBoard(Context context, String text) {
ClipboardManager cm = (ClipboardManager) context.getSystemService(
Context.CLIPBOARD_SERVICE);
cm.setPrimaryClip(ClipData.newPlainText("xdroid_copy", text));
Toast.makeText(context, "复制成功", Toast.LENGTH_SHORT).show();
}
public static void openInBrowser(Context context, String url) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.parse(url);
intent.setData(uri);
if (intent.resolveActivity(context.getPackageManager()) != null) {
context.startActivity(intent);
} else {
Toast.makeText(context, "打开失败了,没有可打开的应用", Toast.LENGTH_SHORT).show();
}
}
public static void shareText(Context context, String shareText) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "分享");
intent.putExtra(Intent.EXTRA_TEXT, shareText);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(Intent.createChooser(intent, "分享"));
}
public static void shareImage(Context context, Uri uri) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.setType("image/jpeg");
context.startActivity(Intent.createChooser(shareIntent, "分享图片"));
}
public static boolean isText(MediaType mediaType) {
if (mediaType.type() != null && mediaType.type().equals("text")) {
return true;
}
if (mediaType.subtype() != null) {
if (mediaType.subtype().equals("json") ||
mediaType.subtype().equals("xml") ||
mediaType.subtype().equals("html") ||
mediaType.subtype().equals("webviewhtml")
)
return true;
}
return false;
}
public static String bodyToString(final Request request) {
try {
final Request copy = request.newBuilder().build();
final Buffer buffer = new Buffer();
copy.body().writeTo(buffer);
return buffer.readUtf8();
} catch (final IOException e) {
return "something error when show requestBody.";
}
}
}
使用方法
//复制浏览器URL
AppKit.copyToClipBoard(this, webView.getUrl());
//调用浏览器打开
AppKit.openInBrowser(this, webView.getUrl());
//调用系统分享功能
AppKit.shareText(this, webView.getTitle() + " " + webView.getUrl() + " 来自「XDroid」");