6.0 Permissions

Source: Internet
Author: User

import Android. Manifest;import Android.app.activity;import Android.app.alertdialog;import Android.content.dialoginterface;import Android.content.pm.packagemanager;import Android.os.bundle;import Android.os.systemclock;import Android.util.Log; Import Android.view.view;import android.widget.button;import Android.widget.toast;import Java.io.File;import Java.io.ioexception;importStaticAndroid.content.ContentValues.TAG; Public classMainactivity extends Activity {PrivateButton Btn3; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Btn3=(Button) Findviewbyid (R.ID.BTN3); Btn3.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {Boolean flag=permissionsutils.haspermission (mainactivity. This, Manifest.permission.WRITE_EXTERNAL_STORAGE); Final string[] Permissions_storage={Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_                External_storage}; if(Flag = =true) {WriteFile (); } Else {                    if(!Shouldshowrequestpermissionrationale (Manifest.permission.WRITE_EXTERNAL_STORAGE)) {Showmessageokcancel ("permission to read the store",                                NewDialoginterface.onclicklistener () {@Override /c6> Public voidOnClick (Dialoginterface Dialog,intwhich) {permissionsutils.requestpermission (mainactivity). This, Permissions_storage);                    }                                }                        ); } Else{permissionsutils.requestpermission (mainactivity). This, Permissions_storage); }                }            }            Private voidshowmessageokcancel (String message, Dialoginterface.onclicklistener oklistener) {NewAlertdialog.builder (mainactivity. This). Setmessage (Message). Setpositivebutton ("OK", Oklistener). Setnegativebutton ("Cancel",NULL). Create (). Show ();    }        }); } @Override Public voidOnrequestpermissionsresult (intRequestcode, string[] permissions,int[] grantresults) {        Switch(requestcode) { CasePermissionsutils.requestcode:if(grantresults[0] ==packagemanager.permission_granted) {                    //Permission grantedWriteFile (); } Else {                    //Permission DeniedToast.maketext ( This,"Deny authorization", Toast.length_short). Show (); }                 Break; default: Super.onrequestpermissionsresult (Requestcode, permissions, grantresults); }    }    Private voidWriteFile () {String fileName="ABC"+ systemclock.currentthreadtimemillis () +". txt"; File File=NewFile ("/storage/emulated/0/"+fileName); Try{file.createnewfile (); if(File.exists ()) {log.i (TAG,"WriteFile:"+File.getabsolutepath ()); }        } Catch(IOException e) {e.printstacktrace (); }    }    Private voidreadFile () {File file=NewFile ("/storage/emulated/0/blank_500_500.png"); if(File.exists ()) {log.i (TAG,"ReadFile:"+File.getabsolutepath ()); }    }}
Import Android.app.activity;import Android.content.pm.packagemanager;import Android.support.v4.app.ActivityCompat; Public classpermissionsutils {FinalStatic intRequestcode =111;  Public Static voidrequestpermission (activity activity, string[] permisssions) {activitycompat.requestpermissions (    Activity, permisssions, Requestcode); }     Public StaticBoolean haspermission (activity activity, String permission) {intPermissioncode =activitycompat.checkselfpermission (activity, permission); if(Permissioncode! =packagemanager.permission_granted) {            return false; } Else {            return true; }    }}

6.0 Permissions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.