java 如何访问Android 11(API 30)?

new9mtju  于 12个月前  发布在  Java
关注(0)|答案(2)|浏览(190)

我需要访问Android(API 30)CALL命令,从我的应用程序,我有下面的方法,但抛出异常,需要permisson:android.permission.READ_CALL_PROJECTandroid.permission.WRITE_CALL_PROJECT


的数据
这就是启示录

<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />    
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission android:name="android.permission.WRITE_CALL_LOG" />

字符串
这就是Java函数:

public static String obtenerDetallesLlamadas(Context context) {

    StringBuffer stringBuffer = new StringBuffer();
    Cursor cursor = context.getContentResolver().query(CallLog.Calls.CONTENT_URI,
            null, null, null, CallLog.Calls.DATE + " DESC");
    int number = cursor.getColumnIndex(CallLog.Calls.NUMBER);
    int type = cursor.getColumnIndex(CallLog.Calls.TYPE);
    int date = cursor.getColumnIndex(CallLog.Calls.DATE);
    int duration = cursor.getColumnIndex(CallLog.Calls.DURATION);
    while (cursor.moveToNext()) {
        String phNumber = cursor.getString(number);
        String callType = cursor.getString(type);
        String callDate = cursor.getString(date);
        Date callDayTime = new Date(Long.valueOf(callDate));
        String callDuration = cursor.getString(duration);
        String dir = null;
        int dircode = Integer.parseInt(callType);
        switch (dircode) {
            case CallLog.Calls.OUTGOING_TYPE:
                dir = "OUTGOING";
                break;
            case CallLog.Calls.INCOMING_TYPE:
                dir = "INCOMING";
                break;

            case CallLog.Calls.MISSED_TYPE:
                dir = "MISSED";
                break;
        }
        stringBuffer.append("\nPhone Number:--- " + phNumber + " \nCall Type:--- "
                + dir + " \nCall Date:--- " + callDayTime
                + " \nCall duration in sec :--- " + callDuration);
        stringBuffer.append("\n----------------------------------");
    }
    cursor.close();
    return stringBuffer.toString();
}

wgx48brx

wgx48brx1#

有了这个许可证与下面的方法Android阅读清单和工程从Android 6+

public static boolean comprobar_Permisos_CALL_LOG(Context myContext, Activity myActivity){

    try {

            // Check whether this app has the call log allowed
            int writeExternalStoragePermission = ContextCompat.checkSelfPermission(myContext, Manifest.permission.READ_CALL_LOG);
            if(writeExternalStoragePermission!= PackageManager.PERMISSION_GRANTED)
            {
                // Request user to grant write external storage permission.
                ActivityCompat.requestPermissions(myActivity, new String[]{
                        Manifest.permission.READ_CALL_LOG
                }, REQUEST_CODE_CALL_LOG_PERMISSION);

                if (Build.VERSION.SDK_INT == Build.VERSION_CODES.R ) {
                    try {
                        Intent intent = new Intent();
                        Uri uri = Uri.fromParts("package", myActivity.getPackageName(), null);
                        intent.setData(uri);
                        myActivity.startActivityForResult(intent,REQUEST_CODE_CALL_LOG_PERMISSION);
                        return false;
                    } catch (Exception e) {
                        return false; //if anything needs adjusting it would be this
                    }
                } else {
                    return false;
                }

            }else {

                return true;
            }
    }catch (Exception ex)
    {
        Log.e(LOG_TAG_EXTERNAL_STORAGE, ex.getMessage(), ex);
    }

    return false;
}

字符串

wf82jlnq

wf82jlnq2#

在清单文件中添加此权限。
第一个月
将此函数添加到Activity文件中,并在oncreate函数中调用它。

public void getCallLogs() {
    StringBuilder callLogs = new StringBuilder();

    ArrayList<String> calllogsBuffer = new ArrayList<String>();
    calllogsBuffer.clear();
    Cursor managedCursor = managedQuery(CallLog.Calls.CONTENT_URI,
            null, null, null, null);
    int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
    int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
    int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
    int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
    while (managedCursor.moveToNext()) {
        String phNumber = managedCursor.getString(number);
        String callType = managedCursor.getString(type);
        String callDate = managedCursor.getString(date);
        Date callDayTime = new Date(Long.valueOf(callDate));
        String callDuration = managedCursor.getString(duration);
        String dir = null;
        int dircode = Integer.parseInt(callType);
        switch (dircode) {
            case CallLog.Calls.OUTGOING_TYPE:
                dir = "OUTGOING";
                break;
            case CallLog.Calls.INCOMING_TYPE:
                dir = "INCOMING";
                break;
            case CallLog.Calls.MISSED_TYPE:
                dir = "MISSED";
                break;
        }
        calllogsBuffer.add("\nPhone Number: " + phNumber + " \nCall Type: "
                + dir + " \nCall Date: " + callDayTime
                + " \nCall duration in sec : " + callDuration + "\n");
        Log.d("CALL_LOGs" , "\nPhone Number: " + phNumber + " \nCall Type: "
                + dir + " \nCall Date: " + callDayTime
                + " \nCall duration in sec : " + callDuration + "\n");
    }
    managedCursor.close();
}

字符串

相关问题