2017-07-28 95 views
4

試圖做簡單的APP將記錄插入到電話簿中。 它看起來像我沒有權限問題,但系統不會讓反正創造紀錄:android sdk 23檢測權限問題(java.lang.SecurityException)

07-28 18:11:44.799 2304 10616 I UpdateIcingCorporaServi: Updating corpora: APPS=com.example.aero.myapplication, CONTACTS=MAYBE 
07-28 18:11:45.362 2304 10648 I UpdateIcingCorporaServi: Updating corpora: APPS=com.example.aero.myapplication.test, CONTACTS=MAYBE 
07-28 18:11:45.651 10663 10678 I GrantPermissionCallable: Permission: android.permission.WRITE_CONTACTS is already granted! 
07-28 18:11:46.255 10663 10678 I OK  : contact Permission to record allowed 
07-28 18:11:46.255 10663 10678 D Info : Creating contact: 123456789 
07-28 18:11:46.259 2097 2114 E DatabaseUtils: java.lang.SecurityException: Proxy package com.android.providers.contacts from uid 10001 or calling package com.example.aero.myapplication.test from uid 10097 not allowed to perform WRITE_CONTACTS 
07-28 18:11:46.260 10663 10678 E Error : Exception encountered while inserting contact: java.lang.SecurityException: Proxy package com.android.providers.contacts from uid 10001 or calling package com.example.aero.myapplication.test from uid 10097 not allowed to perform WRITE_CONTACTS 

什麼想法?爲什麼這個神祕? 一次我運行程序的時候,安裝2 APK:

$ adb push /Users/aero/AndroidStudioProjects/MyApplication/app/build/outputs/apk/app-debug.apk /data/local/tmp/com.example.aero.myapplication 
$ adb shell pm install -r "/data/local/tmp/com.example.aero.myapplication" 

$ adb push /Users/aero/AndroidStudioProjects/MyApplication/app/build/outputs/apk/app-debug-androidTest.apk /data/local/tmp/com.example.aero.myapplication.test 
$ adb shell pm install -r "/data/local/tmp/com.example.aero.myapplication.test" 

只有com.example.aero.myapplication在系統權限,com.example.aero.myapplication.test沒有按」沒有任何權限。我失去了一點點。

package com.example.aero.myapplication; 


import android.Manifest; 
import android.app.Instrumentation; 
import android.content.ContentProviderOperation; 
import android.content.Context; 
import android.content.Intent; 
import android.content.pm.PackageManager; 
import android.os.Bundle; 
import android.os.Environment; 
import android.provider.ContactsContract; 
import android.support.test.InstrumentationRegistry; 
import android.support.test.filters.SdkSuppress; 
import android.support.test.rule.GrantPermissionRule; 
import android.support.test.runner.AndroidJUnit4; 
import android.support.test.uiautomator.By; 
import android.support.test.uiautomator.UiDevice; 
import android.support.v4.app.ActivityCompat; 
import android.support.v7.app.AppCompatActivity; 
import android.util.Log; 

import org.junit.Before; 
import org.junit.Rule; 
import org.junit.runner.RunWith; 

import java.util.ArrayList; 

import static org.hamcrest.CoreMatchers.notNullValue; 
import static org.junit.Assert.assertThat; 


@RunWith(AndroidJUnit4.class) 
public class MainActivity{ 

    @Rule 
    public GrantPermissionRule permissionRule = GrantPermissionRule.grant(Manifest.permission.WRITE_CONTACTS); 

    public Instrumentation instrumentation; 
    private UiDevice mDevice; 
    private static final int RECORD_REQUEST_CODE = 101; 


    @Before 
    public void before() { 
     // Initialize UiDevice instance 
     mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); 
     //instrumentation = InstrumentationRegistry.getInstrumentation(); 
     assertThat(mDevice, notNullValue()); 
     // Start from the home screen 
     mDevice.pressHome(); 
    } 

    @org.junit.Test 
    public void test() throws InterruptedException { 
     Context context = InstrumentationRegistry.getInstrumentation().getContext(); 
     String NAME = "TEST RECORD"; 
     String NMBR = "123456789"; 
     CreatePhoneBookEntry(context, NAME, NMBR); 
    } 

    public void CreatePhoneBookEntry(Context context, String NAME, String NMBR) { 
     //ActivityCompat.requestPermissions(context, 
     //  new String[]{Manifest.permission.WRITE_CONTACTS}, 
     //  RECORD_REQUEST_CODE) 


     int permission = ActivityCompat.checkSelfPermission(context, 
       Manifest.permission.WRITE_CONTACTS); 

     if (permission != PackageManager.PERMISSION_GRANTED) { 
      Log.i("Error", "contact Permission to record denied"); 
     } else { 
      Log.i("OK", "contact Permission to record allowed"); 
      }; 

     /* 
    * Gets values from the UI 
    */ 
     // Creates a new array of ContentProviderOperation objects. 
     ArrayList<ContentProviderOperation> ops = 
       new ArrayList<ContentProviderOperation>(); 

    /* 
    * Creates a new raw contact with its account type (server type) and account name 
    * (user's account). Remember that the display name is not stored in this row, but in a 
    * StructuredName data row. No other data is required. 
    */ 
     ContentProviderOperation.Builder op = 
       ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI) 
         .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null) 
         .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null); 

     // Builds the operation and adds it to the array of operations 
     ops.add(op.build()); 

     // Creates the display name for the new raw contact, as a StructuredName data row. 
     op = 
       ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) 
      /* 
      * withValueBackReference sets the value of the first argument to the value of 
      * the ContentProviderResult indexed by the second argument. In this particular 
      * call, the raw contact ID column of the StructuredName data row is set to the 
      * value of the result returned by the first operation, which is the one that 
      * actually adds the raw contact row. 
      */ 
         .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) 

         // Sets the data row's MIME type to StructuredName 
         .withValue(ContactsContract.Data.MIMETYPE, 
           ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE) 

         // Sets the data row's display name to the name in the UI. 
         .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, NAME); 

     // Builds the operation and adds it to the array of operations 
     ops.add(op.build()); 

     // Inserts the specified phone number and type as a Phone data row 
     op = 
       ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) 
      /* 
      * Sets the value of the raw contact id column to the new raw contact ID returned 
      * by the first operation in the batch. 
      */ 
         .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) 

         // Sets the data row's MIME type to Phone 
         .withValue(ContactsContract.Data.MIMETYPE, 
           ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE) 

         // Sets the phone number and type 
         .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, NMBR) 
         .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, "MOBILE"); 

     // Builds the operation and adds it to the array of operations 
     ops.add(op.build()); 
     // Ask the Contacts Provider to create a new contact 
     Log.d("Info", "Creating contact: " + NMBR); 

    /* 
    * Applies the array of ContentProviderOperation objects in batch. The results are 
    * discarded. 
    */ 
     try { 
      context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops); 
     } catch (Exception e) { 
      // Log exception 
      Log.e("Error", "Exception encountered while inserting contact: " + e); 
     } 
    } 
} 

回答

0

碰到同樣的問題,但爲不同的權限。

對我來說,修復很簡單,我已經忘了添加<uses-permission android:name="android.permission.THE_PERMISSION" />AndroidManifest.xml

因此,也許你應該確保你所需要的<uses-permission.../><uses-feature.../>在那裏。

希望它有幫助..

+0

這也沒有幫助,任何你可能會做的不同嗎? –

相關問題