Posts

How to get the list of Activities from BackStack

Two ways are there,

1. Using ADB Commands

Here is the complete list to get activities, services & processes 1. adb shell dumpsys activity activities -> displays list of activities in back stack 2. adb shell dumpsys activity process -> displays list process in back stack 3. adb shell dumpsys activity services -> displays list of services running in back stack
Another alternate way is through ActivityManager,
ActivityManager m = (ActivityManager) ctx.getSystemService( ctx.ACTIVITY_SERVICE ); List runningTaskInfoList = m.getRunningTasks(10); Iterator itr = runningTaskInfoList.iterator(); while(itr.hasNext()){ RunningTaskInfo runningTaskInfo = (RunningTaskInfo)itr.next(); int id = runningTaskInfo.id; CharSequence desc= runningTaskInfo.description; int numOfActivities = runningTaskInfo.numActivities; String topActivity = runningTaskInfo.topActivity.getShortClassName(); }

Event Bus using Green robot and Google guava

Event bus is a concept to pass events between components without knowing to whom the event is sending and who is going to receive the event. Components just push events to event bus and subscribers subscribe to the events will receive the event. Event enables decoupling of components. In android communication between components is cumbersome. This concept, event bus tries to solve the problem.

Green robot' event bus library is a latest in android which has a neat implementation of event bus. Here I am trying to explain the concept of event bus through a simple example. I have a receiver which will listens to events from Power manager. When ever device is charging or discharging, receiver component will push events to event bus. An activity which is subscribed to the event will receive the events and displayed in the screen.

The same example I tried with both libraries green robot and Google Guava.

You can find the green robot sample code in Git hub

The example for Guava imple…

Firebase cloud messaging - Step by step procedure

Implement FCM cloud messaging in Android app through Android studio is now simple. The step by step procedure is,

1. Create a new project in android studio

2. Click on tab Tools -> Firebase

3. Firebase assistant wiil open on right hand side of studio

4. A new dialog box appears to connect to firebase (This step creates a new project in the same name, here FCMMessaging in firebase console)

5. Then add FCM to app, through firebase assisstant, this step will add dependencies that is required for the app.

6. Now Create a service which should extend FirebaseInstanceIdService, which should override the method onTokenRefresh(), code snippet is as follows @Override public void onTokenRefresh() { super.onTokenRefresh(); // Get updated InstanceID token. String refreshedToken = FirebaseInstanceId.getInstance().getToken(); Log.e("FirebaseInstanceID", "Refresh token: " + refreshedToken); }

7. Now create another service which should extend FirebaseMessagingSe…

Espresso test to check the max length in an editText

Espresso is a testing framework in android, which is used to test the UI elements in android like editText, textView, button, recyclerView and so on. One thing to note here is that Espresso synchronises all UI events. In case if we want to check network operations, we have to use IdlingResource, which I will explain in my upcoming tutorials.

Here I am verifying the mobile number (editText) can enter max 13 characters. I created a custom matcher, checkMaxLength which will check the max length of editText is 13 characters. We can pass any integer value as we wish. import android.os.Build; import android.os.SystemClock; import android.support.annotation.RequiresApi; import android.support.test.rule.ActivityTestRule; import android.support.test.runner.AndroidJUnit4; import android.text.InputFilter; import android.view.View; import android.widget.TextView; import com.smartandroidians.myloci.activities.LoginActivity; import org.hamcrest.Description; import org.hamcrest.TypeSafeMatcher…

Utility method to sort HashMap

A Simple utility method to sort values or keys in a hashMap. Here in the example, I am sorting by values, we can also sort by keys. For that in Compartor, user getkey() instead of getValue() method. Here in the method parameter, boolean value order true means ASC and false means DESC

private static Map sortByComparator(Map unsortMap, final boolean order) { List<Map.Entry<String, Integer>> list = new LinkedList<>(unsortMap.entrySet()); // Sorting the list based on values Collections.sort(list, new Comparator(Map .entry<String, Integer> o1, Map.Entry<String, Integer> o2) { public int compare(Map.Entry o1, Map.Entry o2) { if (order) { return o1.getValue().compareTo(o2.getValue()); } else { return o2.getValue().compareTo(o1.getValue()); } } }); // Maintaining insertion order with the help of LinkedList Map sortedMa…

How to check valid Email address

The code snippet to check whether the entered email address is valid is as follows,

Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+ if (emailPattern.matcher(enteredEmailId).matches()) { // do your stuff }
Happy Coding
Team Androidians

Dismiss or Cancel dialog by swipe gesture

Dismiss dialog by all four swipe gestures as swipe right to left, left to right, top to bottom, bottom to top First, create a inner class in custom dialogFragment class for the the SimpleGestureListener as, class MyGestureDetector extends GestureDetector.SimpleOnGestureListener { private static final int SWIPE_MIN_DISTANCE = 120; private static final int SWIPE_THRESHOLD_VELOCITY = 200; public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { // swipe right to left dismiss(); } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { // swipe left to right dismiss(); } else if (e1.ge…