Возврат исключения при выполнении приложения - Android - PullRequest
0 голосов
/ 17 ноября 2018

Запуск программы возвращает это:

E / AndroidRuntime: FATAL EXCEPTION: main Процесс: pt.ubi.di.pdm.expermissions1, PID: 4474 java.lang.RuntimeException: Невозможно запустить действие ComponentInfo {pt.ubi.di.pdm.expermissions1 / pt.ubi.di.pdm.expermissions1.Lastcall}: java.lang.SecurityException: Отказ в разрешении: открытие поставщика com.android. provider.contacts.CallLogProvider из ProcessRecord {212efa7 4474: pt.ubi.di.pdm.expermissions1 / u0a85} (pid = 4474, uid = 10085) требует android.permission.READ_CALL_LOG или android.permission.WRITE_CALL_LOG на android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2913) на android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3048) на android.app.servertransaction.LaunchActivityItem.execute (LaunchActivityItem.java:78) на android.app.servertransaction.TransactionExecutor.executeCallbacks (TransactionExecutor.java:108) на android.app.servertransaction.TransactionExecutor.execute (TransactionExecutor.java:68) на android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1808) на android.os.Handler.dispatchMessage (Handler.java:106) на android.os.Looper.loop (Looper.java:193) на android.app.ActivityThread.main (ActivityThread.java:6669) в java.lang.reflect.Method.invoke (родной метод) в com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run (RuntimeInit.java:493) на com.android.internal.os.ZygoteInit.main (ZygoteInit.java:858) Причина: java.lang.SecurityException: Отказ в разрешении: открытие провайдера com.android.providers.contacts.CallLogProvider из ProcessRecord {212efa7 4474: pt.ubi.di.pdm.expermissions1 / u0a85} (pid = 4474, uid = 10085) требуется android.permission.READ_CALL_LOG или android.permission.WRITE_CALL_LOG на android.os.Parcel.createException (Parcel.java:1942) на android.os.Parcel.readException (Parcel.java:1910) на android.os.Parcel.readException (Parcel.java:1860) на android.app.IActivityManager $ Stub $ Proxy.getContentProvider (IActivityManager.java:4181) на android.app.ActivityThread.acquireProvider (ActivityThread.java:5970) на android.app.ContextImpl $ ApplicationContentResolver.acquireUnstableProvider (ContextImpl.java:2592) на android.content.ContentResolver.acquireUnstableProvider (ContentResolver.java:1828) на android.content.ContentResolver.query (ContentResolver.java:786) на android.content.ContentResolver.query (ContentResolver.java:752) на android.content.ContentResolver.query (ContentResolver.java:710) в pt.ubi.di.pdm.expermissions1.Lastcall.onCreate (Lastcall.java:19) на android.app.Activity.performCreate (Activity.java:7136) на android.app.Activity.performCreate (Activity.java:7127) на android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1271) на android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2893) на android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3048) на android.app.servertransaction.LaunchActivityItem.execute (LaunchActivityItem.java:78) на android.app.servertransaction.TransactionExecutor.executeCallbacks (TransactionExecutor.java:108) на android.app.servertransaction.TransactionExecutor.execute (TransactionExecutor.java:68) на android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1808) на android.os.Handler.dispatchMessage (Handler.java:106) на android.os.Looper.loop (Looper.java:193) на android.app.ActivityThread.main (ActivityThread.java:6669) в java.lang.reflect.Method.invoke (родной метод) в com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run (RuntimeInit.java:493)на com.android.internal.os.ZygoteInit.main (ZygoteInit.java:858) Вызывается: android.os.RemoteException: трассировка удаленного стека: в com.android.server.am.ActivityManagerService.getContentProviderImpl (ActivityManagerService.java:12188) в com.android.server.am.ActivityManagerService.getContentProvider (ActivityManagerService.java:12585) на android.app.IActivityManager $ Stub.onTransact (IActivityManager.java:357) на com.android.server.am.ActivityManagerService.onTransact (ActivityManagerService.java:3291) на android.os.Binder.execTransact (Binder.java:731)

Код activity_lastcall.xml :

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Lastcall">

    <TextView
        android:id="@+id/lc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

Код LastCall.java :

package pt.ubi.di.pdm.expermissions1;

import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CallLog;
import android.widget.TextView;

public class Lastcall extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lastcall);
        TextView oTV = (TextView)findViewById(R.id.lc);
        String strUriCalls = "content://call_log/calls";
        Uri uriCalls=Uri.parse(strUriCalls);
        Cursor curCalls = getContentResolver().query(uriCalls ,null ,null ,null , null);
        String sInfo = "Last Call:";


        if (curCalls.moveToLast())
            sInfo += curCalls.getString(curCalls.getColumnIndex(CallLog.Calls.NUMBER))+"\n" +
                    "Duration:"+curCalls.getString(curCalls.getColumnIndex(CallLog.Calls.DURATION));
        oTV.setText(sInfo);

    }
}

Код AndroidManifest.xml :

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="pt.ubi.di.pdm.expermissions1">
    <uses-permission
        android:name="android.permission.READ_CALL_LOG"></uses-permission>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".Lastcall">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Мне нужно сделать последний звонок и написать номер и имя в текстовом виде.

1 Ответ

0 голосов
/ 17 ноября 2018

добавьте это также к вашему manifest.xml

<uses-permission
        android:name="android.permission.WRITE_CALL_LOG"></uses-permission>

Вы должны запросить это во время выполнения, как показано ниже. Этот код должен быть выполнен перед выполнением любой задачи, связанной с разрешением вызова.

if (ContextCompat.checkSelfPermission(thisActivity,Manifest.permission.READ_CALL_LOG)!= PackageManager.PERMISSION_GRANTED) {
    // Permission is not granted
     if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
            Manifest.permission.READ_CALL_LOG)) {

    } else {
        // No explanation needed; request the permission
        ActivityCompat.requestPermissions(thisActivity,
                new String[]{Manifest.permission.READ_CALL_LOG},
                MY_PERMISSIONS_READ_CALL_LOG);
    }
}

Это разрешение на чтение журнала вызовов и, при необходимости, также и разрешение записи журнала вызовов.

...