Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
553 views
in Technique[技术] by (71.8m points)

android listen for app launch

I need to develop a service which listen for every activity start. Must I do something like this?

ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfo = am.getRunningAppProcesses();
for (int i = 0; i < runningAppProcessInfo.size(); i++) {
    Log.v("Proc: ", runningAppProcessInfo.get(i).processName);
}

And do I need to do it every X seconds? Does it affect battery consumption?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As far as I know there is a class IActivityController.Stub in android.app package. But this is an {@hide} interface (as someone said there have some method to access @hide api).

We can set a Listener to listen Activity switch like this:

mAm = ActivityManagerNative.getDefault();          
    try {
        mAm.setActivityController(new ActivityController());

   } catch (RemoteException e) {
        System.err.println("** Failed talking with activity manager!");}

and Class ActivityManagerNative is @hide also. ActivityController is a class extends IActivityController.Stub .

How to access @hide Api:

  1. you can get the android source code to build an have-@hide-api Android.jar to use.
  2. by reflection.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...