1

希望不是一種愚蠢的問題,但我的應用程序工作,直到兩天前,但之後,我得到OTA更新升級到棉花糖。之後,不僅我無法獲取我的聯繫人列表,甚至顯示聯繫人列表的默認設備應用程序也不會顯示任何聯繫人。我從Play商店下載了幾個Contact Management應用程序,甚至不顯示任何東西。我甚至不能發送/接收短信:)我想知道wtf正在發生。在棉花糖中訪問聯繫人列表是否改變了?

好的,讓我砍掉b.s並分享我所做的。 我根據Marshmallow需要的新動態權限請求更改了代碼。我啓動應用程序,並授予訪問聯繫人,但沒有顯示:(

這是我的代碼,對不起,我知道很長,但很簡單:

public class MainActivity extends AppCompatActivity 
{ 
    private static final String TAG = MainActivity.class.getSimpleName(); 
    private static final int MY_PERMISSIONS_REQUEST_READ_CONTACTS = 10; 

    private SwipeRefreshLayout mRefreshLayout; 
    private RecyclerView mRecyclerView; 
    private ContactsAdapter mAdapter; 
    private RecyclerView.LayoutManager mLayoutManager; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 

     Log.d(TAG, "********************************"); 
     Log.d(TAG, "*** Contacts Remover Started ***"); 
     Log.d(TAG, "********************************"); 

     setContentView(R.layout.activity_main); 

     mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView); 
     mRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.refreshScreen); 
     mRefreshLayout.setOnRefreshListener(this::refreshLayout); 
     mRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright, 
       android.R.color.holo_green_light, 
       android.R.color.holo_orange_light, 
       android.R.color.holo_red_light); 

     // use this setting to improve performance if you know that changes 
     // in content do not change the layout size of the RecyclerView 
     mRecyclerView.setHasFixedSize(true); 

     // use a linear layout manager 
     mLayoutManager = new LinearLayoutManager(this); 
     mRecyclerView.setLayoutManager(mLayoutManager); 

     // create adapter 
     mAdapter = new ContactsAdapter(); 
     mRecyclerView.setAdapter(mAdapter); 

     // Check required permissions 
     checkContactPermission(); 
    } 

    private void checkContactPermission() 
    { 
     Log.d(TAG, "Yu"); 
     if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED || 
      ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_CONTACTS) != PackageManager.PERMISSION_GRANTED) 
     { 
      // Should we show an explanation? 
      if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_CONTACTS)) 
      { 
       // Show an explanation to the user *asynchronously* -- don't block 
       // this thread waiting for the user's response! After the user 
       // sees the explanation, try again to request the permission. 
      } 
      else 
      { 
       // No explanation needed, we can request the permission. 
       ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CONTACTS, 
         Manifest.permission.WRITE_CONTACTS}, MY_PERMISSIONS_REQUEST_READ_CONTACTS); 
      } 
     } 
     else 
     { 
      Log.d(TAG, "Perms granted"); 
      // Get Contacts and display it 
      refreshLayout(); 
     } 
    } 

    @Override 
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) 
    { 
     switch (requestCode) 
     { 
      case MY_PERMISSIONS_REQUEST_READ_CONTACTS: 
      { 
       // If request is cancelled, the result arrays are empty. 
       if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) 
       { 
        // Get Contacts and display it 
        refreshLayout(); 
       } 
       else 
       { 
        this.finish(); 
       } 
       return; 
      } 

      // other 'case' lines to check for other 
      // permissions this app might request 
     } 
    } 

    private List<Contact> getContactList() 
    { 
     Log.d(TAG, "2"); 
     List<Contact> contactList = new ArrayList<>(100); 

     ContentResolver cr = getContentResolver(); 
     Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); 
     Log.d(TAG, "3"); 
     if (cur != null && cur.getCount() > 0) 
     { 
      int i = 0; 
      Log.d(TAG, "4"); 
      while (cur.moveToNext()) 
      { 
       i++; 
       Log.d(TAG, "-> " + i); 

       Contact contact = new Contact(); 
       contact.setId(cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID))); 
       contact.setName(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME))); 
       contact.setImageAddress(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI))); 

       contactList.add(contact); 
      } 

      cur.close(); 
      Log.d(TAG, "5"); 
     } 

     Log.d(TAG, String.format("Number of %s contacts found", contactList.size())); 
     return contactList; 
    } 

    private Observable<Contact> getApps() 
    { 
     Log.d(TAG, "1"); 
     return Observable.create(subscriber -> { 
      List<Contact> contactList = getContactList(); 
      Log.d(TAG, "6"); 

      if (subscriber.isUnsubscribed()) 
      { 
       return; 
      } 

      Log.d(TAG, "7"); 

      for (Contact contact : contactList) 
      { 
       subscriber.onNext(contact); 
      } 

      Log.d(TAG, "8"); 

      if (!subscriber.isUnsubscribed()) 
      { 
       subscriber.onCompleted(); 
      } 

      Log.d(TAG, "9"); 
     }); 
    } 

    private void refreshLayout() 
    { 
     Log.d(TAG, "0"); 
     if (mRefreshLayout.isRefreshing()) 
     { 
      return; 
     } 

     mRefreshLayout.setRefreshing(true); 

     getApps().toSortedList() 
       .subscribeOn(Schedulers.io()) 
       .observeOn(AndroidSchedulers.mainThread()) 
       .subscribe(new Observer<List<Contact>>() 
       { 
        @Override 
        public void onCompleted() 
        { 
         Log.d(TAG, "10"); 
         Toast.makeText(MainActivity.this, "List updated!", Toast.LENGTH_LONG).show(); 
         mRefreshLayout.setRefreshing(false); 
        } 

        @Override 
        public void onError(Throwable e) 
        { 
         Log.d(TAG, "ERROR!!!"); 
         Toast.makeText(MainActivity.this, "Shit! Something went wrong :(", Toast.LENGTH_LONG).show(); 
         mRefreshLayout.setRefreshing(false); 
        } 

        @Override 
        public void onNext(List<Contact> contactList) 
        { 
         Log.d(TAG, "*"); 
         mAdapter.setContactList(contactList); 
         mAdapter.notifyDataSetChanged(); 
        } 
       }); 
    } 
} 

最後我的日誌:

10-15 01:05:46.239 1514-1514/? D/MainActivity: ******************************** 
10-15 01:05:46.239 1514-1514/? D/MainActivity: *** Contacts Remover Started *** 
10-15 01:05:46.239 1514-1514/? D/MainActivity: ******************************** 
10-15 01:05:46.285 1514-1514/? D/MainActivity: Yu 
10-15 01:05:46.287 1514-1514/? D/MainActivity: Perms granted 
10-15 01:05:46.287 1514-1514/? D/MainActivity: 0 
10-15 01:05:46.288 1514-1514/? D/MainActivity: 1 
10-15 01:05:46.306 1514-14799/? D/MainActivity: 2 

出事了這一行,Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);,沒有錯誤,不死機,不報告!只是什麼:(

+0

入住這https://developer.android.com/training/permissions/requesting.html – Sunny

+0

@Sunny,正是你說的是哪一部分,是啊,我讀了設計過程中一個與工作正常基於我的日誌。 – Hesam

回答

0

你似乎是在浪費你的時間調整你的代碼時,d手機上的默認聯繫人應用程序也顯示沒有聯繫人。

它看起來好像OTA更新已要麼完成了某種局部恢復出廠設置,或者或許僅僅下降了先前提供您的設備聯繫人的帳戶。首先排序,所以默認的應用程序顯示一些聯繫人,我懷疑你的應用程序也將開始顯示聯繫人。

+0

是的,出廠重置後,幸運的是我的設備再次設置爲棉花糖,而不是KitKat。但知道每一件事情都很好,這兩個問題就解決了。 – Hesam