lunes, 30 de noviembre de 2015

Android Software Developer Interview Questions

Android Interview Questions

Do you want to apply for a Job as an Android Developer?, well this time I shared the most popular questions I got while I was in marketing. I hope they are helpful for you.

If I can help with something, just send me an email and I will be happy to help.

Android Framework

  • Describe the architecture of Android Operating System
  • Difference between fragment and activity
  • What is an sticky broadcast receiver
  • Elements of an Intent
  • Different types of services
  • What is ANR and how to avoid it
  • Define Async Task
  • Can you run Async Task in parallel
  • Different types of components from Android Framework
  • What is a nine patch image
  • How do you disable execution of a service from outside of the application
  • What types of Context exist in Android
  • What is a memory leak?
  • What type of context do you pass when you are creating a new view
  • Difference between IntentService and Service
  • How do you communicate multiple threads in Android
  • How many .dex files can you have in an android application
  • What is DDMS, what do you use it for?
  • What are the essential items in an Android Project?
  • What is a weak reference?
  • Explain the life cycle of an activity
  • Difference between Activity and fragment?
  • What is Dalvik Virtual Machine?
  • What is ART?
  • What is ADB?
  • What is a .dex file?
  • Tell me about the best practices you know to program in Android
  • What are the use of DDMS?
  • How do you create Unit Testing in Android?
  • How do you use a AlarmManager?
  • How do you configure an alarm manager to fire every day at a specific time of the day?
  • What is a ViewHolder?
  • What happen if I define two activities with an intent filter with the category of launcher?

Core Java

  • What are the ways to create a Thread?
  • What is a dead lock
  • What is multi inheritance?, is allow in java?
  • What is final, finalize()
  • Define Static
  • What is an abstract class?
  • What is generics
  • What is polymorphism
  • What is encapsulation

Design Patterns

  • What is Singleton Design Pattern
  • What is Thread Safe Singleton Design Pattern
  • What is Abstract Factory Design Pattern
  • What is Strategy Design Pattern
  • Tell me some design patterns that you have applied in Android

Web Services

  • Difference between REST and SOAP
  • How do you decide between REST and SOAP?
  • Algorithms

    • How do you remove duplicated elements in an array of ints
    • How to get the middle element of an Simple Linked List in one pass
    • How to know if a strings are palindromes
    • How to determine if two strings are anagrams
    • How do you get the common parent of two given nodes? Every node has a reference for its parents, and the root node has a reference to null.
    • 
                 A
      
                / \ \
      
               B   C  M
      
              /     \
      
             D       E
      
            / \
      
           F   G
      
                \
      
                 H
      
      

    jueves, 19 de noviembre de 2015

    Common commands for Android development

    Hi my friends, this time i will share the most common commands I use on a daily basis working as an Android Developer. If you want to add some more commands feel free to comment and we can extend this list.

    Android

    Update android sdk in silent mode

    • Mac
    • ./android update sdk --no-ui

    List available packages for installation

    • Mac
    • ./android list sdk

    Android Debug Bridge (ADB)

    Execute commands on device.

    • Install application (apk)
    • adb install /pathToApk
    • Replace existing application installation (apk)
    • adb install -r /pathToApk
    • Start main activity (apk)
    • adb shell am start -a android.intent.action.MAIN -n applicationPackage/.MainActivity
    • List applications installed by package name
    • adb shell pm list packages
    • Uninstall application by package name
    • adb shell pm uninstall mx.com.app
    • List files on sdcard
    • adb shell ls sdcard/
    • List files on sdcard
    • adb shell ls sdcard/
    • Get a file from device sdcard
    • adb pull /sdcard/directory/file.extension
    • Send a file to the device sdcard
    • adb push /pathToFile/file.extension /sdcard/directory/
    • Run adb as root on device. This allow to install an aplicacion as preinstalled in the device.
    • adb root
    • Mount file system with read-write permissions
    • adb remount

    Gradle

    • Show graph of dependencies for the project
    • gradle :app:dependencies
    • Show available tasks
    • gradle tasks
    • Package application
    • gradle assembleDebug
    • Test application
    • gradle assembleDebug
    • Refresh dependencies
    • gradle --refresh-dependencies
    • Offline mode
    • gradle assembleDebug -offline
    • Debug mode
    • gradle assembleDebug -debug

    martes, 10 de noviembre de 2015

    Interact across user Android profiles

    It has been a while since the last time I shared something. Well, this time i will write about how to communicate between user profiles in Android because as we already know in latest releases we have the option to create multiple user profiles in one device. And because i have some trouble trying to figure out how to do it :).

    First i want to share the link with the document where you can find more info related to building applications with multi users profiles.

    But lets start with some requirements to create the test application:

    • Android device with user profiles functionality
    • Rooted device
    • System application permissions.

    Register for the switch user event

    Add the following code to register as a listener of the user profile change event

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            ...
    
            SwitchingUserReceiver receiver = new SwitchingUserReceiver();
            IntentFilter switchingUserProfileEvent = new IntentFilter();
            switchingUserProfileEvent.addAction(Intent.ACTION_USER_BACKGROUND);
            switchingUserProfileEvent.addAction(Intent.ACTION_USER_FOREGROUND);
            registerReceiver(receiver, switchingUserProfileEvent);
        }
    

    Create broadcast receiver for the switch user event

    Create the following broadcast receiver to listen for the switching user profile event.

    public class SwitchingUserReceiver extends BroadcastReceiver {
    
        private static final String TAG = SwitchingUserReceiver.class.getSimpleName();
    
        @Override
        public void onReceive(Context context, Intent intent) {
        }
    }
    

    Because we register the broadcast receiver in runtime, is not necessary to add this event in the Manifest file.

    Launch broadcast receiver to interact with an application in the starting user profile

    UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
                final int userId = UserHelper.getCurrentUser();
    int previousUser = intent.getExtras().getInt("android.intent.extra.user_handle");
    
                UserHandle userHandle = userManager.getUserForSerialNumber(userId);
                Log.d(TAG, "userHandle " + userHandle);
    
                for (Method method : context.getClass().getMethods()) {
                    if ("sendBroadcastAsUser".equalsIgnoreCase(method.getName())) {
                        if (method.getParameterTypes().length == 2) {
                            method.setAccessible(true);
                            Intent broadcastIntent = new Intent();
                            broadcastIntent.setAction("com.example.clerks.myapplication.myaction");
                            int processId = android.os.Process.myPid();
                            broadcastIntent.putExtra("user", previousUser);
                            broadcastIntent.putExtra("processId", processId);
                            method.invoke(context, broadcastIntent, userHandle);
                        }
                    }
                }
    
    ...AndroidManifest...
    <uses-permission android:name="android.permission.MANAGE_USERS" />
        <uses-permission android:name="android.permission.INTERACT_ACROSS_USERS"
                         android:protectionLevel="signatureOrSystem"/>
    

    Now we are going to use the method sendBroadcastAsUser. This method is not available to execute directly, but we are going to invoke it by reflection.

    sendBroadcastAsUser receives as firs parameter the intent with the action to send the broadcast and the user where we want to send the brodcast. Note that when this onReceive method executes, the current user has already change, so the user in the foreground is the user profile that we select in the user profile interface. Thats why we are sending the broadcast to this current user (userHandle).

    And the action("com.example.clerks.myapplication.myaction"), is the one that we are going to use to select our broadcast receiver.

    To execute this code we require the permissions to MANAGE_USERS and INTERACT_ACROSS_USERS, so we are going to create system application.

    Broadcast receiver for the custom action

    public class OnUserSwitchedReceiver extends BroadcastReceiver {
    
        private static final String TAG = OnUserSwitchedReceiver.class.getSimpleName();
    
        @Override
        public void onReceive(final Context context, Intent intent) {
            final int previousUser = intent.getIntExtra("user", -1);
            final int previousProcessId = intent.getIntExtra("processId", -1);
            final int processId = android.os.Process.myPid();
    
            final int userId = UserHelper.getCurrentUser();
            Log.d(TAG, "Active user = " + userId);
    
            new Handler(Looper.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(context, String.format("Message received. Process Ids: %d vs %d, Users: %d vs %d",
                                    processId, previousProcessId, userId,
                                    previousUser),
                            Toast.LENGTH_LONG).show();
                }
            });
        }
    }
    
    <receiver android:name=".OnUserSwitchedReceiver">
                <intent-filter>
                    <action android:name="com.example.clerks.myapplication.myaction" />
                </intent-filter>
            </receiver>
    

    In this case we have to register this broadcast receiver in the manifest file for the application that can handle this action

    Helper class to get the current user

    public class UserHelper {
        public static int getCurrentUser() {
            Method getCurrentUser = null;
            try {
                getCurrentUser = ActivityManager.class.getDeclaredMethod("getCurrentUser");
                getCurrentUser.setAccessible(true);
                return (int) getCurrentUser.invoke(null);
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            return -1;
        }
    }
    

    Deploy application

    echo "setting as root"
    adb root
    
    echo "remounting device"
    adb remount
    
    echo "creating package for application"
    gradle clean assembleDebug
    mv build/outputs/apk/app-debug.apk build/outputs/apk/MultiprofileApp.apk
    
    echo "removing current apk in priv-app"
    adb shell rm system/priv-app/MultiprofileApp/MultiprofileApp.apk
    
    echo "moving current apk to priv-app"
    adb push build/outputs/apk/MultiprofileApp.apk system/priv-app/MultiprofileApp/ 
    
    echo "Changing permissions to directory MultiprofileApp"
    adb shell chmod -R 755 system/priv-app/MultiprofileApp
    
    echo "changing permissions to apk (644)"
    adb shell chmod 644 system/priv-app/MultiprofileApp/MultiprofileApp.apk
    
    echo "restarting device"
    adb reboot
    

    As you can see I have created a directory call MultiprofileApp in the priv-app directory. priv-app is used to install sytem applications. After create the directory and push the apk file, we need to reboot the device so the operating system can scan again the applications.

    Test application

    Start application, change the user and check that the toast appears in the open user.

    Some things to keep in mind

    • The context has another methods to interact between users, for example, start a service and an activity, I just test the startServiceAsUser and sendBroadcastAsUser but with startServiceAsUser I was getting all the time an exception related to INTERACT_ACROSS_USERS permission, even if i have include that permission and install my application as preinstalled, i was getting that exception. But you can try by yourself.
    • I´m having some problems to get the method sendBroadcastAsUser to execute in the current Context, that's why you can see that for approach.

    And that's all, now you can interact between system applications.