Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
552 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 Reply

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...