安卓后Android应用程序不要求权限

时间:2017-09-23 16:08:59

标签: java android permissions runtime-permissions dangerous-permissions

我创建了一个应用程序,它应该在运行时请求权限。但它不要求任何权限。我可以从设置手动授予权限,但它自己没有请求权限。此外,当点击按钮时,它应该要求权限,并且在授予权限后,它应该显示图像。即使我从设置手动授予权限后,也没有显示图像。我该如何解决这个问题?有人可以帮忙吗?

Bacon1 是包含点击按钮的活动,点击它应该切换到 ActivityHome

Bacon1.java

public class Bacon1 extends Activity {

    private static final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 1;
    private static int RESULT_LOAD_IMAGE = 1;

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


    }

    private void buttonClicked(View view) {
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.READ_EXTERNAL_STORAGE) + ContextCompat
                .checkSelfPermission(this,
                        Manifest.permission.INTERNET)
                != PackageManager.PERMISSION_GRANTED) {

            Snackbar.make(view, "Permission not Granted, Requesting permission.", Snackbar.LENGTH_LONG).show();
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.READ_EXTERNAL_STORAGE)) {

                Snackbar.make(view, "We need permission to internal storage for displaying songs", Snackbar.LENGTH_LONG).show();

            } else {

                Snackbar.make(view, "Allow myapp3 to access this device's internal storage", Snackbar.LENGTH_LONG).show();

                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                        MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);

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


    public void onClick2(View view) {

        buttonClicked(view);
        Intent viewIntent2 = new Intent(this, ActivityHome.class);
        startActivity(viewIntent2);
    }




    public void onClick5(View view) {

        Intent viewIntent1 = new Intent(Intent.ACTION_VIEW);
        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.getExternalStorageDirectory() + "/myapp") + "/" + "downloadedfile.zip");
        viewIntent1.setDataAndType(Uri.fromFile(file), "application/zip");
        startActivity(Intent.createChooser(viewIntent1, null));
    }





    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case 1: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(Bacon1.this, "WRITE_CONTACTS granted", Toast.LENGTH_SHORT)
                            .show();


                } else {

                    Toast.makeText(Bacon1.this, "WRITE_CONTACTS Denied", Toast.LENGTH_SHORT)
                            .show();

                }
                return;
            }

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

ActivityHome.java

public class ActivityHome extends AppCompatActivity implements SlideMenuAdapterInterface{

    private Context mContext;
    private Toolbar toolbar;
    private DrawerLayout Drawer;
    private ActionBarDrawerToggle mDrawerToggle;
    private FragmentManager fragmentManager = null;
    private FragmentTransaction fragmentTransaction = null;
    private Fragment currentFragment=null;

    private ListView slidingList;
    private SlideMenuAdapter mSlideMenuAdapter;
    private int currentPosition=0;

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

         mContext=getApplicationContext();
         initializeActionBar();
         initialCalling();

    }

    @Override
    public void onBackPressed() {
        if(Drawer.isDrawerOpen(Gravity.LEFT)){
            Drawer.closeDrawer(Gravity.LEFT);
        }else{
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void slideRowClickEvent(int postion) {
        if (currentPosition== postion) {
            closeDrware();
            return;
        }
        currentPosition= postion;
        getFragment(postion);
        attachedFragment();
    }

    private void initializeActionBar() {
        toolbar = (Toolbar) findViewById(R.id.tool_bar);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);

        slidingList=(ListView)findViewById(R.id.sliding_listView);
        mSlideMenuAdapter=new SlideMenuAdapter(mContext, getSlideList());
        mSlideMenuAdapter.setSlidemenuadapterinterface(this);
        slidingList.setAdapter(mSlideMenuAdapter);

        Drawer = (DrawerLayout) findViewById(R.id.DrawerLayout);
        mDrawerToggle = new ActionBarDrawerToggle(this, Drawer, toolbar,
                R.string.openDrawer, R.string.closeDrawer) {

            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);

            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
            }

        };
        Drawer.setDrawerListener(mDrawerToggle);
        mDrawerToggle.syncState();


    }

    private void closeDrware(){
        if(Drawer.isDrawerOpen(Gravity.LEFT)){
            Drawer.closeDrawer(Gravity.LEFT);
        }
    }

    private void initialCalling(){
        fragmentManager = getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();

        getFragment(0);
        attachedFragment();
    }



    private void attachedFragment(){
        try {
            if (currentFragment != null) {
                if (fragmentTransaction.isEmpty()) {
                    fragmentTransaction.add(R.id.fragment_container, currentFragment,"" + currentFragment.toString());
                    fragmentTransaction.commit();
                    toolbar.setTitle(title[currentPosition]);
                }else {
                    fragmentTransaction = fragmentManager.beginTransaction();
                    fragmentTransaction.replace(R.id.fragment_container, currentFragment,"" + currentFragment.toString());
                    fragmentTransaction.commit();
                    toolbar.setTitle(title[currentPosition]);
                }

            }
            closeDrware();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private void getFragment(int postion){
        switch (postion) { 
        case 0:
            currentFragment = new GalleryFragment();
            break;
        case 1:
            currentFragment = new CameraFragment();
            break;
        case 2:
            currentFragment = new VideoFragment();
            break;

        default:
            break;
        }
    }


    /**
     * Slide Menu List Array.
     */
    private String[] title={"All Images","Camera","Video"};
    private int[] titleLogo={R.drawable.selector_allpic,R.drawable.selector_camera,R.drawable.selector_video};
    private ArrayList<SlideData> getSlideList(){
        ArrayList<SlideData> arrayList=new ArrayList<SlideData>();
        for (int i = 0; i < title.length; i++) {
            SlideData mSlideData=new SlideData();
            mSlideData.setIcon(titleLogo[i]);
            mSlideData.setName(title[i]);
            mSlideData.setState((i==0)?1:0);
            arrayList.add(mSlideData);
        }
        return arrayList;
    }
}

AndroidManifest:

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

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_MEDIA_STORAGE" />
    <uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL" />
    <uses-permission android:name="android.permission.INTERNET" />


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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Bacon1"
            android:label="activity_bacon1" />
        <activity
            android:name=".activity.ActivityHome"
            android:label="activity_home" />
        <activity
            android:name=".activity.AlbumActivity"
            android:label="@string/app_name">
        </activity>
        <activity
            android:name=".activity.PhotoPreviewActivity"
            android:label="@string/app_name">
        </activity>
    </application>

</manifest>

build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"
    defaultConfig {
        applicationId "com.example.dell_1.copycat2"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    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:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.android.support:design:26.+'
    compile files('libs/universal-image-loader-1.9.2-SNAPSHOT-with-sources.jar')
    testCompile 'junit:junit:4.12'
}

1 个答案:

答案 0 :(得分:0)

首先INTERNET不是dangerous权限,因此没有运行时权限的角色。其次,您不能将两个checkSelfPermission()结果一起添加并期望有用的东西。

删除+ ContextCompat .checkSelfPermission(this, Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED

看看是否能解决你的问题。