【问题标题】:StartActivity call requires permissions problemsStartActivity 调用需要权限问题
【发布时间】:2016-09-10 13:22:16
【问题描述】:

I follow this video tutorial 以前没有问题。

今天我再试一次,但还是有问题。This picture

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.fooddeliverycaller">


<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

<uses-sdk
    android:minSdkVersion="15"
    android:targetSdkVersion="24" />


<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <meta-data android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version"/>

    <activity android:name="com.google.android.gms.ads.AdActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
</application>

【问题讨论】:

标签: android android-intent phone-call


【解决方案1】:

您需要添加(运行时)权限检查。从 Android API 23 及更高版本开始,您需要在运行时请求执行某些任务(例如:调用、位置信息...)的权限。为此,只需单击以下代码:

startActivity(callIntent);

转到代码左侧的红色警告三角形/标志。单击它,然后单击添加权限检查。现在,当您运行您的应用程序时,会弹出一个“对话框”,并要求用户允许该应用程序拨打电话。

希望这会有所帮助。有关更多信息,请参阅此链接:

Android permission checks

或查看这个先前回答的问题(由@CommonsWare 建议):

Android permission doesn't work even if I have declared it

【讨论】:

    【解决方案2】:

    你应该使用

    Intent.ACTION_DIAL 
    

    而不是 Intent.ACTION_CALL。 Intent.ACTION_DIAL 使用您传递的号码预先填充拨号器。它不需要许可。

    希望它能解决问题!

    【讨论】:

      【解决方案3】:

      在 API 23 之后,用户必须明确批准任何可能访问用户机密数据的权限。此类权限包含Calendar, Camera, Contact, Location, Microphone, Phone, Sensor, SMS, Storage.

      以下是我从Documentation 获取的代码,并进行了一些修改。

      private void callNumber() {
      
          if (ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE)
                  != PackageManager.PERMISSION_GRANTED) {
              // Should we show an explanation?
              if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                      Manifest.permission.CALL_PHONE)) {
                    // Show an expanation 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.CALL_PHONE},
                          MY_PERMISSIONS_REQUEST_READ_CONTACTS);
      
              }
          } else {
              //This is for lower version <23
              Intent callIntent = new Intent(Intent.ACTION_DIAL);
              callIntent.setData(Uri.parse("tel:" + MyCellNumber);
              startActivity(callIntent);
          }
      }
      

      然后覆盖下面的方法

      @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) {
      
                      Intent callIntent = new Intent(Intent.ACTION_DIAL);
                      callIntent.setData(Uri.parse("tel:" + MyCellNumber));
                      startActivity(callIntent);
                      // permission was granted, yay! Do the
                      // contacts-related task you need to do.
                  } else {
                      // permission denied, boo! Disable the
                      // functionality that depends on this permission.
                  }
                  return;
              }
      
              // other 'case' lines to check for other
              // permissions this app might request
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-23
        • 2019-03-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多