【问题标题】:getting ANR creating contact through my app when any contact app is not installed在未安装任何联系人应用程序时通过我的应用程序获取 ANR 创建联系人
【发布时间】:2018-05-04 22:12:37
【问题描述】:

我正在通过我的应用程序创建联系人。但如果手机中没有任何联系人应用程序,那么它会给出 ANR。如何检查手机中是否安装了任何联系人应用程序。

Intent intent = new Intent(ContactsContract.Intents.Insert.ACTION);
intent.setType(ContactsContract.RawContacts.CONTENT_TYPE);
intent.putExtra(ContactsContract.Intents.Insert.PHONE, contactNumber)
      .putExtra(ContactsContract.Intents.Insert.NAME, contactName);

【问题讨论】:

  • 为什么不使用内容提供者来创建联系人?它不需要安装联系人应用程序
  • 你能提供任何代码sn-p - @Sagar

标签: android


【解决方案1】:

当没有安装应用来解决你的意图时调用catch块,你可以在块上通知用户

try {
  startActivity(intent);
} catch (ActivityNotFoundException ex) {
  //Do something
}

【讨论】:

    【解决方案2】:

    您可以使用 ContentProvider 来创建联系人,而不是使用 Intent。这不需要在您的设备上安装联系人的应用程序。你可以关注这个SO 这样做。

    【讨论】:

      【解决方案3】:
      • 出现 ANR 对话框是因为您执行的操作耗时超过 5 主线程中的第二个。所以你应该在后台线程中做。
      • 你应该试试这个。
      • 联系人选择器方法:

      例子:

      public void contactIntent(View v){
      
                  Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
                  startActivityForResult(contactPickerIntent, RESULT_PICK_CONTACT);
              }
      
          @Override
          protected void onActivityResult(int requestCode, int resultCode, Intent data) {
              // check whether the result is ok
              if (resultCode == RESULT_OK) {
                  // Check for the request code, we might be usign multiple startActivityForReslut
                  switch (requestCode) {
                  case RESULT_PICK_CONTACT:
                      contactPicked(data);
                      break;
                  }
              } else {
                  Log.e("MainActivity", "Failed to pick contact");
              }
          }
          /**
           * Query the Uri and read contact details. Handle the picked contact data.
           * @param data
           */
          private void contactPicked(Intent data) {
              Cursor cursor = null;
              try {
                  String phoneNo = null ;
                  String name = null;
                  // getData() method will have the Content Uri of the selected contact
                  Uri uri = data.getData();
                  //Query the content uri
                  cursor = getContentResolver().query(uri, null, null, null, null);
                  cursor.moveToFirst();
                  // column index of the phone number
                  int  phoneIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                  // column index of the contact name
                  int  nameIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
                  phoneNo = cursor.getString(phoneIndex);
                  name = cursor.getString(nameIndex);
                  // Set the value to the textviews
                  textView1.setText(name);
                  textView2.setText(phoneNo);
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-02-20
        • 1970-01-01
        • 2017-01-16
        • 2013-04-12
        • 2012-02-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多