解决错误::撤销权限android.permission.CALL_PHONE

Posted

技术标签:

【中文标题】解决错误::撤销权限android.permission.CALL_PHONE【英文标题】:solve the error:: revoked permission android.permission.CALL_PHONE 【发布时间】:2016-04-06 08:16:48 【问题描述】:

如何解决这个错误:

撤销权限 android.permission.CALL_PHONE

这是我的 Java 代码:

public class DetailContactActivity extends AppCompatActivity implements
OnClickListener 

    private TextView    phone, email;
    private int         id;
    private ImageView   call, btnEmail;
    private ContactDB   db;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail_contact);

        db = ContactDB.getInstance(this);

        phone = (TextView) findViewById(R.id.txt_phone_number);
        email = (TextView) findViewById(R.id.txt_email);
        call = (ImageView) findViewById(R.id.btn_call);
        btnEmail = (ImageView) findViewById(R.id.btn_email);

        Bundle b = getIntent().getExtras();
        if (b != null)
        
            id = b.getInt("id");
            phone.setText(b.getString("phone"));
            getSupportActionBar().setTitle(b.getString("nama"));
            if (!b.getString("email").equals("null"))
            
                email.setText(b.getString("email"));
             else
            
                email.setText("");
            

        

        call.setOnClickListener(this);
        btnEmail.setOnClickListener(this);
    

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    
        getMenuInflater().inflate(R.menu.detail_contact, menu);
        return true;
    

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    
        switch (item.getItemId())
        
            case R.id.action_edit:
                Bundle b = new Bundle();
                b.putInt("id", id);
                b.putString("name", getSupportActionBar().getTitle().toString());
                b.putString("email", email.getText().toString());
                b.putString("phone", phone.getText().toString());

                Intent i = new Intent(this, EditActivity.class);
                i.putExtras(b);
                startActivity(i);
                finish();
                return true;
            case R.id.action_delete:
                showSettingsAlert();
                return true;
            default:
                return super.onOptionsItemSelected(item);

        

    

    @Override
    public void onClick(View v)
    
        if (v == btnEmail)
        
            if (!email.getText().toString().equals(""))
            
                Intent emailIntent = new Intent(Intent.ACTION_SENDTO,
                        Uri.fromParts("mailto", email.getText().toString(),
                                null));

                startActivity(Intent
                        .createChooser(emailIntent, "Send email..."));

            

         else if (v == call)
        
            Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:" + phone.getText().toString()));
            startActivity(callIntent);

            //Uri call = Uri.parse("tel:" + phone.getText().toString());             
            //Intent surf = new Intent(Intent.ACTION_CALL, call); 
            //startActivity(surf);
        

    

    private void showSettingsAlert()
    
        AlertDialog.Builder alert = new AlertDialog.Builder(this);

        alert.setTitle("Delete");
        alert.setMessage("Contact akan dihapus");

        alert.setPositiveButton("Ya", new DialogInterface.OnClickListener() 

            @Override
            public void onClick(DialogInterface dialog, int which)
            
                db.deleteContact(id);
                startActivity(new Intent(DetailContactActivity.this,
                        MainActivity.class));
                finish();
            
        );
        alert.setNegativeButton("Tidak", new DialogInterface.OnClickListener() 

            @Override
            public void onClick(DialogInterface dialog, int which)
            
                dialog.cancel();
            
        );

        alert.show();

    

    @Override
    public void onBackPressed()
    
        super.onBackPressed();
        startActivity(new Intent(DetailContactActivity.this, MainActivity.class));
        finish();
    

错误:

01-02 02:05:41.493: D/AndroidRuntime(2927): Shutting down VM
01-02 02:05:41.493: D/AndroidRuntime(2927): --------- beginning of crash
01-02 02:05:41.616: E/AndroidRuntime(2927): FATAL EXCEPTION: main
01-02 02:05:41.616: E/AndroidRuntime(2927): Process: com.example.phonedb, PID: 2927
01-02 02:05:41.616: E/AndroidRuntime(2927): java.lang.SecurityException: Permission Denial: starting Intent  act=android.intent.action.CALL dat=tel:xxxxxxxxxx cmp=com.android.server.telecom/.components.UserCallActivity  from ProcessRecordf5320c4 2927:com.example.phonedb/u0a69 (pid=2927, uid=10069) with revoked permission android.permission.CALL_PHONE
01-02 02:05:41.616: E/AndroidRuntime(2927):     at android.os.Parcel.readException(Parcel.java:1599)
01-02 02:05:41.616: E/AndroidRuntime(2927):     at android.os.Parcel.readException(Parcel.java:1552)
01-02 02:05:41.616: E/AndroidRuntime(2927):     at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:2658)
01-02 02:05:41.616: E/AndroidRuntime(2927):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1507)
01-02 02:05:41.616: E/AndroidRuntime(2927):     at android.app.Activity.startActivityForResult(Activity.java:3917)
01-02 02:05:41.616: E/AndroidRuntime(2927):     at android.app.Activity.startActivityForResult(Activity.java:3877)
01-02 02:05:41.616: E/AndroidRuntime(2927):     at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:748)
01-02 02:05:41.616: E/AndroidRuntime(2927):     at android.app.Activity.startActivity(Activity.java:4200)
01-02 02:05:41.616: E/AndroidRuntime(2927):     at android.app.Activity.startActivity(Activity.java:4168)
01-02 02:05:41.616: E/AndroidRuntime(2927):     at com.example.phonedb.DetailContactActivity.onClick(DetailContactActivity.java:127)
01-02 02:05:41.616: E/AndroidRuntime(2927):     at android.view.View.performClick(View.java:5198)
01-02 02:05:41.616: E/AndroidRuntime(2927):     at android.view.View$PerformClick.run(View.java:21147)
01-02 02:05:41.616: E/AndroidRuntime(2927):     at android.os.Handler.handleCallback(Handler.java:739)
01-02 02:05:41.616: E/AndroidRuntime(2927):     at android.os.Handler.dispatchMessage(Handler.java:95)
01-02 02:05:41.616: E/AndroidRuntime(2927):     at android.os.Looper.loop(Looper.java:148)
01-02 02:05:41.616: E/AndroidRuntime(2927):     at android.app.ActivityThread.main(ActivityThread.java:5417)
01-02 02:05:41.616: E/AndroidRuntime(2927):     at java.lang.reflect.Method.invoke(Native Method)
01-02 02:05:41.616: E/AndroidRuntime(2927):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
01-02 02:05:41.616: E/AndroidRuntime(2927):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

这是mainfest文件,希望帮助::

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.phonedb"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="23" />

<uses-permission-sdk-m android:name="android.permission.READ_CONTACTS" />
<uses-permission-sdk-m android:name="android.permission.WRITE_CONTACTS" />
 <uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.phonedb.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".AddContactActivity"
        android:label="@string/title_activity_add_contact" >
    </activity>
    <activity
        android:name="com.example.phonedb.DetailContactActivity"
        android:label="@string/title_activity_detail_contact" >
    </activity>
    <activity
        android:name=".EditActivity"
        android:label="@string/title_activity_edit" >
    </activity>
</application>

【问题讨论】:

【参考方案1】:

切换到 DIAL_PHONE,因为这不需要权限。

只需替换这一行

Intent callIntent = new Intent(Intent.ACTION_CALL);

Intent callIntent = new Intent(Intent.ACTION_DIAL);

【讨论】:

【参考方案2】:

Android 6 (SDK 23) 允许用户撤销应用的权限。我想,这就是这里发生的事情。您的应用必须能够应对这种情况。

详情请查看documentation。

特别是:

如果设备运行的是 Android 6.0 或更高版本,并且您的应用的目标 SDK 为 23 或更高版本:应用必须在清单中列出权限,并且必须在应用运行时请求它需要的每个危险权限。用户可以授予或拒绝每个权限,即使用户拒绝权限请求,应用程序也可以继续以有限的功能运行。

在运行时请求权限是described in detail here。

【讨论】:

我也是这样想的,但是不知道怎么用,怎么获取权限 我试了好难,不能用,有没有别的解决办法,能不能把版本改成23以下,请帮忙 我应该把代码放在哪里来请求权限@Henry 最好的就是在您需要它之前。这让用户清楚为什么请求权限。【参考方案3】:

在清单文件中添加此权限:

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

或者也试试这个。

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

CALL_PRIVILEGED 允许应用程序拨打任何电话号码, 包括紧急号码,无需通过拨号器用户 用户确认呼叫的界面。 不适用于第三方应用程序。

【讨论】:

你在哪里添加了权限?你能发布android清单文件吗? @Haneen 编辑您的问题并粘贴您的 Menifest.xml 如果您想解决问题。 我试过但它给了我一个错误(权限只授予系统应用程序)【参考方案4】:

我迟到了。您必须动态请求权限。

    // Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
                Manifest.permission.READ_CONTACTS)
        != PackageManager.PERMISSION_GRANTED) 

    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
            Manifest.permission.READ_CONTACTS)) 

        // 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(thisActivity,
                new String[]Manifest.permission.READ_CONTACTS,
                MY_PERMISSIONS_REQUEST_READ_CONTACTS);

        // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
        // app-defined int constant. The callback method gets the
        // result of the request.
    

更多详情https://developer.android.com/training/permissions/requesting.html

【讨论】:

【参考方案5】:

请更改 call_phone 以拨打电话。因为你需要拨号 号码。

Intent callIntent=new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel:1234567890"));
startActivity(callIntent);

【讨论】:

以上是关于解决错误::撤销权限android.permission.CALL_PHONE的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Android 中使用自定义权限?

android:撤销权限

如何在google-apps-script上恢复已撤销的权限?

启动时的运行时权限

Android:权限拒绝:以撤销权限启动 Intent android.permission.CAMERA

如何获得Android的崩溃日志