Example how to not losing asynctask state when rotate screen

public class MainActivity extends AppCompatActivity {
    private Async task;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        task = (Async) getLastCustomNonConfigurationInstance();

        if (task == null) {
            task = new Async(this);
            task.execute();
        } else {
            task.attach(this);
        }
    }

    @Override
    public Object onRetainCustomNonConfigurationInstance() {
        task.detach();
        return task;
    }

    private static class Async extends AsyncTask<Void, Void, Void> {
        private static final String TAG = "Async";
        private Activity mActivity;
        private int counter = 0;

        public Async(Activity activity) {
            attach(activity);
        }

        @Override
        protected Void doInBackground(Void... params) {
            while (true) {
                Log.d(TAG, "doInBackground: " + "still working... " + counter++);
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

        public void detach() {
            mActivity = null;
        }

        public void attach(Activity activity) {
            mActivity = activity;
        }
    }
}

 

Remove operator boot animation i.e. plus in Android – Samsung device

First of all You should have root on device, second be able to change your partition on android device to status as: writeable (posted few posts ago what is the command for it).

I assume all is set. Now go in adb shell as a root to : /system/media/

When You call to list files, You will get two interesting files:

-rw------- root     root        69056 2016-12-26 01:42 bootsamsung.qmg
-rw-r--r-- root     root        69056 2016-06-02 17:23 bootsamsungloop.qmg

First is the first static or animating animation, second is the loop. Usually it is like:

-show anoying operator animation
-show after that samsung animation

What to do is to backup first file, and copy the second one but renaming it like the backuped file, which is here “bootsamsung”.

As a result there will be two files as above, but both will do the same animation.

Merge/concatenate jsonObjects

public static JSONObject concat(@NonNull JSONObject[] objs) {
        ArrayList<JSONObject> jsonObjectArrayList = new ArrayList<>();

        for (JSONObject o : objs) {
            if (o.length() > 0) {
                jsonObjectArrayList.add(o);
            }
        }

        JSONObject merged = new JSONObject();
        JSONObject[] jsonObjects = jsonObjectArrayList.toArray(
                new JSONObject[jsonObjectArrayList.size()]);
        for (JSONObject obj : jsonObjects) {
            Iterator it = obj.keys();
            while (it.hasNext()) {
                String key = (String) it.next();
                Object value = null;

                try {
                    value = obj.get(key);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                try {
                    merged.put(key, value);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }

        return merged;
    }

 

Checking if passing object is instance of class by method

public static boolean valid(@NonNull Intent intent, String stringKey, Class<Builder> dataClass) {
    if (dataClass == null) {
        throw new IllegalArgumentException("dataClass must be set");
    }

    if (!intent.hasExtra(stringKey)) {
        Log.d(TAG, "valid() called with: intent = [" + intent + "], stringKey = [" + stringKey + "], dataClass = [" + dataClass + "]  failed");
        return false;
    }

    if (intent.getSerializableExtra(stringKey) == null) {
        Log.d(TAG, "valid() called with: intent = [" + intent + "], stringKey = [" + stringKey + "], dataClass = [" + dataClass + "]  failed");
        return false;
    }

    if (!(dataClass.isAssignableFrom(intent.getSerializableExtra(stringKey).getClass()))) {
        Log.d(TAG, "valid() called with: intent = [" + intent + "], stringKey = [" + stringKey + "], dataClass = [" + dataClass + "]  failed");
        return false;
    }

    return true;
}j

 

TIL: difference between service vs/and intentService

Source: http://stackoverflow.com/questions/15524280/service-vs-intentservice

Tejas Lagvankar wrote a nice post about this subject. Below are some key differences between Service and IntentService.

When to use?

  • The Service can be used in tasks with no UI, but shouldn’t be too long. If you need to perform long tasks, you must use threads within Service.
  • The IntentService can be used in long tasks usually with no communication to Main Thread. If communication is required, can use Main Thread handler or broadcast intents. Another case of use is when callbacks are needed (Intent triggered tasks).

How to trigger?

  • The Service is triggered by calling method startService().
  • The IntentService is triggered using an Intent, it spawns a new worker thread and the method onHandleIntent() is called on this thread.

Triggered From

  • The Service and IntentService may be triggered from any thread, activity or other application component.

Runs On

  • The Service runs in background but it runs on the Main Thread of the application.
  • The IntentService runs on a separate worker thread.

Limitations / Drawbacks

  • The Service may block the Main Thread of the application.
  • The IntentService cannot run tasks in parallel. Hence all the consecutive intents will go into the message queue for the worker thread and will execute sequentially.

When to stop?

  • If you implement a Service, it is your responsibility to stop the service when its work is done, by calling stopSelf() or stopService(). (If you only want to provide binding, you don’t need to implement this method).
  • The IntentService stops the service after all start requests have been handled, so you never have to call stopSelf().