views:

169

answers:

3

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!

A: 

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.

Rich
Hi Rick,It's very nice of you to give a so detailed and patient response!I think it must be a good method to count my own activities.But for my scenario, I need to count very Activities include others(like Activities of Gmail, Messaging, Browser, Facebook etc...).So is there a common method for every activities?Thanks again for your help!
Yu Zou
Sorry it is not possible to do this.
hackbod
Hi Hackbod,I just find an app in market, named "Protector Demo". It can add a password check for some specified apps or activities, how could that be? I think my problem can use that method if we know how it works:D
Yu Zou
A: 

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:

http://android.git.kernel.org/?p=platform/development.git;a=blob;f=apps/SpareParts/src/com/android/spare_parts/SpareParts.java;h=c00cc516a3aace20918113ef26b82367fde5a231;hb=HEAD

HXCaine
+1  A: 

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);
        }
    };
}
Yu Zou