博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
取得正在运行的Activity
阅读量:7238 次
发布时间:2019-06-29

本文共 2567 字,大约阅读时间需要 8 分钟。

 

 

在main.xml中:

 

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

  xmlns:android="http://schemas.android.com/apk/res/android"

  android:orientation="vertical"

  android:layout_width="fill_parent"

  android:layout_height="fill_parent"

  android:background="#3399ff">

  <ListView

     android:id="@+id/tasklist"

     android:layout_gravity="center_horizontal"

     android:layout_width="fill_parent"

     android:layout_height="wrap_content" />

</LinearLayout>

 

 

 

 

 

在MyActivityRun.java中:

 

package com.li.activityrun;

 

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

 

import android.app.Activity;

import android.app.ActivityManager;

import android.content.Context;

import android.os.Bundle;

import android.widget.ArrayAdapter;

import android.widget.ListAdapter;

import android.widget.ListView;

 

public class MyActivityRun extends Activity {

  private ListView tasklist = null ;

  private ListAdapter adapter = null ;

  private List<String> all = new ArrayList<String>() ;

  private ActivityManager activityManager = null ;

  private List<ActivityManager.RunningTaskInfo> allTaskInfo ;

  @Override

  public void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);

     super.setContentView(R.layout.main);

     this.tasklist = (ListView) super.findViewById(R.id.tasklist) ;

     this.activityManager = (ActivityManager) super

         .getSystemService(Context.ACTIVITY_SERVICE);

     this.listActivity() ;

  }

 

  private void listActivity() {

     this.allTaskInfo = this.activityManager.getRunningTasks(30); // 返回30条

     Iterator<ActivityManager.RunningTaskInfo> iterInfo = this.allTaskInfo.iterator() ;

     while(iterInfo.hasNext()) {

       ActivityManager.RunningTaskInfo task = iterInfo.next() ;

       this.all.add("【ID = " + task.id + " 】 "

            + task.baseActivity.getClassName());

     }

     this.adapter = new ArrayAdapter<String>(this,

         android.R.layout.simple_list_item_1, this.all);

     this.tasklist.setAdapter(this.adapter) ;

  }

}

 

 

 

 

 

 

在AndroidManifest.xml中修改权限:

 

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.li.activityrun"

    android:versionCode="1"

    android:versionName="1.0" >

 

    <uses-sdk

        android:minSdkVersion="8"

        android:targetSdkVersion="15" />

    <uses-permission android:name="android.permission.GET_TASKS"/>

 

    <application

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

            android:name=".MyActivityRun"

            android:label="@string/title_activity_my_activity_run" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

 

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>

 

</manifest>

 

 

 

你可能感兴趣的文章
PEACHPIE 0.9.11 版本发布,可以上生产了
查看>>
异常检测——局部异常因子(Local Outlier Factor ,LOF)算法
查看>>
记录一次广州白云区项目数据库连接失败问题的解决过程
查看>>
干货:Vue粒子特效(vue-particles插件)
查看>>
Silverlight自定义数据绑定控件应该如何处理IEditableObject和IEditableCollectionView对象...
查看>>
加密PDF为只读模式
查看>>
让你编写的控件库在 XAML 中有一个统一的漂亮的命名空间(xmlns)和命名空间前缀...
查看>>
MySQL数据库的锁详解【转】
查看>>
ip route 解释
查看>>
【转】Android中保持Service的存活
查看>>
Consul功能简介
查看>>
IdentityServer4实战 - API与IdentityServer的交互过程解析
查看>>
Delphi编程 -- 使用CPUID指令获取CPU信息(转自大富翁)
查看>>
Android setRequestedOrientation用法
查看>>
面向对象三大基本特性,五大基本原则
查看>>
更改窗口图标并将其显示在任务栏
查看>>
包含的语句
查看>>
正则表达式-匹配标点符号
查看>>
osworkflow descriptor 解析 重要概念
查看>>
Edmonds_Karp 算法 (转)
查看>>