Marshmallow – how does new Android version make your life easier?

Posted on September 23, 2015

Marshmallow – how new Android version makes life easier

August 17th comes with the latest version of the new APIs 23 – preview 3. What can developers and users expect from next Android version? If you are looking for article showing how to use new features in your app and how implement necessary code, this one definitely might be helpful.

New API includes such changes as asking for permission at runtime, direct sharing and authorization in apps with fingerprint..

Runtime Permissions

With API 23 Android introduced completely new permissions model which orders to ask for  permissions at runtime. While using Android M, if any app needs access to such things like camera, body sensors, location etc., you’ll be asked to give specific permission.

Also user is able to edit permissions at Settings->Apps->->App permissions.

How to implement new permissions model

final int REQUEST_CAMERA = 0;

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)

{

       requestCameraPermission();

}

private void requestCameraPermission()

{

    if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA))

    {

          // Provide an additional rationale to the user if the permission was not granted

          // and the user would benefit from additional context for the use of the permission.

      ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA);

   }

   else

   {

          // Camera permission has not been granted yet. Request it directly.

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

   }

}

To listen on result of permissions dialog decision implement ActivityCompat.OnRequestPermissionsResultCallback Interface and override onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) method in your class.

All you need to do now is check if requestCode equals your REQUEST_CAMERA and grantResults[0] == PackageManager.PERMISSION_GRANTED
You can always ask for more than one permission. Then for next indexes in the grantResults list you got answers for other permissions.

This permissions model is only supported on devices running the M Developer Preview. Before calling any of these methods, the app should verify if platform runs on API 23.

private boolean androidM()

{

       return Build.VERSION.SDK_INT == Build.VERSION_CODES.M;

}

Code sample: https://github.com/googlesamples/android-RuntimePermissions

Fingerprint Authentication 

Fingerprint authentication can be used in any app that runs on device with fingerprint sensor and Android M. This is best way for fast authentication that can be best solution in developing applications which include payments.

To implement fingerprint authentication in your app first add USE_FINGERPRINT permission in your manifest.

<uses-permission  android:name=“android.permission.USE_FINGERPRINT” />

With API 23 comes new class FingerprintManager. Creating instance of this class and calling method authenticate() you can compare your fingerprint with the one created earlier in settings.

To test fingerprint authentication without proper device you can go to Settings -> Security -> Fingerprint on your emulator and emulate fingerprint touch events with commend

adb e emu finger touch <finger_id>

If you are using Windows you may have to run telnet 127.0.0.1 <emulator-id> followed by finger touch <finger_id>.

New API doesn’t support UI for capturing fingerprint so you have to create your own or use Android sample library with ready to use fingerprint dialog: https://github.com/googlesamples/android-FingerprintDialog

It is recommended to use Android fingerprint icon when creating UI fingerprint authentication.

New API also delivers credential libraries that helps you generate keys and control time that user has been validated.

Direct share

From Android M you can create more intuictive sharing which allows user to start another social app with specified target.

To use this in your app you have to extend your class with ChooserTargetService, declare the service in manifest file with the BIND_CHOOSER_TARGET_SERVICE permission and include intent filter with the SERVICE_INTERFACE action.
<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>

By adding <meta-data> to your activity in manifest file you can use your .MyChooserTargetService file to share with specific target.

<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=“.ChooserTargetService” />

</activity>

New floating apps policy

With apps targeting API 23 or above comes new rules for drawing on screen.

If you are using Facebook Messenger app then you probably know floating bubble interface very good. Even if you are using different apps you can see them and that behaviour won’t be default on the newest Android.

You’ll have to agree for screen sharing for different apps.

Applications won’t be able to allow screen sharing straight from app. User must go to the right screen where he can choose if he wants to draw over other apps.

You’ll be able to change permission at any time in Settings->Apps->->Draw over other apps.

Developers can check if their apps got permission for drawing over other apps by calling

Settings.canDrawOverlays(). If not, by creating Intent and setting description to Settings.ACTION_MANAGE_OVERLAY_PERMISSION user can be sent to list of apps. By setting URI to specific package, navigation goes straight to particular app.

If you would like to know more, check this site http://www.androidpolice.com/2015/09/07/android-m-begins-locking-down-floating-apps-requires-users-to-grant-special-permission-to-draw-on-other-apps/  where you can find precise information about policy changes.

Power Optimizations

Doze mode – it actives when device is left for a while with screen turned off and unplugged charger. Doze mode works as sleep mode. When it’s running, device makes periodically synchronization to perform any pending operations.

App standby – it tells system if app is idle when user is not using it.

According to Google IO 2015 new power optimization effects with twice longer battery life comparing devices with Android M and Android Lollipop with lot of apps run.

In my opinion these changes can make the biggest impact in using new Andriod M.
Unfortunately number of devices with fingerprint sensor still isn’t impressive, but completly new  payment https://www.youtube.com/watch?v=VOn7VrTRlA4 system got huge potential and in five years can be really helpful in everyday life.

What’s your opinion? Which change was the most interesting for you?
Please leave your comments below.

For more information check –  https://developer.android.com/preview/api-overview.html

 

Would you like to know more about Appformation’s work and get all the new articles? Please join our free newsletter now!

 

Appformation

We develop business applications for the web, iOS and Android– bringing you flexibility, responsiveness and quality. We like to be there for our customers – and with our headquarters in Wroclaw, Poland and a sales office just outside London, we’re handily placed for businesses across Europe.