StrictMode – Detects things you might be doing by accident

Useful diagnostic tool. It helps to find and fix performance issues, object leaks and other runtime issues.

if (BuildConfig.DEBUG) {
 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
       .detectAll()
       .penaltyLog()
       .build());
 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
       .detectAll()
       .penaltyLog()
       .penaltyDeathOnNetwork()
       .build());
}

Example in logs:

D: StrictMode policy violation; ~duration=19 ms: 
        android.os.StrictMode$StrictModeDiskReadViolation: 
                policy=543 violation=2
at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1137)
at android.app.SharedPreferencesImpl.awaitLoadedLocked(SharedPreferencesImpl.java:202)
at android.app.SharedPreferencesImpl.getBoolean(SharedPreferencesImpl.java:259)
at .MyApplication.notificationToggleCheck(MyApplication.java:54)
at .MyApplication.onCreate(MyApplication.java:35)
...

http://developer.android.com/reference/android/os/StrictMode.html

How to check if a service is running

private boolean isMyServiceRunning(Class<?> serviceClass) {
    ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (serviceClass.getName().equals(service.service.getClassName())) {
            return true; 
        } 
    } 
    return false; 
} 

Infinite ViewPager

MainActivity – onCreate()

...
SectionsPagerAdapter mSectionsPagerAdapter = 
        new SectionsPagerAdapter(getSupportFragmentManager(), numOfPages);
ViewPager viewPager = (ViewPager) findViewById(R.id.container_view_pager);
viewPager.setAdapter(mSectionsPagerAdapter);
viewPager.setCurrentItem(1000 * numOfPages);
...

Keep Reading

Stretchable bitmap image short guide – 9-patch/.9.png

  1. png9Add one pixel width transparent border to your image (png).
  2. On the top transparent line that we added, paint a black line and it would mark the stretchable zone on the x-axis.
  3. On the left transparent line that we added, paint a black line and it would mark the stretchable zone on the y-axis.
  4. On the right and bottom transparent lines that we added, paint a black lines and they would mark the content zone.
  5. Check that all the pixels that we’ve added around the image are transparent or completely black.
  6. You should add the markers after you’ve created the images for all dimensions.

Keep Reading