Android中列出手机的所有应用和剩余内存使用情况

来源:互联网 发布:淘宝编辑宝贝流程 编辑:程序博客网 时间:2024/06/22 23:21

//获取应用信息 很简单的、相信不用解释大家都能看明白

package com.android.icompus_gkd.ui.live;

import java.util.Collections;
import java.util.List;

import com.android.icompus_gkd.R;

import android.R.animator;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class SoftActivity extends Activity implements Runnable ,OnItemClickListener{
 
 private static final String SCHEME = "package";
 //使用可以进入应用管理
// private static final String APP_PKG_NAME_21 = "com.android.settings.ApplicationPkgName";
 private static final String APP_PKG_NAME_22 = "pkg";
// private static final String APP_DETAILS_PACKAGE_NAME = "com.android.settings";
// private static final String APP_DETAILS_CLASS_NAME = "com.android.settings.InstalledAppDetails";
 
// private List<Map<String, Object>> list = null;
 private ListView softlist = null;
 private ProgressDialog pd;
 private Context context;
 private PackageManager mPackageManager;
 private List<ResolveInfo> mAllApps;

 
 private   ActivityManager am;
 
 private Button  bt;
 
 
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  setContentView(R.layout. activity__soft);
  //CPU使用情况
  bt=(Button) this.findViewById(R.id.tv);
//  setTitle("文件管理器");
  
  context = this;
     mPackageManager = getPackageManager();
     
  softlist = (ListView) findViewById(R.id.lv_soft);
          //加载条
   pd = ProgressDialog.show(this, "请稍候..", "正在收集软件信息...", true,false);
   Thread thread = new Thread(this);
      thread.start();

  super.onCreate(savedInstanceState);
 }
 /**
  * 检查系统应用程序,添加到应用列表中
  */
 private void bindMsg(){
  //应用过滤条件
     Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
     mAllApps = mPackageManager.queryIntentActivities(mainIntent, 0);
     softlist.setAdapter(new MyAdapter(context, mAllApps));
     softlist.setOnItemClickListener(this);
     //按报名排序
        Collections.sort(mAllApps, new ResolveInfo.DisplayNameComparator(mPackageManager));
       
 }
 
 @Override
 public void run() {
  bindMsg();
     handler.sendEmptyMessage(0);

 }
 private Handler handler = new Handler() {
        @SuppressLint("HandlerLeak")
  public void handleMessage(Message msg) {
            pd.dismiss();
        }
    };
   
    class MyAdapter extends BaseAdapter{

  private Context context;
     private List<ResolveInfo> resInfo;
//      获得该资源图片在R文件中的值 (对应于android:icon属性)
//     private ImageView app_icon=null;
//     private TextView app_tilte=null,app_des=null;
     private ResolveInfo res;
  private LayoutInflater infater=null;
     
     public MyAdapter(Context context, List<ResolveInfo> resInfo) {   
   this.context = context;
   this.resInfo = resInfo;
    infater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  }

  @Override
  public int getCount() {
   
   return resInfo.size();
  }

  @Override
  public Object getItem(int arg0) {
   
   return arg0;
  }

  @Override
  public long getItemId(int position) {
   
   return position;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
   
  // View view = null; 
         ViewHolder holder = null; 
         if (convertView == null || convertView.getTag() == null) { 
          convertView = infater.inflate(R.layout.soft_row, null); 
             holder = new ViewHolder(convertView); 
             convertView.setTag(holder); 
         }  
         else{ 
        //     view = convertView ; 
             holder = (ViewHolder) convertView.getTag() ; 
         } 
         //获取应用程序包名,程序名称,程序图标
         res = resInfo.get(position);
         //应用图标
         holder.appIcon.setImageDrawable(res.loadIcon(mPackageManager)); 
          // 应用程序名称
         holder.tvAppLabel.setText("        "+res.loadLabel(mPackageManager).toString());
         holder.tvAppLabel.setText(res.loadLabel(mPackageManager).toString());
          //应用包名
         holder.tvPkgName.setText(res.activityInfo.packageName+'\n'+res.activityInfo.name);
   return convertView;
   
  }
    }
    //设定界面布局
    class ViewHolder { 
        ImageView appIcon; 
        TextView tvAppLabel; 
        TextView tvPkgName; 
 
        public ViewHolder(View view) { 
            this.appIcon = (ImageView) view.findViewById(R.id.img); 
            this.tvAppLabel = (TextView) view.findViewById(R.id.name); 
            this.tvPkgName = (TextView) view.findViewById(R.id.desc); 
        } 
    } 
    /**
     * 单击应用程序后进入系统应用管理界面
     */
 @Override
 public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
  
  ResolveInfo res = mAllApps.get(position);
  String pkg=res.activityInfo.packageName;
  Intent intent = new Intent();
//  Intent intent=MainActivity.this.getPackageManager().getLaunchIntentForPackage("packageName");
//  startActivity(intent);
  
  final int apiLevel = Build.VERSION.SDK_INT;
  
  //通过queryIntentActivities()方法,查询Android系统的所有具备ACTION_MAIN和CATEGORY_LAUNCHER 的Intent的应用程序,点击后,能启动该应用,
  if (apiLevel >= 9) {
   //getPacageManager()调用这给方法可以获得手机上安装的应用程序、
   intent = this.getPackageManager(). getLaunchIntentForPackage(pkg);
    startActivity(intent);
   
   
    //进入程序管理中、
//   intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); 
//         Uri uri = Uri.fromParts(SCHEME, pkg, null); 
//         intent.setData(uri); 
  }
 }
  
  public  void onCheck(View v){
      
   final ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
     
      ActivityManager.MemoryInfo info = new ActivityManager.MemoryInfo();  
     
      activityManager.getMemoryInfo(info);   
  
  
     
      bt.setText("系统剩余内存:"+(info.availMem >> 10)+"k");
//      bt.setText("系统是否处于低内存运行:"+info.lowMemory);
//      bt.setText("当系统剩余内存低于"+info.threshold+"时就看成低内存运行");
  
   
 }
 
    }
   
   
     
   
   


 

0 0