• 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 + "
    
    ";
                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);
    
        }
    
    }
  • 相关阅读:
    [改善Java代码]在equals中使用getClass进行类型判断
    [改善Java代码]equals应该考虑null值的情景
    [改善Java代码]覆写equals方法时不要识别不出自己
    [改善Java代码] 推荐使用序列化实现对象的拷贝
    [改善Java代码]避免对象的浅拷贝
    [改善Java代码]让工具类不可实例化
    [改善Java代码]建议40:匿名类的构造函数很特殊
    [改善Java代码]使用匿名类的构造函数
    [改善Java代码]使用静态内部类提高封装性
    [改善Java代码]构造函数尽量简化
  • 原文地址:https://www.cnblogs.com/mengdd/p/3170087.html
Copyright © 2020-2023  润新知