如何以编程方式显示在android中运行的进程? [英] How to show processes running in android programmatically?

查看:77
本文介绍了如何以编程方式显示在android中运行的进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在android程序的帮助下显示正在运行的进程列表。但它没有显示任何内容。怎么办?代码如下:



MainActivity.java:



package com.sysinfo.procinfo; < br $>


import java.util.List;



import android.app.Activity;

import android.app.ActivityManager;

import android.app.ActivityManager.RunningAppProcessInfo;

import android.os.Bundle;

import android.widget.ListView;

import android.widget.Toast;



/ **

*应用程序的起点。本课程将显示当前正在运行的

*流程列表。

*



* @version 1.0 29 -01-2015

*

* /

公共类MainActivity扩展活动{



private ListView mProcessListView = null;



@Override

protected void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);



setContentView(R.layout.activity_main);



initViews ();



loadProcessInfo();

}



private void initViews(){



mProcessListView =(ListView)findViewById(R.id.processListView);

}



/ **

*获取当前正在运行的进程列表。

* /

private void loadProcessInfo ()


ActivityManager manager =(ActivityManager)getSystemService(ACTIVITY_SE RVICE);

List< runningappprocessinfo> runningProcesses = manager

.getRunningAppProcesses();



if(runningProcesses!= null&& runningProcesses.size()> 0 ){

mProcessListView

.setAdapter(new ListAdapter(this,runningProcesses));

} else {

Toast.makeText(getApplicationContext(),

找不到正在运行的进程。,Toast.LENGTH_LONG)。show();

}

}

}



ListAdapter.java



包com.sysinfo。 procinfo;



import java.util.List;



import android.app.ActivityManager.RunningAppProcessInfo;

导入android.content.Context;

导入android.view.LayoutInflater;

导入android.view.View;

import android.view.ViewGroup;

import android.widget.ArrayAdapter;

import android.widget.TextView;



/ **

*要处理的适配器列表中的数据。



* @version 1.0 29-01-2015

*

* /

公共类ListAdapter扩展ArrayAdapter< runningappprocessinfo> {



private Context mContext = null;

private List< runningappprocessinfo> mProcessList = null;



public ListAdapter(Context context,List< runningappprocessinfo> values){



super (context,R.layout.list_item_process,values);



this.mContext = context;

this.mProcessList = values;

}



@Override

public View getView(int position,View convertView,ViewGroup parent){



LayoutInflater inflater =(LayoutInflater)mContext

.getSystemService(Context.LAYOUT_INFLATER_SERVICE);



查看rowView = inflater.inflate(R.layout.list_item_process,parent,

false);



TextView appName =(TextView) rowView.findViewById(R.id.processName);

appName.setText(mProcessList.get(position).processName);



返回rowView;

}



}



activity_process_info.xml



< relativelayout xmlns:android =http://schemas.android.com/apk/res/android>

xmlns:tools =http://schemas.android.com/工具

android:layout_width =match_parent

android:layout_height =match_parent

tools:context =。ProcessorInfoActivity>



< textview>

android:id =@ + id / View2

android:layout_width =wrap_removed

android:layout_height =wrap_removed

android:layout_alignParentLeft =true

android:layout_alignParentTop =true

android:lineSpacingExtra =10dp

android:text =处理器信息。

android:textSize =30dp

android:textStyle =bold/>



< listview> android:id =@ + id / listView1

android:layout_width =match_parent

android:layout_height =wrap_content

android :layout_alignParentLeft =true

android:layout_below =@ + id / View2

android:layout_marginLeft =38dp>





它正在编译代码,当在模拟器上运行时,它只显示标题Process Info和页面的其余部分为空。我的代码缺少什么?

I want to show the list of running processes with the help of android program.But it's not showing anything.What to do?? The code is given below:

MainActivity.java:

package com.sysinfo.procinfo;

import java.util.List;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.Toast;

/**
* Starting point of the app. This class will show the list of currently running
* process.
*

* @version 1.0 29-01-2015
*
*/
public class MainActivity extends Activity {

private ListView mProcessListView = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initViews();

loadProcessInfo();
}

private void initViews() {

mProcessListView = (ListView) findViewById(R.id.processListView);
}

/**
* Get the list of currently running process.
*/
private void loadProcessInfo() {

ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List<runningappprocessinfo> runningProcesses = manager
.getRunningAppProcesses();

if (runningProcesses != null && runningProcesses.size() > 0) {
mProcessListView
.setAdapter(new ListAdapter(this, runningProcesses));
} else {
Toast.makeText(getApplicationContext(),
"No running process found.", Toast.LENGTH_LONG).show();
}
}
}

ListAdapter.java

package com.sysinfo.procinfo;

import java.util.List;

import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

/**
* The adapter to handle the data in list.

* @version 1.0 29-01-2015
*
*/
public class ListAdapter extends ArrayAdapter<runningappprocessinfo> {

private Context mContext = null;
private List<runningappprocessinfo> mProcessList = null;

public ListAdapter(Context context, List<runningappprocessinfo> values) {

super(context, R.layout.list_item_process, values);

this.mContext = context;
this.mProcessList = values;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View rowView = inflater.inflate(R.layout.list_item_process, parent,
false);

TextView appName = (TextView) rowView.findViewById(R.id.processName);
appName.setText(mProcessList.get(position).processName);

return rowView;
}

}

activity_process_info.xml

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android">
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ProcessorInfoActivity" >

<textview>
android:id="@+id/View2"
android:layout_width="wrap_removed"
android:layout_height="wrap_removed"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:lineSpacingExtra="10dp"
android:text="Processor Info."
android:textSize="30dp"
android:textStyle="bold" />

<listview> android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/View2"
android:layout_marginLeft="38dp" >


It is compiling the code and when running on emulator,it shows only the heading Process Info and rest of the page blank.Where my code is lacking?

推荐答案

http:// androidbite。 blogspot.in/2013/07/android-display-running-application.html [ ^ ]







见此链接
http://androidbite.blogspot.in/2013/07/android-display-running-application.html[^]



See this link


引用:

Android 5.0+ kill getRunningTasks(int)和getRunningAppProcesses()。这两种方法现在都已弃用,只返回调用者的应用程序进程。



Android 5.0引入了UsageStatsManager,可以访问设备使用历史记录和统计信息。此API需要权限android.permission.PACKAGE_USAGE_STATS,这是系统级权限,不会授予第三方应用程序。但是,声明权限意味着打算使用API​​,设备用户可以通过Settings应用程序授予权限。

Android 5.0+ killed getRunningTasks(int) and getRunningAppProcesses(). Both of these methods are now deprecated and only return the caller’s application process.

Android 5.0 introduced UsageStatsManager which provides access to device usage history and statistics. This API requires the permission android.permission.PACKAGE_USAGE_STATS, which is a system-level permission and will not be granted to third-party apps. However, declaring the permission implies intention to use the API and the user of the device can grant permission through the Settings application.





我发现这个库哪个可能支持版本6及以下的Android设备。



GitHub - jaredrummler / AndroidProcesses:已弃用 [ ^ ]



看看这是否有帮助。

如果您找到任何其他简化解决方案,请分享。



I found this library which may support android devices for versions 6 and below.

GitHub - jaredrummler/AndroidProcesses: DEPRECATED[^]

See if this helps.
Please share if you find any other simplified solutions.


这篇关于如何以编程方式显示在android中运行的进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆