-2

I want to use the ActivityManager to get a list of current APPs and return the newest process` name to the system clipboard.Just like this

 protected void onStart() {
        super.onStart();
        Amanager=(ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
        ActivityManager.RunningAppProcessInfo App=null;
        List<ActivityManager.RunningAppProcessInfo> RunningTasks;
        try {
            RunningTasks = Amanager.getRunningAppProcesses();
            App = RunningTasks.get(0);
        }catch (final Exception e){Log.d(TAG, "List ERROR");}
        if(App!=null) {
            String result =App.processName;
            contentText.setText(result);;
            Clipboard.setText(result);
        }else {
            Log.d(TAG, "COMPONENT ERROR");
        }
    }

But when running at the Simulator, some errors occured:

java.lang.RuntimeException: Unable to start activity ComponentInfo{//the absolute address}:

Elletlar
  • 3,136
  • 7
  • 32
  • 38
Arics Lee
  • 49
  • 4

2 Answers2

2

Try this

ActivityManager actvityManager = (ActivityManager)
this.getSystemService( ACTIVITY_SERVICE );
List<RunningAppProcessInfo> procInfos = actvityManager.getRunningAppProcesses();

for(int i = 0; i < procInfos.size(); i++)
 {
   Log.i("App-Package","" + procInfos.get(i).processName);
}

This may helps you

Sathish Kumar J
  • 4,280
  • 1
  • 20
  • 48
2

You code is perfectly fine. You are getting this exception because your contentText is null. Make sure you're referencing it correctly.

Talha Mir
  • 1,228
  • 11
  • 19