According to the "first line of code," hit the code, and then egg, exclamation the rapid progress of technology. Finally remind one point: it books must pay attention to the publication time! Publication time! Publication time! Important thing to say three times
The problem is in the android6.0 permission to get the problem, previously as long as the manifest.xml one-time access can be, android6.0 after the manual to obtain the runtime permissions.
1. The new API
Contextcompact.checkselfpermission ()---> Check for permissions
Activitycompat.requestpermission ()---> To apply for permission, Activitycompat inherits from Contextcompact
Onrequestpermissionresult ()---Request permission is an asynchronous process, so in the callback to process the results, in Onrequestpermissionresult () to get the user authorization, and do the appropriate processing.
Activitycompat.shouldshowrequsetpermissionrationale ()--explain to the user what permissions are used to do, before the user refuses to appear
2. Using the process
3. Example
To call as an example, first register permissions in Manifest.xml
Then you can follow the process in the Mainactivity.java.
1 Packagecom.example.permissionadmin;2 3 ImportAndroid. Manifest;4 Importandroid.app.Activity;5 Importandroid.content.Intent;6 ImportAndroid.content.pm.PackageManager;7 ImportAndroid.net.Uri;8 ImportAndroid.os.Bundle;9 ImportAndroid.support.v4.app.ActivityCompat;Ten ImportAndroid.support.v4.content.ContextCompat; One ImportAndroid.view.View; A ImportAndroid.view.View.OnClickListener; - ImportAndroid.widget.Button; - the Public classMainactivityextendsActivityImplementsonclicklistener{ - - PrivateButton Callphone; - + @Override - protected voidonCreate (Bundle savedinstancestate) { + Super. OnCreate (savedinstancestate); A Setcontentview (r.layout.activity_main); atCallphone=(Button) Findviewbyid (r.id.call_phone); -Callphone.setonclicklistener ( This); - } - - @Override - Public voidOnClick (View v) { in //TODO auto-generated Method Stub - //Request permission If you don't have permission, or call directly to if(Contextcompat.checkselfpermission ( This, Manifest.permission.CALL_PHONE)! = + packagemanager.permission_granted) { - //The second argument is that the array indicates that multiple permissions can be obtained at once, and the subsequent request code and the callback handler function are theActivitycompat.requestpermissions ( This,NewString[]{manifest.permission.call_phone}, 1); *}Else{ $ Docallphone ();Panax Notoginseng } - } the + Public voidDocallphone () { AIntent intent=NewIntent (intent.action_call); theUri Data=uri.parse ("Tel:" + "10086"); + intent.setdata (data); - This. StartActivity (intent); $ } $ - @Override - /** the * Different request codes to correspond to different authorization requests - * Grantresults array: Permission request resultWuyi */ the Public voidOnrequestpermissionsresult (intRequestcode, -string[] Permissions,int[] grantresults) { Wu if(requestcode==1){ - if(grantresults[0]==packagemanager.permission_granted) { About Docallphone (); $}Else{ - //Prompt for user rights not granted - } - } A } +}
4. Spit Groove
Learn the first line of code in the 11th chapter based on the location of the service, according to the above code location is always unable to display, the original thought is a permission problem, and later found that even in accordance with the above is still unable to display their latitude and longitude, so the example changed to call. Now the reason may be: 1. GPS room cannot be located. 2. For the network location, the default is Google, can not be used.
Think of the possible reason is the two, I hope that the great God to answer questions, thank you!!!
Android6.0 Getting permissions