欢迎加入Unity业内qq交流群:956187480
qq扫描二维码加群
研究AndroidStudio已经半个月了,就为了写一个给unity用的arr包其实也就是jar包。真的是一路坎坷,鼻青脸肿,问专业做安卓开发的朋友竟然也是一脸懵,即便从github拿来的源码都无从下手,眼看着时间一天天推进日子一天天过,这个问题还不能解决真的是茶不思饭不想。刚好又赶上最近搬家心好累啊,在上海这地方寸土寸金,看来还得找时间让老板加薪,不然老婆就跑了。不扯了入正题,
不知道unity如何跟安卓建立交互的可以去看我之前的博客,在这里就不讲unity安卓交互了。开发过程有问题的欢迎留言,放心绝对会有让你抓狂的各种问题。
博客链接: Unity调用安卓原生的通用前奏(血泪史)
首先在新建的工程下新建一个library类型的Module,具体操作自己摸索很简单。
建一个java类用来管理通讯录
package com.crf.xfd.entrty;
import android.text.TextUtils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ContactBean {
private String name;
private String phoneNumber;
private String email;
public String getName() {
if (TextUtils.isEmpty(name)){
return "";
}
return name;
}
public String getPhoneNumber() {
if (TextUtils.isEmpty(phoneNumber)){
return "";
}
return phoneNumber;
}
public String getEmail() {
if (TextUtils.isEmpty(email)){
return "";
}
return email;
}
public void setEmail(String email) {
if (TextUtils.isEmpty(email)){
this.email="";
}else {
this.email = email;
}
}
public void setPhoneNumber(String phoneNumber) {
if (TextUtils.isEmpty(phoneNumber)){
if (TextUtils.isEmpty(this.phoneNumber)){
this.phoneNumber="";
}
}else {
Pattern p = Pattern.compile("\\s*|\t|\r|\n|");
Matcher m = p.matcher(phoneNumber);
phoneNumber = m.replaceAll("");
phoneNumber = phoneNumber.replaceAll("\\-", "");
if (TextUtils.isEmpty(this.phoneNumber)){
this.phoneNumber=phoneNumber;
}else {
this.phoneNumber+=","+phoneNumber;
}
}
}
public void setName(String name) {
if (TextUtils.isEmpty(name)){
this.name="";
}else {
this.name = name;
}
}
@Override
public String toString() {
return "{\"phone_name\"" + ":\"" +name + "\"," +
"\"phone_number\"" +":\"" +phoneNumber + "\"" + '}';
}
}
然后再来一个类获取遍历手机上的信息
package com.crf.xfd.entrty;
import android.text.TextUtils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ContactBean {
private String name;
private String phoneNumber;
private String email;
public String getName() {
if (TextUtils.isEmpty(name)){
return "";
}
return name;
}
public String getPhoneNumber() {
if (TextUtils.isEmpty(phoneNumber)){
return "";
}
return phoneNumber;
}
public String getEmail() {
if (TextUtils.isEmpty(email)){
return "";
}
return email;
}
public void setEmail(String email) {
if (TextUtils.isEmpty(email)){
this.email="";
}else {
this.email = email;
}
}
public void setPhoneNumber(String phoneNumber) {
if (TextUtils.isEmpty(phoneNumber)){
if (TextUtils.isEmpty(this.phoneNumber)){
this.phoneNumber="";
}
}else {
Pattern p = Pattern.compile("\\s*|\t|\r|\n|");
Matcher m = p.matcher(phoneNumber);
phoneNumber = m.replaceAll("");
phoneNumber = phoneNumber.replaceAll("\\-", "");
if (TextUtils.isEmpty(this.phoneNumber)){
this.phoneNumber=phoneNumber;
}else {
this.phoneNumber+=","+phoneNumber;
}
}
}
public void setName(String name) {
if (TextUtils.isEmpty(name)){
this.name="";
}else {
this.name = name;
}
}
@Override
public String toString() {
return "{\"phone_name\"" + ":\"" +name + "\"," +
"\"phone_number\"" +":\"" +phoneNumber + "\"" + '}';
}
}
最后在跟unity交互的单例类调用
package com.crf.xfd;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat;
import android.widget.Toast;
import com.crf.xfd.entrty.CallLogInfoBean;
import com.crf.xfd.entrty.ContactBean;
import com.crf.xfd.entrty.NativeAppInfoBean;
import com.crf.xfd.tools.CallLogUtils;
import com.crf.xfd.tools.ContactUtil;
import com.crf.xfd.tools.NativeAppUtils;
import com.crf.xfd.tools.PermissionUtil;
import com.unity3d.player.UnityPlayerActivity;
import java.util.List;
public class GetInfoManager extends UnityPlayerActivity{
private int callLogRequestCode = 1;
private int contactRequestCode = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
// 获取本机安装应用的列表
public String GetAppList(){
try {
List allApps = NativeAppUtils.getAllApps(this, 0);
return "{\"app_list\":"+ allApps.toString()+"}";
} catch (Throwable throwable) {
throwable.printStackTrace();
return null;
}
}
//获取本机通讯录
@RequiresApi(api = Build.VERSION_CODES.M)
public String GetContact(){
boolean isReadContact = checkPermission(Manifest.permission.READ_CONTACTS);
if (isReadContact) {
return readContact();
} else {
requestPerssion(new String[]{Manifest.permission.READ_CONTACTS}, contactRequestCode);
return null;
}
}
//读取本机通话记录
@RequiresApi(api = Build.VERSION_CODES.M)
public String GetCallLog(){
boolean b = checkPermission(Manifest.permission.READ_CALL_LOG);
if (b) {
return readCallLog();
} else {
requestPerssion(new String[]{Manifest.permission.READ_CALL_LOG}, callLogRequestCode);
return null;
}
}
/**
* 读取通话记录
*/
private String readCallLog() {
List callLogInfos = CallLogUtils.getCallLogInfos(this, 0);
return "{\"records\":" + callLogInfos.toString()+"}";
}
/**
* 读取本机通讯录信息
*
* @throws Throwable
*/
private String readContact() {
try {
List allContact = ContactUtil.getAllContact(this);
return "{\"phones\":" + allContact.toString() +"}";
} catch (Throwable throwable) {
throwable.printStackTrace();
return null;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == callLogRequestCode) {
readCallLog();
} else if (requestCode == contactRequestCode) {
readContact();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (callLogRequestCode == requestCode) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//已经授权
readCallLog();
} else {
//点击了不再提示,拒绝权限
if (!ActivityCompat.shouldShowRequestPermissionRationale(this, permissions[0])) {
//跳转到设置界面
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivityForResult(intent, callLogRequestCode);
} else {
Toast.makeText(this, "权限拒绝", Toast.LENGTH_SHORT).show();
}
}
} else if (contactRequestCode == requestCode) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//已经授权
readContact();
} else {
//点击了不再提示,拒绝权限
if (!ActivityCompat.shouldShowRequestPermissionRationale(this, permissions[0])) {
//跳转到设置界面
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivityForResult(intent, callLogRequestCode);
} else {
Toast.makeText(this, "权限拒绝", Toast.LENGTH_SHORT).show();
}
}
}
}
/**
* 检查权限
*
* @param permission
* @return
*/
@RequiresApi(api = Build.VERSION_CODES.M)
public boolean checkPermission(String permission) {
boolean b = PermissionUtil.checkPermissionWrapper(this, permission);
return b;
}
/**
* 申请权限
*
* @param permission
* @param requestCode
*/
public void requestPerssion(String[] permission, int requestCode) {
PermissionUtil.requestPermissionsWrapper(this, permission, requestCode);
}
}
期间打包运行后需要在AndroidStudio上开运行输出日志,别笑话我刚开始这个日志输出面板我都找不到
最后打包在手机上运行的截图:
我会把源码跟工程地址直接贴给大家
安卓端工程代码:https://download.csdn.net/download/qq_37310110/10471920
Unity端工程代码:https://download.csdn.net/download/qq_37310110/10471930
二合一最新优化更新简单易懂版本:https://download.csdn.net/download/qq_37310110/10472555
下一篇简单讲解通话记录的获取
欢迎加入Unity业内qq交流群:956187480
qq扫描二维码加群