I just want to get numbers of times to be used for each Activity. So the very straightforward method I thought is increasing the count for an Activity when it was started. But how can I get the information?
Thanks for your great help!
I just want to get numbers of times to be used for each Activity. So the very straightforward method I thought is increasing the count for an Activity when it was started. But how can I get the information?
Thanks for your great help!
You have the activity life cycle methods to know when they are created and when they are resumed, etc. Not sure if you mean when it is created (onCreate) or when it is resumed or comes back into focus (onResume).
Personally, I'd sub-class Activity and Application. To sub-class application, you have to remember to declare the class name of the Application in your manifest. The application can have a HashMap keyed by Activity names with an int value of the number of times each activity is resumed or created (or whatever you want).
All your activities currently derive from the Activity class. So, you could create your own ActivityBase that extends Activity and adds overriden methods for onCreate and onResume. These overridden methods would call a method on the Application class to tell it to increment their count. Then, all your activities would extend ActivityBase instead of Activity.
I am certain it's possible since apps like Protector Demo are able to listen for applications opening.
In the Android "Spare Parts" application (it's in the emulator, but probably not on any commercial phones), there is a section called "Usage Statistics". It records not only how many times applications were opened, but also how many milliseconds in total they've been open for.
I don't know how it would achieve that. My best guess would be to pull the information from the system, although other possibilities are an Intent listener or Broadcast Receiver.
The source for Spare Parts is here, if you want to have a look:
Finally, I found a solution, use IActivityWatcher and ActivityManagerNative, and build in source code.
Thank T3Roar very much, I just follow your clue to find this.
sample code below:
package zouyu.sample.activitymonitor;
import android.app.Activity;
import android.app.ActivityManagerNative;
import android.app.IActivityWatcher;
import android.os.Bundle;
import android.os.RemoteException;
import android.util.Log;
public class ActivityMonitor extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
ActivityManagerNative.getDefault().registerActivityWatcher(mActivityWatcher);
} catch (RemoteException e) {
}
}
private IActivityWatcher.Stub mActivityWatcher = new IActivityWatcher.Stub() {
public void activityResuming(int activityId) throws RemoteException {
Log.e("zouyu", "In ActivityMonitor, an Activity resuming: " + activityId);
}
public void closingSystemDialogs(String reason) {
Log.e("zouyu", "In ActivityMonitor, an Activity closing: " + reason);
}
};
}