How do I properly fire ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS intent?

Intent intent = new Intent(); String packageName = context.getPackageName(); PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); if (pm.isIgnoringBatteryOptimizations(packageName)) intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS); else { intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); intent.setData(Uri.parse(“package:” + packageName)); } context.startActivity(intent); Kotlin val intent = Intent() val pm : PowerManager = getSystemService(Context.POWER_SERVICE) as PowerManager if (pm.isIgnoringBatteryOptimizations(context.packageName)) { intent.action = Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS } else { intent.action = Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS intent.data = Uri.parse(“package:${context.packageName}”) } context.startActivity(intent) … Read more

Send request over WiFi (without connection) even if Mobile data is ON (with connection) on Android M

Stanislav‘s answer is correct but incomplete because only works in Lollipop. I’ve wrote a complete solution for Lollipop and Marshmallow onwards for you to route all network requests through WiFi when connected to a specific network of your choice. Kotlin In your Activity, @RequiresApi(Build.VERSION_CODES.LOLLIPOP) class RoutingActivity : Activity() { private var mConnectivityManager: ConnectivityManager? = null … Read more

Android Marshmallow: Changing permissions at run time crashes app

It’s because of additional features added from Marshmallow. You need to request from user at runtime. For this use this class which I have made. Then use it whereever required public class AppPermission { public static boolean isMarshmallowPlusDevice() { return Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1; } @TargetApi(Build.VERSION_CODES.M) public static boolean isPermissionRequestRequired(Activity activity, @NonNull String[] permissions, int requestCode) … Read more

Expand/Collapse Lollipop toolbar animation (Telegram app)

Edit : Since the release of the Android Design support library, there’s an easier solution. Check joaquin’s answer — Here’s how I did it, there probably are many other solutions but this one worked for me. First of all, you have to use a Toolbar with a transparent background. The expanding & collapsing Toolbar is … Read more

Design lib – CoordinatorLayout/CollapsingToolbarLayout with GridView/listView

With ListView/GridView, it works only on Lollipop by following code- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { listView.setNestedScrollingEnabled(true); } I think for Now CoordinatorLayout works only with RecyclerView and NestedScrollView EDIT : use – ViewCompat.setNestedScrollingEnabled(listView/gridview,true); (add Android Support v4 Library 23.1 or +)

Wakelock and doze mode

Based on some testing, using a Nexus 5 with the the final(?) preview of Android 6.0 installed: Holding a PARTIAL_WAKE_LOCK is insufficient to block Doze mode — the device will still doze, even though you have the WakeLock and are trying to do regular work (e.g., setExactAndAllowWhileIdle() to get control every minute) Keeping the screen … Read more

Android Marshmallow: Test permissions with Espresso?

With the new release of the Android Testing Support Library 1.0, there’s a GrantPermissionRule that you can use in your tests to grant a permission before starting any tests. @Rule public GrantPermissionRule permissionRule = GrantPermissionRule.grant(android.Manifest.permission.ACCESS_FINE_LOCATION); Kotlin solution @get:Rule var permissionRule = GrantPermissionRule.grant(android.Manifest.permission.ACCESS_FINE_LOCATION) @get:Rule must be used in order to avoid java.lang.Exception: The @Rule ‘permissionRule’ must … Read more