Android 查看手機中所有進程


  真機測試的時候發現DDMS對進程的顯示很不給力,一些進程管理工具又不顯示包名。

  所以就自己寫了一個小程序,查看自己手機中的進程,顯示當前時間和進程的包名:

  程序運行截圖:

  布局:

<LinearLayout 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"
    android:orientation="vertical" >

    <Button
        android:id="@+id/updateBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Update ProcessInfos" />

    <TextView
        android:id="@+id/time"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:textStyle="bold" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16sp" 
            android:padding="5dp"/>
    </ScrollView>

</LinearLayout>

  主要代碼:

package com.example.helloprocess;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class HelloProcessActivity extends Activity
{
    private TextView mTextView = null;
    private TextView mTime = null;
    private Button mButton = null;
    private String mText = "";

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hello_process);

        mTextView = (TextView) findViewById(R.id.text);
        mTime = (TextView) findViewById(R.id.time);
        mButton = (Button) findViewById(R.id.updateBtn);

        mButton.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                updateProcessInfo();
            }
        });

    }

    private void updateProcessInfo()
    {
        mText = "";
        mTextView.setText(mText);

        // 獲取ActivityManager
        ActivityManager activityManager = (ActivityManager) this
                .getSystemService(Context.ACTIVITY_SERVICE);

        // 更新時間
        updateTimeInfo();

        // 獲取進程信息***************************************************
        List<RunningAppProcessInfo> infos = activityManager
                .getRunningAppProcesses();

        for (RunningAppProcessInfo info : infos)
        {
            String name = info.processName;

            mText = mTextView.getText().toString();
            mText += name + "\n\n";
            mTextView.setText(mText);

        }

    }

    private void updateTimeInfo()
    {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 設置日期格式
        String time = df.format(new Date());
        System.out.println(time);// new Date()為獲取當前系統時間

        mTime.setText(time);

    }

}

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM