What is new in Android M

3,830 views

Published on

A walkthrough with code samples of some of the developer highlights found in Android 6.0/Marshmallow.
- Now on Tap and the Assist API
- Runtime Permission handling
- App Linking
- Direct Share
...and more

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
3,830
On SlideShare
0
From Embeds
0
Number of Embeds
1,268
Actions
Shares
0
Downloads
15
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

What is new in Android M

  1. 1. What is new in Android Marshmallow?
  2. 2. Android 6.0 Highlights 4 Runtime Permissions 4 'Now on Tap', the Assist API and Voice Interactions 4 App Linking 4 Direct Share 4 Auto Backup for Apps 4 Doze and App Standby 4 Fingerprint Authentication and Confirm Credential
  3. 3. Runtime Permissions Like the web, but different! :) 4 No blocked auto-updates! 4 User controlled! 4 Revokable 4 Simplified
  4. 4. Requesting permissions if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) { if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity, Manifest.permission.READ_CONTACTS)) { // Show dialog why permission is required. } else { ActivityCompat.requestPermissions(thisActivity, new String[]{Manifest.permission.READ_CONTACTS}, MY_PERMISSIONS_REQUEST_READ_CONTACTS); } }
  5. 5. Handling permission request results @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case MY_PERMISSIONS_REQUEST_READ_CONTACTS: { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // permission was granted, yay! Do the // contacts-related task you need to do. } else { // permission denied, boo! Disable the // functionality that depends on this permission. } return; } } }
  6. 6. Now on Tap 4 Based on the new Assist API 4 Reads meta-data from app 4 Get "smarter" over time... 4 Possible to build custom assistants!
  7. 7. Assist API - Opt-out with secure flag: private void disableAssistant(Activity activity) { Window window = activity.getWindow(); window.addFlag(WindowManager.LayoutParams.FLAG_SECURE); }
  8. 8. Assistant meta-data 4 Follow accessibility guidelines! 4 Callbacks in Application and Activity classes 4 JSON-LD as defined by Schema.org @Override public void onProvideAssistContent(AssistContent assistContent) { super.onProvideAssistContent(assistContent); String structuredJson = new JSONObject() .put("@type", "MusicRecording") .put("@id", "https://example.com/music/recording") .put("name", "Album Title") .toString(); assistContent.setStructuredData(structuredJson); }
  9. 9. Voice Interactions 4 Started with voice command? - Activity.isVoiceInteraction() 4 Use andorid.app.VoiceInteraction for further interactions 4 VoiceInteractor.ConfirmationRequest 4 VoiceInteractor.CommandRequest
  10. 10. App Linking - Automatic verification <activity ...> <intent-filter android:autoVerify="true"> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="http" android:host="www.android.com" /> <data android:scheme="https" android:host="www.android.com" /> </intent-filter> </activity>
  11. 11. App Linking - assetslinks.json [{ "relation": ["delegate_permission/common.handle_all_urls"], "target": { "namespace": "android_app", "package_name": "com.example", "sha256_cert_fingerprints": ["14:6D:E9:83:C5: ... :3F:CF:44:E5"] } }]
  12. 12. Direct Share 4 Multiple share targets 4 Improved sharing UX
  13. 13. Direct Share - Manifest <service android:name=".MyChooserTargetService" android:label="@string/service_name" android:permission="android.permission.BIND_CHOOSER_TARGET_SERVICE"> <intent-filter> <action android:name="android.service.chooser.ChooserTargetService" /> </intent-filter> </service> <activity android:name=".MyShareActivity" android:label="@string/share_activity_label"> <intent-filter> <action android:name="android.intent.action.SEND" /> </intent-filter> <meta-data android:name="android.service.chooser.chooser_target_service" android:value=".MyChooserTargetService" /> </activity>
  14. 14. Implementing ChooserTargetService public class MyChooserTargetService extends ChooserTargetService { public List<ChooserTarget> onGetChooserTargets(ComponentName targetActivityName, IntentFilter matchedFilter) { Icon chooserIcon = Icon.createWithBitmap(getBitmapForTarget()); ComponentName targetActivity = new ComponentName(this, TargetActivity.class); Bundle extras = new Bundle(); // Populate as needed ChooserTarget target = new ChooserTarget("Title", chooserIcon, targetActivity, extras); // TODO Create a list of ChooserTargets... return chooserTargets; } }
  15. 15. Doze and App Standby
  16. 16. Doze restrictions 4 Network access is suspended. 4 The system ignores wake locks. 4 Standard AlarmManager alarms (including setExact() and setWindow()) are deferred to the next maintenance window. 4 If you need to set alarms that fire while in Doze, use setAndAllowWhileIdle() or setExactAndAllowWhileIdle(). 4 Alarms set with setAlarmClock() continue to fire normally — the system exits Doze shortly before those alarms fire. 4 The system does not perform Wi-Fi scans. 4 The system does not allow sync adapters to run. 4 The system does not allow JobScheduler to run.
  17. 17. Doze and AlarmManager AlarmManager.setAndAllowWhileIdle() and AlarmManager.setExactAndAllowWhileIdle() 4 Adds your app to a temporary whitelist for 10 seconds! 4 Can't run more than every 15 minutes! 4 Can be rescheduled by the system!
  18. 18. How to avoid it! 4 Let the user whitelist your app from AppStandby PowerManager alarmManager = context.getSystemService(PowerManager.class); if(!alarmManager.isIgnoringBatteryOptimizations(BuildConfig.APPLICATION_ID)) { Intent intent = new Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS); context.startActivity(intent); }
  19. 19. Auto Backup for Apps 4 Limited to 25 MB app data 4 Use android:fullBackupContent="@xml/ mybackupscheme" in the application tag for configuration <full-backup-content> <include domain=["file" | "database" | "sharedpref" | "external" | "root"] path="string" /> <exclude domain=["file" | "database" | "sharedpref" | "external" | "root"] path="string" /> </full-backup-content>
  20. 20. Fingerprint Authentication 4 Use in conjunction with Android Keystore system <uses-permission android:name="android.permission.USE_FINGERPRINT" />
  21. 21. Fingeprint Authentication FingerprintManager mgr = context.getSystemService(FingerprintManager.class); mgr.authenticate(...);
  22. 22. Confirm Credential 4 Invalidates a key from the Keystore after a certain period keyGenerator.init(new KeyGenParameterSpec.Builder(KEY_NAME, KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT) .setBlockModes(KeyProperties.BLOCK_MODE_CBC) .setUserAuthenticationRequired(true) // Invalidate this key after 2 minutes inactivity... .setUserAuthenticationValidityDurationSeconds(120) .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7) .build()); keyGenerator.generateKey();
  23. 23. Upgrade Tips'n'Tricks 1. Bump compileSdkVersion 2. Test! 3. Bump targetSdkVersion 4. Win!
  24. 24. android { compileSdkVersion 23 buildToolsVersion "23.0.1" defaultConfig { applicationId "com.awesomeapp" minSdkVersion 16 targetSdkVersion 22 versionCode 1 versionName "1.0" } }
  25. 25. Apache HTTP Client Removal 4 org.apache.http is removed from the APIs 4 Still available at runtime! 4 Can be re-enabled in build.gradle: android { useLibrary 'org.apache.http.legacy' } 4 Highly discouraged!
  26. 26. Thank you for listening! Any questions?

×