真的没有人能解答这个奇怪的问题吗 50C

如题,我的app想早每次resume(在oncreate也不行)的时候检查权限,如果没有权限就询问,但是他并没有询问的那个弹窗这是为啥呢

我尝试过,把CALL_PHONE 换成READ_EXTERNAL_STORAGE,它就可以显示提示允许还是拒绝读写内存的那个权限提示。这是为什么呢?我调试的设备是小米5an
droid7.0的
下面 是关键代码

 package com.example.administrator.myapplication;



import android.Manifest;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import java.util.Arrays;


import static android.content.pm.PackageManager.PERMISSION_GRANTED;


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (Build.VERSION.SDK_INT >= 23) {
            if (PERMISSION_GRANTED == ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE)) {
                //has permission, do operation directly
                Toast.makeText(this,"alreadyge",Toast.LENGTH_SHORT).show();
            } else {
                //do not have permission
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, 11);
                Toast.makeText(this,"2",Toast.LENGTH_SHORT).show();
            }
        }
    }





}

下面是配置文件

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.administrator.myapplication">
    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
    <uses-permission android:name="android.permission.CALL_PHONE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        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>
    </application>

</manifest>

下面是build.gradle

 apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.example.administrator.myapplication"
        minSdkVersion 17
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    testCompile 'junit:junit:4.12'
}

5个回答

/**
*
*/
package neusoft.sxt.com.permission;

import android.Manifest;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;

import java.util.ArrayList;
import java.util.List;

import neusoft.sxt.com.R;

/**

  • 继承了Activity,实现Android6.0的运行时权限检测
  • 需要进行运行时权限检测的Activity可以继承这个类 *
  • @author xingtao sun
  • @创建时间:2016年5月27日 下午3:01:31
  • @项目名称: 安卓6.0运行时权限
    /
    public class CheckPermissionsActivity extends AppCompatActivity
    implements
    ActivityCompat.OnRequestPermissionsResultCallback {
    /
    *

    • 需要进行检测的权限数组 */ protected String[] needPermissions = { Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.READ_PHONE_STATE };

    private static final int PERMISSON_REQUESTCODE = 0;

    /**

    • 判断是否需要检测,防止不停的弹框 */ private boolean isNeedCheck = true;

    @Override
    protected void onResume() {

    if (isNeedCheck) {
        checkPermissions(needPermissions);
    }
    super.onResume();
    

    }

    /**
    *
    */
    protected void checkPermissions(String... permissions) {
    List needRequestPermissonList = findDeniedPermissions(permissions);
    if (null != needRequestPermissonList
    && needRequestPermissonList.size() > 0) {
    ActivityCompat.requestPermissions(this,
    needRequestPermissonList.toArray(
    new String[needRequestPermissonList.size()]),
    PERMISSON_REQUESTCODE);
    }
    }

    /**

    • 获取权限集中需要申请权限的列表 *
    • @param permissions
    • @return
    • @since 2.5.0 */ private List findDeniedPermissions(String[] permissions) { List needRequestPermissonList = new ArrayList(); for (String perm : permissions) { if (ContextCompat.checkSelfPermission(this, perm) != PackageManager.PERMISSION_GRANTED || ActivityCompat.shouldShowRequestPermissionRationale( this, perm)) { needRequestPermissonList.add(perm); } } return needRequestPermissonList; }

    /**

    • 检测是否有的权限都已经授权 *
    • @param grantResults */ protected boolean verifyPermissions(int[] grantResults) { for (int result : grantResults) { if (result != PackageManager.PERMISSION_GRANTED) { return false; } } return true; }

    @Override
    public void onRequestPermissionsResult(int requestCode,
    String[] permissions, int[] paramArrayOfInt) {
    if (requestCode == PERMISSON_REQUESTCODE) {
    if (!verifyPermissions(paramArrayOfInt)) {
    showMissingPermissionDialog();
    isNeedCheck = false;
    }
    }
    }

    /**

    • 显示提示信息
      */
      private void showMissingPermissionDialog() {
      AlertDialog.Builder builder = new AlertDialog.Builder(this);
      builder.setTitle(R.string.notifyTitle);
      builder.setMessage(R.string.notifyMsg);

      // 拒绝, 退出应用
      builder.setNegativeButton(R.string.cancel,
      new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {
      finish();
      }
      });

      builder.setPositiveButton(R.string.setting,
      new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {
      startAppSettings();
      }
      });

      builder.setCancelable(false);

      builder.show();
      }

    /**

    • 启动应用的设置 *
    • @since 2.5.0 */ private void startAppSettings() { Intent intent = new Intent( Settings.ACTION_APPLICATION_DETAILS_SETTINGS); intent.setData(Uri.parse("package:" + getPackageName())); startActivity(intent); }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
    this.finish();
    return true;
    }
    return super.onKeyDown(keyCode, event);
    }

}

现在Android7.0系统对于系统权限要求很高了,要写成动态权限,只在清单文件中没用了

EncoreLaw
EncoreLaw 我已经动态申请了啊,你应该没看我代码把?
3 年多之前 回复

还是没人能解决,服气

少侠用andpermession框架一键获取权限

EncoreLaw
EncoreLaw 例如呢,我用了permissionsdispatcher也一样不行
3 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐