Unity Android SDK 集成 常用功能
2020-03-27 本文已影响0人
xzhuan
xzhuan原创稿件,转载请注明出处!
//复制
public String CopyDataToClipboard(String txt){
try {
//获取剪贴板管理器:
ClipboardManager clip = (ClipboardManager)getBaseContext().getSystemService(Context.CLIPBOARD_SERVICE);
// 创建普通字符型ClipData
ClipData data = ClipData.newPlainText(null,txt);//Label是任意文字标签
// 将ClipData内容放到系统剪贴板里。
clip.setPrimaryClip(data);
return "";
}
catch (Exception e){
return e.getMessage();
}
}
//粘贴
public String GetDataFromClipboard(){
ClipboardManager cm = (ClipboardManager) getBaseContext().getSystemService(Context.CLIPBOARD_SERVICE);
String result = "";
ClipData clipData = cm.getPrimaryClip();
//result = cm.toString(); //ClipData{ text/plain "Label"{T:"str"}}; //取出的是ClipData
//result = cm.getText().toString(); //"str" //方法deprecated
ClipData.Item item = clipData.getItemAt(0); //这里获取第一条,也可以用遍历获取任意条
CharSequence charSequence = item.coerceToText(getBaseContext().getApplicationContext());
result = charSequence.toString();
return result;
}
//打开 app(这里以 微信app为例)
//打开微信
public boolean OpenWechatApi() {
return OpenWechatApi("检查到您手机没有安装微信,请安装后使用该功能");
}
public boolean OpenWechatApi(String unInstallHint){
_LocalErrMsg="";
//UnityPlayer.UnitySendMessage("UnityFunc");
String packageName= "com.tencent.mm"
//如果是支付宝就填 "com.eg.android.AlipayGphone",其它 请自行百度查询;
boolean ret = IsAppInstalled(packageName);
try {
if(ret){
Intent intent = new Intent(Intent.ACTION_MAIN);
ComponentName cmp = new ComponentName(packageName,"com.tencent.mm.ui.LauncherUI");
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setComponent(cmp);
startActivity(intent);
}
}
/*catch (ActivityNotFoundException e) {
// TODO: handle exception
_LocalErrMsg=e.getMessage();
ret= false;
}
*/
catch (Exception e){
_LocalErrMsg=e.getMessage();
ret= false;
}
if(!ret && unInstallHint!="")
ShowDialog("提示",unInstallHint);
return true;
}
//根据包名 检查app是否安装
public boolean IsAppInstalled(String packageName){
return new File("/data/data/"+packageName).exists();
}
//保存图片到相册(传全路径文件名、传数据、错误提示内容、成功提示内容)
public boolean SaveImageToGallery(String srcFileName, byte[] srcFileData, String errHint, String successHint,String targetFileName){
_LocalErrMsg="";
try{
if(srcFileData==null){
srcFileData = LoadFileData(srcFileName);
if(srcFileData==null)
return false;
}
File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Pictures/Screenshots");//DCIM/Camera
if(!dir.exists())
dir.mkdir();
File file = new File(dir,targetFileName);
FileOutputStream fos = new FileOutputStream(file);
fos.write(srcFileData,0,srcFileData.length);
fos.flush();
//广播通知相册更新
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri uri = Uri.fromFile(file);
intent.setData(uri);
this.sendBroadcast(intent);
if(successHint!="")
ShowDialog("提示",successHint);//"成功保存到相册");
}
catch (Exception e){
e.printStackTrace();
_LocalErrMsg=e.getMessage();
if(errHint!="")
ShowDialog("提示",errHint);
return false;
}
return true;
}
//保存图片到相册
byte[] LoadFileData(String srcFileName)
{
try {
File file = new File(srcFileName);
FileInputStream fis = new FileInputStream(file);
byte[] srcFileData = new byte[fis.available()];
fis.read(srcFileData, 0, srcFileData.length);
fis.close();
return srcFileData;
}
catch (Exception e) {
_LocalErrMsg = e.getMessage();
return null;
}
}
//系统提示框
public void ShowDialog(final String mTitle, final String mContent) {
runOnUiThread(new Runnable() {
@Override
public void run() {
AlertDialog.Builder mBuilder = new AlertDialog.Builder(ToolActivity.this);
mBuilder.setTitle(mTitle)
.setMessage(mContent)
.setPositiveButton("确定", null);
mBuilder.show();
}
});
}
//分享
//分享文本
public void ShareText(String subject, String body) {
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);//创建一个分享
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);//设置分享类型
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, body);//设置分享内容
startActivity(Intent.createChooser(sharingIntent, "Share via"));//分享标题
}