开发者

Can an Android application know when another Android application is running?

开发者 https://www.devze.com 2023-03-07 07:00 出处:网络
Assume Applications Foo an Eggs are on the same Android device.It\'s possible for either application to get a list of all applications on the device.Is it p开发者_如何学JAVAossible for one application

Assume Applications Foo an Eggs are on the same Android device. It's possible for either application to get a list of all applications on the device. Is it p开发者_如何学JAVAossible for one application to know if another application has run and for how long?


You can get a list of installed applications by using the PackageManager. Code from here:

public class AppList extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        PackageManager pm = this.getPackageManager();

        Intent intent = new Intent(Intent.ACTION_MAIN, null);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);

        List list = pm.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED);
        for (ResolveInfo rInfo : list) {
            Log.w("Installed Applications", rInfo.activityInfo.applicationInfo.loadLabel(pm).toString());
        }
    }
}

To see the currently running apps, you can use the ActivityManager.


This post explains how you can achieve that functionality in a generic way.

This post and this one have snippets of an Activity that lists the running applications, using ActivityManager's getRunningAppProcesses().

This post explains how to get a list of all installed applications and then to choose one to run.


Take a look at the ActivityManager.

0

精彩评论

暂无评论...
验证码 换一张
取 消