Thursday, 19 June 2014

Getting Installed Applications name and package name information from device

installedapps_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/installedapps_listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>


installedapps_customlayout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/installedapps_customlayout_textview_appname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/installedapps_customlayout_textview_packagename"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>


GetInstalledAppsActivity.java:
package com.ram.installedapps;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.example.checking.R;

//Class to display installed app names and package names from device
public class GetInstalledAppsActivity extends Activity
{
    // ArrayList to hold app names
    ArrayList<String> appnames;
    // ArrayList to hold app package names
    ArrayList<String> packagenames;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {

        super.onCreate(savedInstanceState);
        // Set a Activity content from installedapps_activity layout
        setContentView(R.layout.installedapps_activity);

        // ArrayList to hold app names
        appnames = new ArrayList<String>();
        // ArrayList to hold app package names
        packagenames = new ArrayList<String>();

        // Get a handle to the listview to display installed apps information
        ListView listView = (ListView) findViewById(R.id.installedapps_listView);

        // Getting Global package information
        PackageManager packageManager = getPackageManager();

        // Get a List of all application packages that are installed on the
        // device

        List<ApplicationInfo> packageInfos = packageManager
                .getInstalledApplications(PackageManager.GET_META_DATA);

        // Getting every app information
        for (ApplicationInfo a : packageInfos)
        {

            // To get app name
            String appname = a.loadLabel(getPackageManager()).toString();
            appnames.add(appname);

            // To get app package name
            String packageName = a.packageName;
            packagenames.add(packageName);

        }

        // Creating Adapter object to create views in listview
        CustomAdapter customAdapter = new CustomAdapter();

        // Set a adapter object to the listview
        listView.setAdapter(customAdapter);
    }

    class CustomAdapter extends BaseAdapter
    {

        @Override
        public int getCount()
        {
            // return the all apps count
            return appnames.size();
        }

        @Override
        public Object getItem(int position)
        {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int position)
        {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            // Inflating installedapps_customlayout
            View view = getLayoutInflater().inflate(R.layout.installedapps_customlayout,
                    null);

            // Calling Textview references
            TextView appnameTextView = (TextView) view
                    .findViewById(R.id.installedapps_customlayout_textview_appname);
            TextView packageNameTextView = (TextView) view
                    .findViewById(R.id.installedapps_customlayout_textview_packagename);

            // Setting data to the TextViews
            appnameTextView.setText(appnames.get(position));
            packageNameTextView.setText(packagenames.get(position));

            return view;
        }

    }
}
 


Give following permission in AndoridManifest.xml:
  <uses-permission android:name="android.permission.READ_PHONE_STATE" />