研究AndroidStudio已經半個月了,就為了寫一個給unity用的arr包其實也就是jar包。真的是一路坎坷,鼻青臉腫,問專業做安卓開發的朋友竟然也是一臉懵,即便從github拿來的源碼都無從下手,眼看着時間一天天推進日子一天天過,這個問題還不能解決真的是茶不思飯不想。剛好又趕上最近搬家心好累啊,在上海這地方寸土寸金,看來還得找時間讓老闆加薪,不然老婆就跑了。不扯了入正題,
不知道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<NativeAppInfoBean> 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<CallLogInfoBean> callLogInfos = CallLogUtils.getCallLogInfos(this, 0);
return "{\"records\":" + callLogInfos.toString()+"}";
}
/**
* 讀取本機通訊錄資訊
*
* @throws Throwable
*/
private String readContact() {
try {
List<ContactBean> 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上開運作輸出日志,别笑話我剛開始這個日志輸出面闆我都找不到
最後打包在手機上運作的截圖: