Beruflich Dokumente
Kultur Dokumente
EverythingeveryAndroidDevelopermustknowaboutnewAndroid'sRuntimePermission::TheCheeseFactory
(/blog/things-you-need-to-know-about-android-mpermission-developer-edition/en)
(/blog/things-you-need-to-
know-about-android-m-permission-developer-edition/th)
Android M's name was just announced officially days ago. The final
version is almost thereand would be released not so long.
Although Android is being keep developedbut the latest update to
Android M is totally different since there is some major change that
would change everything like new Runtime Permission. Surprisingly
it is not much talked about in Android Developer community even
though it is extremely important and may cause somebig trouble in
the near future.
That's the reason why Idecide to blog about this topic today.
Everything you needto know about this new Runtime Permission
including how to implement it in your code. Let's do it before it's too
late.
http://inthecheesefactory.com/blog/thingsyouneedtoknowaboutandroidmpermissiondeveloperedition/en
1/29
1/11/2016
EverythingeveryAndroidDevelopermustknowaboutnewAndroid'sRuntimePermission::TheCheeseFactory
http://inthecheesefactory.com/blog/thingsyouneedtoknowaboutandroidmpermissiondeveloperedition/en
2/29
1/11/2016
EverythingeveryAndroidDevelopermustknowaboutnewAndroid'sRuntimePermission::TheCheeseFactory
http://inthecheesefactory.com/blog/thingsyouneedtoknowaboutandroidmpermissiondeveloperedition/en
3/29
1/11/2016
EverythingeveryAndroidDevelopermustknowaboutnewAndroid'sRuntimePermission::TheCheeseFactory
You might already feel like there is some cold wind blowing through
your arms ... If you are an Android Developer, you will suddenly
know that programming logic is totally changed. You cannot just call
a function to do the job like previous but you have to check for the
permission for every single feature or your application will just
simply crash !
Correct. I would not spoil you that it is easy. Although it is a great
thingfor user but it is truly nightmare for us developer. We have to
take coding to the next level or it will surely have a problem in both
short-term and long-term.
http://inthecheesefactory.com/blog/thingsyouneedtoknowaboutandroidmpermissiondeveloperedition/en
4/29
1/11/2016
EverythingeveryAndroidDevelopermustknowaboutnewAndroid'sRuntimePermission::TheCheeseFactory
Anyway this new Runtime Permission will work like described only
when we set the application's targetSdkVersion to 23 which mean it
is declared that applicationhas already been tested on API Level 23.
And this feature will work only on Android 6.0 Marshmallow. The
same app will run with same old behavior on pre-Marshmallow
device.
http://inthecheesefactory.com/blog/thingsyouneedtoknowaboutandroidmpermissiondeveloperedition/en
5/29
1/11/2016
EverythingeveryAndroidDevelopermustknowaboutnewAndroid'sRuntimePermission::TheCheeseFactory
http://inthecheesefactory.com/blog/thingsyouneedtoknowaboutandroidmpermissiondeveloperedition/en
6/29
1/11/2016
EverythingeveryAndroidDevelopermustknowaboutnewAndroid'sRuntimePermission::TheCheeseFactory
http://inthecheesefactory.com/blog/thingsyouneedtoknowaboutandroidmpermissiondeveloperedition/en
7/29
1/11/2016
EverythingeveryAndroidDevelopermustknowaboutnewAndroid'sRuntimePermission::TheCheeseFactory
8/29
1/11/2016
EverythingeveryAndroidDevelopermustknowaboutnewAndroid'sRuntimePermission::TheCheeseFactory
Warning: Right now when you create a new project in Android Studio.
targetSdkVersion will be automatically set to the latest version, 23. If you
are not ready to make your application fully support the Runtime
Permission, I suggest you to step downthe targetSdkVersion to 22 first.
9/29
1/11/2016
EverythingeveryAndroidDevelopermustknowaboutnewAndroid'sRuntimePermission::TheCheeseFactory
android.permission.KILL_BACKGROUND_PROCESSES
android.permission.MODIFY_AUDIO_SETTINGS
android.permission.NFC
android.permission.READ_SYNC_SETTINGS
android.permission.READ_SYNC_STATS
android.permission.RECEIVE_BOOT_COMPLETED
android.permission.REORDER_TASKS
android.permission.REQUEST_INSTALL_PACKAGES
android.permission.SET_TIME_ZONE
android.permission.SET_WALLPAPER
android.permission.SET_WALLPAPER_HINTS
android.permission.SUBSCRIBED_FEEDS_READ
android.permission.TRANSMIT_IR
android.permission.USE_FINGERPRINT
android.permission.VIBRATE
android.permission.WAKE_LOCK
android.permission.WRITE_SYNC_SETTINGS
com.android.alarm.permission.SET_ALARM
com.android.launcher.permission.INSTALL_SHORTCUT
com.android.launcher.permission.UNINSTALL_SHORTCUT
10/29
1/11/2016
1
2
3
4
5
6
7
8
9
EverythingeveryAndroidDevelopermustknowaboutnewAndroid'sRuntimePermission::TheCheeseFactory
android{
compileSdkVersion23
...
defaultConfig{
...
targetSdkVersion23
...
}
privatestaticfinalStringTAG="Contacts";
privatevoidinsertDummyContact(){
//Twooperationsareneededtoinsertanewcontact.
ArrayList<ContentProviderOperation>operations=newArrayList<Conte
//First,setupanewrawcontact.
ContentProviderOperation.Builderop=
ContentProviderOperation.newInsert(ContactsContract.RawConta
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME
operations.add(op.build());
//Next,setthenameforthecontact.
op=ContentProviderOperation.newInsert(ContactsContract.Data.CONTEN
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.StructuredName.CONT
.withValue(ContactsContract.CommonDataKinds.StructuredName.D
"__DUMMYCONTACTfromruntimepermissionssample"
operations.add(op.build());
//Applytheoperations.
ContentResolverresolver=getContentResolver();
try{
resolver.applyBatch(ContactsContract.AUTHORITY,operations);
}catch(RemoteExceptione){
Log.d(TAG,"Couldnotaddanewcontact:"+e.getMessage());
}catch(OperationApplicationExceptione){
Log.d(TAG,"Couldnotaddanewcontact:"+e.getMessage());
}
}
http://inthecheesefactory.com/blog/thingsyouneedtoknowaboutandroidmpermissiondeveloperedition/en
11/29
1/11/2016
EverythingeveryAndroidDevelopermustknowaboutnewAndroid'sRuntimePermission::TheCheeseFactory
<usespermissionandroid:name="android.permission.WRITE_CONTACTS"/>
http://inthecheesefactory.com/blog/thingsyouneedtoknowaboutandroidmpermissiondeveloperedition/en
12/29
1/11/2016
EverythingeveryAndroidDevelopermustknowaboutnewAndroid'sRuntimePermission::TheCheeseFactory
13/29
1/11/2016
EverythingeveryAndroidDevelopermustknowaboutnewAndroid'sRuntimePermission::TheCheeseFactory
finalprivateintREQUEST_CODE_ASK_PERMISSIONS=123;
privatevoidinsertDummyContactWrapper(){
inthasWriteContactsPermission=checkSelfPermission(Manifest.permis
if(hasWriteContactsPermission!=PackageManager.PERMISSION_GRANTED)
requestPermissions(newString[]{Manifest.permission.WRITE_CONTA
REQUEST_CODE_ASK_PERMISSIONS);
return;
}
insertDummyContact();
}
http://inthecheesefactory.com/blog/thingsyouneedtoknowaboutandroidmpermissiondeveloperedition/en
14/29
1/11/2016
EverythingeveryAndroidDevelopermustknowaboutnewAndroid'sRuntimePermission::TheCheeseFactory
@Override
publicvoidonRequestPermissionsResult(intrequestCode,String[]permiss
switch(requestCode){
caseREQUEST_CODE_ASK_PERMISSIONS:
if(grantResults[0]==PackageManager.PERMISSION_GRANTED){
//PermissionGranted
insertDummyContact();
}else{
//PermissionDenied
Toast.makeText(MainActivity.this,"WRITE_CONTACTSDenied
.show();
}
break;
default:
super.onRequestPermissionsResult(requestCode,permissions,g
}
}
http://inthecheesefactory.com/blog/thingsyouneedtoknowaboutandroidmpermissiondeveloperedition/en
15/29
1/11/2016
EverythingeveryAndroidDevelopermustknowaboutnewAndroid'sRuntimePermission::TheCheeseFactory
http://inthecheesefactory.com/blog/thingsyouneedtoknowaboutandroidmpermissiondeveloperedition/en
16/29
1/11/2016
EverythingeveryAndroidDevelopermustknowaboutnewAndroid'sRuntimePermission::TheCheeseFactory
finalprivateintREQUEST_CODE_ASK_PERMISSIONS=123;
privatevoidinsertDummyContactWrapper(){
inthasWriteContactsPermission=checkSelfPermission(Manifest.permis
if(hasWriteContactsPermission!=PackageManager.PERMISSION_GRANTED)
if(!shouldShowRequestPermissionRationale(Manifest.permissio
showMessageOKCancel("YouneedtoallowaccesstoContact
newDialogInterface.OnClickListener(){
@Override
publicvoidonClick(DialogInterfacedialog,
requestPermissions(newString[]{Manifes
REQUEST_CODE_ASK_PERMISSIONS);
}
});
return;
}
requestPermissions(newString[]{Manifest.permission.WRITE_CONTA
REQUEST_CODE_ASK_PERMISSIONS);
return;
}
insertDummyContact();
}
privatevoidshowMessageOKCancel(Stringmessage,DialogInterface.OnClick
newAlertDialog.Builder(MainActivity.this)
.setMessage(message)
.setPositiveButton("OK",okListener)
.setNegativeButton("Cancel",null)
.create()
.show();
}
http://inthecheesefactory.com/blog/thingsyouneedtoknowaboutandroidmpermissiondeveloperedition/en
17/29
1/11/2016
EverythingeveryAndroidDevelopermustknowaboutnewAndroid'sRuntimePermission::TheCheeseFactory
The result are rational dialog will be shown when this permission is
requested for the first time and also be shown if user has ever
marked that permission as Never ask again. For the latter case,
onRequestPermissionsResult will be called with PERMISSION_DENIED
Done !
http://inthecheesefactory.com/blog/thingsyouneedtoknowaboutandroidmpermissiondeveloperedition/en
18/29
1/11/2016
EverythingeveryAndroidDevelopermustknowaboutnewAndroid'sRuntimePermission::TheCheeseFactory
finalprivateintREQUEST_CODE_ASK_MULTIPLE_PERMISSIONS=124;
privatevoidinsertDummyContactWrapper(){
List<String>permissionsNeeded=newArrayList<String>();
finalList<String>permissionsList=newArrayList<String>();
if(!addPermission(permissionsList,Manifest.permission.ACCESS_FINE_
permissionsNeeded.add("GPS");
if(!addPermission(permissionsList,Manifest.permission.READ_CONTACT
permissionsNeeded.add("ReadContacts");
if(!addPermission(permissionsList,Manifest.permission.WRITE_CONTAC
permissionsNeeded.add("WriteContacts");
if(permissionsList.size()>0){
if(permissionsNeeded.size()>0){
//NeedRationale
Stringmessage="Youneedtograntaccessto"+permission
for(inti=1;i<permissionsNeeded.size();i++)
message=message+","+permissionsNeeded.get(i);
showMessageOKCancel(message,
newDialogInterface.OnClickListener(){
@Override
publicvoidonClick(DialogInterfacedialog,
requestPermissions(permissionsList.toArray(
REQUEST_CODE_ASK_MULTIPLE_PERMISSION
}
});
return;
}
requestPermissions(permissionsList.toArray(newString[permission
REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
return;
}
insertDummyContact();
}
privatebooleanaddPermission(List<String>permissionsList,Stringpermi
if(checkSelfPermission(permission)!=PackageManager.PERMISSION_GRA
permissionsList.add(permission);
//CheckforRationaleOption
if(!shouldShowRequestPermissionRationale(permission))
returnfalse;
}
45
returntrue;
http://inthecheesefactory.com/blog/thingsyouneedtoknowaboutandroidmpermissiondeveloperedition/en
19/29
1/11/2016
45
46
EverythingeveryAndroidDevelopermustknowaboutnewAndroid'sRuntimePermission::TheCheeseFactory
returntrue;
}
When every single permission got its grant result, the result will be
sent to the same callback method, onRequestPermissionsResult . I
use HashMap to make source codelooks cleaner and more
readable.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
@Override
publicvoidonRequestPermissionsResult(intrequestCode,String[]permiss
switch(requestCode){
caseREQUEST_CODE_ASK_MULTIPLE_PERMISSIONS:
{
Map<String,Integer>perms=newHashMap<String,Integer>();
//Initial
perms.put(Manifest.permission.ACCESS_FINE_LOCATION,PackageM
perms.put(Manifest.permission.READ_CONTACTS,PackageManager.
perms.put(Manifest.permission.WRITE_CONTACTS,PackageManager
//Fillwithresults
for(inti=0;i<permissions.length;i++)
perms.put(permissions[i],grantResults[i]);
//CheckforACCESS_FINE_LOCATION
if(perms.get(Manifest.permission.ACCESS_FINE_LOCATION)==P
&&perms.get(Manifest.permission.READ_CONTACTS)==P
&&perms.get(Manifest.permission.WRITE_CONTACTS)==
//AllPermissionsGranted
insertDummyContact();
}else{
//PermissionDenied
Toast.makeText(MainActivity.this,"SomePermissionisDe
.show();
}
}
break;
default:
super.onRequestPermissionsResult(requestCode,permissions,g
}
}
http://inthecheesefactory.com/blog/thingsyouneedtoknowaboutandroidmpermissiondeveloperedition/en
20/29
1/11/2016
EverythingeveryAndroidDevelopermustknowaboutnewAndroid'sRuntimePermission::TheCheeseFactory
if(Build.VERSION.SDK_INT>=23){
//Marshmallow+
}else{
//PreMarshmallow
}
http://inthecheesefactory.com/blog/thingsyouneedtoknowaboutandroidmpermissiondeveloperedition/en
21/29
1/11/2016
EverythingeveryAndroidDevelopermustknowaboutnewAndroid'sRuntimePermission::TheCheeseFactory
privatevoidinsertDummyContactWrapper(){
inthasWriteContactsPermission=ContextCompat.checkSelfPermission(M
Manifest.permission.WRITE_CONTACTS);
if(hasWriteContactsPermission!=PackageManager.PERMISSION_GRANTED)
if(!ActivityCompat.shouldShowRequestPermissionRationale(MainAct
Manifest.permission.WRITE_CONTACTS)){
showMessageOKCancel("YouneedtoallowaccesstoContacts"
newDialogInterface.OnClickListener(){
@Override
publicvoidonClick(DialogInterfacedialog,
ActivityCompat.requestPermissions(MainActivi
newString[]{Manifest.permission.WR
REQUEST_CODE_ASK_PERMISSIONS);
}
});
return;
}
ActivityCompat.requestPermissions(MainActivity.this,
newString[]{Manifest.permission.WRITE_CONTACTS},
REQUEST_CODE_ASK_PERMISSIONS);
return;
}
insertDummyContact();
}
Besides we are also able to call the last two methods in Fragment
with some help from Support Library v13 by calling
http://inthecheesefactory.com/blog/thingsyouneedtoknowaboutandroidmpermissiondeveloperedition/en
22/29
1/11/2016
EverythingeveryAndroidDevelopermustknowaboutnewAndroid'sRuntimePermission::TheCheeseFactory
http://inthecheesefactory.com/blog/thingsyouneedtoknowaboutandroidmpermissiondeveloperedition/en
23/29
1/11/2016
EverythingeveryAndroidDevelopermustknowaboutnewAndroid'sRuntimePermission::TheCheeseFactory
24/29
1/11/2016
EverythingeveryAndroidDevelopermustknowaboutnewAndroid'sRuntimePermission::TheCheeseFactory
http://inthecheesefactory.com/blog/thingsyouneedtoknowaboutandroidmpermissiondeveloperedition/en
25/29
1/11/2016
EverythingeveryAndroidDevelopermustknowaboutnewAndroid'sRuntimePermission::TheCheeseFactory
Tweet
Like
209
Share 298peoplelikethis.
37Comments
Sortby Oldest
Addacomment...
NitinSethiBangalore,India
Nicearticle.Thanks.Youneedtoproofreadthearticlebetterasyoualways
endupwithanumberoftypos.
LikeReply
3Aug25,201510:05pmEdited
RodrigoGraaDeveloperatEVOKEIT
Thisisinsane...Aretheycrazy?
AndroidMshouldprompttheuser,nottheappdeveloper....
http://inthecheesefactory.com/blog/thingsyouneedtoknowaboutandroidmpermissiondeveloperedition/en
26/29
1/11/2016
EverythingeveryAndroidDevelopermustknowaboutnewAndroid'sRuntimePermission::TheCheeseFactory
AndroidMshouldprompttheuser,nottheappdeveloper....
Awaytomakesureanappworks,willbetoaskforallthepermissionswhen
theappstarts,sincetheprocessisclosediftheuserschangesitwhilethe
applicationisopen...
LikeReply
2Aug26,20154:33am
DouglasDrumondAndroidDevelopmentAnalystatMovile
Thenyourisktheuserdenyingallofthem,sincetheydon'ttrustthe
appyetandhavenoideawhyitneedsthatpermission(unlessit's
obvious,likeacameraappaskingforcamerapermission).
LikeReplyAug27,201511:45pm
RodrigoGraaDeveloperatEVOKEIT
@DouglasDrumondOk....So,itshouldbetheOS(Android)askingfor
thepermissionwhenthecodecalledsomethingforthefirsttime.....
Thisway,ifacameraappsuddenlywantedtosendoneSMS,theuser
wouldnoticethepromptforthepermissionandprobablyuninstallthe
app...
LikeReply
1Aug28,20151:47amEdited
ViratSingh
Thankyouforthisblogpost!IthelpedmegetstartedwiththeAndroidM
permissions.
IwouldliketomentionthatshouldShowRequestPermissionRationale(...)returns
falsefor2reasonsnowwiththefinalAndroidMSDKupdate:
1.When"Don'taskagain"ischecked(asyoumentioned)
2.Ifthedevicepolicyprohibitstheappfromhavingthatpermission(returns
falseoninitialpermissionprompt)
Ithinkthe2ndreasonmaycausesomeissueswithyourcurrent
implementation.SinceasofthefinalAndroidMSDKupdate(update3,August
2015),shouldShowRequestPermissionRationale(...)alwaysreturnsfalseinitially
aswellaswhen"Don'taskagain"ischecked.
Hereisalinktothedocswiththeexplanation:... SeeMore
LikeReply
2Aug26,201511:07amEdited
DavidLaumaillier
IhopeallthisisforrootedphonesonlyotherwiseI'mintrouble.Didn't
understandmostofit.
LikeReplyAug26,20157:29pm
DouglasDrumondAndroidDevelopmentAnalystatMovile
Ifyou'renotadeveloper,don'tworry.Ifyou'readeveloperanddon't
getit,sorrytoinformyou,youneedtostudyalittlebit,thisisnotrocket
science.
LikeReply
1Aug27,201511:44pm
DavidLaumaillier
Wow,thanksforthatinsult.Sorryfortroublingyoursuperioritywithmy
comment.
http://inthecheesefactory.com/blog/thingsyouneedtoknowaboutandroidmpermissiondeveloperedition/en
27/29
1/11/2016
EverythingeveryAndroidDevelopermustknowaboutnewAndroid'sRuntimePermission::TheCheeseFactory
comment.
LikeReplyAug28,20152:05am
JiaxinJiWorksat
DavidLaumaillier!~
LikeReplyAug31,20151:12am
Show1morereplyinthisthread
Related
(/blog/how-to-install-and-use-acra-android/en)
Posted on 1 Mar 2015 23:35
http://inthecheesefactory.com/blog/thingsyouneedtoknowaboutandroidmpermissiondeveloperedition/en
28/29
1/11/2016
EverythingeveryAndroidDevelopermustknowaboutnewAndroid'sRuntimePermission::TheCheeseFactory
(http://twitter.com/thecheesefact)
(http://facebook.com/inthecheesefactory)
(/blog/en/rss.xml)
http://inthecheesefactory.com/blog/thingsyouneedtoknowaboutandroidmpermissiondeveloperedition/en
29/29