Wednesday, December 9, 2015

How to get running processes in android

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".MainActivity"
    android:background="#eff6ff"
    >
    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        >
        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
    </ScrollView>
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Run"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:background="#5ea8f7"
        />
</RelativeLayout>
MainActivity.java

package com.cfsuman.me.androidcode;

import android.app.ActivityManager;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;


public class MainActivity extends AppCompatActivity {
    private Context mContext;

    private Button mButton;
    private TextView mTextView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // Request window feature action bar
        requestWindowFeature(Window.FEATURE_ACTION_BAR);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the application context
        mContext = getApplicationContext();

        // Set the action bar color
        getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.RED));

        // Get the widgets reference from XML layout
        mButton = (Button) findViewById(R.id.btn);
        mTextView = (TextView) findViewById(R.id.tv);

        // Set a click listener for button widget
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Empty the TextView
                mTextView.setText("");

                /*
                    ActivityManager
                        Interact with the overall activities running in the system.
                */

                // Initialize a new instance of ActivityManager
                ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);

                /*
                    ActivityManager.RunningAppProcessInfo
                        Information you can retrieve about a running process.
                */

                /*
                    public List<ActivityManager.RunningAppProcessInfo> getRunningAppProcesses ()
                        Returns a list of application processes that are running on the device.
                        Note: this method is only intended for debugging or building a user-facing
                        process management UI.

                    Returns
                        Returns a list of RunningAppProcessInfo records, or null if there are no
                        running processes (it will not return an empty list). This list ordering
                        is not specified.
                */
                List<ActivityManager.RunningAppProcessInfo> runningProcesses = am.getRunningAppProcesses();

                // Display the number of running processes
                Toast.makeText(mContext,"Processes : " + runningProcesses.size(),Toast.LENGTH_SHORT).show();

                // Loop through the running processes
                for(ActivityManager.RunningAppProcessInfo processInfo: runningProcesses ){
                    // Get the process name
                    mTextView.setText(mTextView.getText() + processInfo.processName + "\n");
                }
            }
        });
    }
}