Unity獲取手機通話記錄《二》安卓端
阿新 • • 發佈:2019-02-12
上一篇簡單記錄了一下unity獲取通訊錄的過程,這一篇咱們就瞭解一下通話記錄跟app列表的獲取,其實大體流程跟上一篇的獲取通訊錄一樣的,只不過程式碼不一樣而已,時間因素我就不詳細解說直接上程式碼有疑問的可以在下面留言我看到了能幫就會幫。
我會把原始碼跟工程地址直接貼給大家
獲取通話記錄:
最後輸出型別是json資料,如果大家有特殊需求的話可以自定義
package com.crf.xfd.entrty; public class CallLogInfoBean { private int id; private String name; // 名稱 private String number; // 號碼 private String date; // 日期 private int type; // 來電:1,撥出:2,未接:3 private int count; // 通話次數 private long duration;//通話時長 public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public int getType() { return type; } public void setType(int type) { this.type = type; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } public long getDuration() { return duration; } public void setDuration(long duration) { this.duration = duration; } @Override public String toString() { return "{\"call_no\":" + "\""+ number + "\"," + "\"call_date\":" +"\""+ date + "\"," +"\"call_type\":" +"\""+ type +"\"," + "\"duration\":" +"\""+ duration +"\"," + "\"call_name\":" + "\""+name + "\"," + "\"count\":" +"\""+ count +"\"}"; } }
package com.crf.xfd.tools; import android.content.ContentResolver; import android.content.Context; import android.database.Cursor; import android.net.Uri; import android.provider.CallLog; import com.crf.xfd.entrty.CallLogInfoBean; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; public class CallLogUtils { // private static final Uri uri = CallLog.Calls.CONTENT_URI; private static final Uri uri = Uri.parse("content://call_log/calls"); private static final String[] projection = { CallLog.Calls.DATE, // 日期 CallLog.Calls.NUMBER, // 號碼 CallLog.Calls.TYPE, // 型別 CallLog.Calls.CACHED_NAME, // 名字 CallLog.Calls._ID, // id CallLog.Calls.DURATION//通話時長 }; public static List<CallLogInfoBean> getCallLogInfos(Context context, long lastPostTime) { List<CallLogInfoBean> callLogInfos = new ArrayList<>(); SimpleDateFormat sfd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //獲取ContentResolver ContentResolver contentResolver = context.getContentResolver(); if (lastPostTime == 0) { lastPostTime = System.currentTimeMillis() - 90 * 24 * 60 * 60 * 1000l; } Cursor cursor = contentResolver.query(uri, projection, null, null, CallLog.Calls.DEFAULT_SORT_ORDER); if (cursor != null && cursor.getCount() > 0) { Date date; cursor.moveToFirst(); for (int i = 0; i < cursor.getCount(); i++) { cursor.moveToPosition(i); if (lastPostTime > cursor.getLong(cursor .getColumnIndex(CallLog.Calls.DATE))) { continue; } date = new Date(cursor.getLong(cursor .getColumnIndex(CallLog.Calls.DATE))); String number = cursor.getString(cursor .getColumnIndex(CallLog.Calls.NUMBER)); int type = cursor.getInt(cursor .getColumnIndex(CallLog.Calls.TYPE)); String cachedName = cursor.getString(cursor .getColumnIndex(CallLog.Calls.CACHED_NAME)); int id = cursor.getInt(cursor .getColumnIndex(CallLog.Calls._ID)); long duration = cursor.getLong(cursor .getColumnIndex(CallLog.Calls.DURATION)); CallLogInfoBean callLog = new CallLogInfoBean(); callLog.setId(id); callLog.setNumber(number); callLog.setName(cachedName); callLog.setDuration(duration); if (null == cachedName || "".equals(cachedName)) { callLog.setName(number); } callLog.setType(type); callLog.setDate(sfd.format(date)); callLogInfos.add(callLog); } } return callLogInfos; } }
跟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); } }
上一篇忘了帖AndroidManifest的程式碼如下
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.crf.xfd" >
<!--讀取手機通話記錄許可權-->
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<!-- 讀聯絡人許可權 -->
<uses-permission android:name="android.permission.READ_CONTACTS" />
<!--讀取簡訊資訊的許可權-->
<uses-permission android:name="android.permission.READ_SMS" />
<application
android:theme="@android:style/Theme.NoTitleBar" >
<activity android:name=".GetInfoManager" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="unityplayer.UnityActivity"
android:value="true" />
</activity>
</application>
</manifest>
最後打包執行截圖
我會把原始碼跟工程地址直接貼給大家
下一篇簡單講解app列表的獲取