Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get user current opened application?

I am looking for a solution that i would like to get user opened or installed application on his device.I have to get which app opened by user from my BroadcastReceiver class.I have implemented my code as follows:

public class AppReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {

    ActivityManager am = (ActivityManager)context.getSystemService(context.ACTIVITY_SERVICE);
    List l = am.getRunningAppProcesses();
    Iterator i = l.iterator();
    PackageManager pm = context.getPackageManager();
    while(i.hasNext()) {
      ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo)(i.next());
      try {

        CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
        Log.v(" App Name : ", c.toString());


      }catch(Exception e) {
      }
    }


}

I have also added about this reciever on manifest file as :

  <receiver android:name="AppReciever">
        <intent-filter>
            <category android:name="android.intent.category.DEFAULT" />
            <action android:name="android.intent.action.PACKAGE_ADDED"></action>
            <action android:name="android.intent.action.PACKAGE_CHANGED"></action>
            <action android:name="android.intent.action.PACKAGE_INSTALL"></action>
            <action android:name="android.intent.action.PACKAGE_REPLACED"></action>
            <action android:name="android.intent.action.PACKAGE_RESTARTED"></action>
  <data android:scheme="package" />

  </intent-filter>
    </receiver>

From the above code AppReciver not executing for get the app name at Log.v when I opened new app which are already existed(installed) on the device.It is working for only once other app run on device.

Please any body help me on get the current opened applications from BroadcastReceiver

like image 545
prasad.gai Avatar asked Jan 08 '13 06:01

prasad.gai


1 Answers

Add to you service registerReceiver(). Don't forget to unregister receiver;

    AppReceiver appReceiver = new AppReceiver();
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.setPriority(900);
    intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
    intentFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
    intentFilter.addAction(Intent.ACTION_PACKAGE_INSTALL); //@deprecated This constant has never been used.
    intentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
    intentFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
    registerReceiver(appReceiver, intentFilter);

For unregistering: unregisterReceiver(appReceiver);

like image 140
resource8218 Avatar answered Oct 13 '22 20:10

resource8218