123.s1-手机软件信息的展示

手机软件信息的展示,首先软件大致有几个特征:图标,包名,名称,大小,位置等。首先写一个入口activiy,再在主的手机软件展示的activity里面写逻辑文件,需要依赖到工具类AppInfos.java来获取到所有的需要展示的特征,通过集合来返回数据到activity进行展示,先创建一个bean初始化一个app所需要的特征。

activity_app_manager.xml展示所有的app条目的信息

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <TextView 
		style="@style/TitleStyle"
    	android:text="软件管理"
        />
    <LinearLayout 
        android:layout_width="match_parent"
    	android:layout_height="wrap_content"
    	android:orientation="horizontal"
        >
        <TextView 
            android:id="@+id/tv_rom"
            android:layout_width="wrap_content"
    		android:layout_height="wrap_content"
    		android:text="内存可用"
    		android:textSize="20sp"
    		android:textColor="@color/black"
    		android:layout_weight="1"
            />
		<TextView 
		    android:id="@+id/tv_sd"
            android:layout_width="wrap_content"
    		android:layout_height="wrap_content"
    		android:text="内存可用"
    		android:textSize="20sp"
    		android:textColor="@color/black"
    		android:layout_weight="1"
            />       
    </LinearLayout>
	<include 
	    android:id="@+id/list_view_callsafe"
	    layout="@layout/list_view_callsafe"
	    
	    />
</LinearLayout>

item_app_manager.xml手机信息展示的单个条目所需要展示信息的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
    

    <ImageView 
        android:id="@+id/iv_icon"
        android:layout_width="50dp"
    	android:layout_height="50dp"
    	android:background="@drawable/ic_launcher"
        />
    <LinearLayout 
        android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:layout_marginLeft="5dp"
    	android:orientation="vertical"
        >
        <TextView 
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
    		android:layout_height="wrap_content"
    		android:textSize="20sp"
    		android:textColor="@color/black"
    		android:singleLine="true"
    		android:text="手机卫视"
            />
        <TextView 
            android:id="@+id/tv_location"
            android:layout_width="match_parent"
    		android:layout_height="wrap_content"
    		android:textSize="16sp"
    		android:layout_marginTop="2dp"
    		android:singleLine="true"
    		android:text="手机内存"
            />     
    </LinearLayout>
    <RelativeLayout 
        android:layout_width="wrap_content"
   		android:layout_height="wrap_content"
        >
	   	<TextView
	   	    android:id="@+id/tv_apksize" 
	        android:layout_width="wrap_content"
	   		android:layout_height="wrap_content"
	   		android:textSize="16sp"
	   		android:layout_marginTop="10dp"
	   		android:layout_marginRight="10dp"
	   		android:singleLine="true"
	   		android:text="112M"
	   		android:layout_alignParentRight="true"
	            />	        
    </RelativeLayout>

</LinearLayout>

AppInfo.java手机信息的bean文件

package com.ldw.safe.bean;

import android.graphics.drawable.Drawable;

public class AppInfo {

	//图片的icon,Drawable比bitmap更广泛,后者只针对图,前者可以针对xml
	private Drawable icon;
	private String apkName;
	private long apkSize;//app的大小
	private boolean userApp;//true表示用户app,false表示系统app
	private boolean isRom;//app放置的位置
	private String apkPackageName;//app的包名
	public Drawable getIcon() {
		return icon;
	}
	public void setIcon(Drawable icon) {
		this.icon = icon;
	}
	public String getApkName() {
		return apkName;
	}
	public void setApkName(String apkName) {
		this.apkName = apkName;
	}
	public long getApkSize() {
		return apkSize;
	}
	public void setApkSize(long apkSize) {
		this.apkSize = apkSize;
	}
	public boolean isUserApp() {
		return userApp;
	}
	public void setUserApp(boolean userApp) {
		this.userApp = userApp;
	}
	public boolean isRom() {
		return isRom;
	}
	public void setRom(boolean isRom) {
		this.isRom = isRom;
	}
	public String getApkPackageName() {
		return apkPackageName;
	}
	public void setApkPackageName(String apkPackageName) {
		this.apkPackageName = apkPackageName;
	}
	@Override
	public String toString() {
		return "AppInfo [icon=" + icon + ", apkName=" + apkName + ", apkSize="
				+ apkSize + ", userApp=" + userApp + ", isRom=" + isRom
				+ ", apkPackageName=" + apkPackageName + "]";
	}
	
	
}

AppInfos.java获取手机所有的信息,并利用javabean封装到集合返回

package com.ldw.safe.engine;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;

import com.ldw.safe.bean.AppInfo;

/*
 * apps的基本信息
 */
public class AppInfos {


	//app的信息是多个,返回一个集合
	public static List<AppInfo> getAppInfos(Context context){
		
		ArrayList<AppInfo> packageAppInfos = new ArrayList<AppInfo>();
		
		//获取到包的管理者
		PackageManager packageManager = context.getPackageManager();
		//获取到所有的安装包
		List<PackageInfo> installedPackages= packageManager.getInstalledPackages(0);
		for(PackageInfo installedPackage : installedPackages){
			AppInfo appInfo = new AppInfo();
			
			//获取到应用程序的图标
			Drawable drawable = installedPackage.applicationInfo.loadIcon(packageManager);
			appInfo.setIcon(drawable);
			//获取到应用程序的名字
			String apkName = (String) installedPackage.applicationInfo.loadLabel(packageManager);
			appInfo.setApkName(apkName);
			//获取到应用程序的包名
			String apkPackageName = installedPackage.packageName;
			appInfo.setApkPackageName(apkPackageName);
			//获取到应用程序的路径
			String sourceDir = installedPackage.applicationInfo.sourceDir;
			//获取到应用程序的大小
			File file = new File(sourceDir);
			long apkSize = file.length();
			appInfo.setApkSize(apkSize);
			
			//第三方应用放在/data/app
			//系统应用放在/system/app
			//获取到安装程序的标记
			int flags = installedPackage.applicationInfo.flags;
			//和系统应用进行与运算
			if((flags & ApplicationInfo.FLAG_SYSTEM) != 0){
				//系统应用
				appInfo.setUserApp(false);
			}else{
				//第三方应用
				appInfo.setUserApp(true);
			}
			//判断位置
			if((flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0){
				//sd卡
				appInfo.setRom(false);
			}else{
				//rom手机内存
				appInfo.setRom(true);
			}
			
			packageAppInfos.add(appInfo);
			System.out.println("==============");
			System.out.println("名字"+ apkName);
			System.out.println("包名"+ apkPackageName);
			System.out.println("大小"+ apkSize);
		}
		return packageAppInfos;
	}
}

HomeActivity.java入口到手机应用展示的界面

package com.ldw.safe.Activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.ldw.safe.R;
import com.ldw.safe.utils.MD5Utils;

public class HomeActivity extends Activity{
	
	private GridView gv_home;
	
	private String[] mItems = new String[] {"手机防盗", "通讯卫士", "软件管理", "进程管理", "流量统计", "手机杀毒", "缓存清理", "高级工具", "设置中心"};
	private int[] mPics = new int[] {R.drawable.safe, R.drawable.callmsgsafe, R.drawable.app, 
			R.drawable.taskmanager, R.drawable.netmanager, R.drawable.sysoptimize, 
			R.drawable.trojan, R.drawable.atools, R.drawable.settings};

	private SharedPreferences mPref;
	
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        
        mPref = getSharedPreferences("config", MODE_PRIVATE);
        
        gv_home = (GridView) findViewById(R.id.gv_home);
        gv_home.setAdapter(new HomeAdapter());
        //监听gridview点击事件
        gv_home.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				switch(position){
				case 0:
					//手机防盗
					showPasswordDialog();
					break;
				case 1:
					//通讯卫士
					startActivity(new Intent(HomeActivity.this, CallSafeActivity.class));
					break;
				case 2:
					//软件管理
					startActivity(new Intent(HomeActivity.this, AppManagerActivity.class));
					break;
				case 7:
					//高级工具
					startActivity(new Intent(HomeActivity.this, AdvancedToolsActivity.class));
					break;			
				case 8:
					//设置中心
					startActivity(new Intent(HomeActivity.this, SettingActivity.class));
					break;
				default:
					break;
				}
			}
        	
        });
        
	}
	
	/*
	 * 显示密码的弹窗
	 */
	protected void showPasswordDialog(){
		//判断是否设置密码
		String savePassword = mPref.getString("password", null);
		if(!TextUtils.isEmpty(savePassword)){
			//输入密码的弹窗
			showPasswordInputDialog();
		}else{
			//如果没有设置过,弹出密码设置的弹窗
			showPasswordSetDialog();
		}

	}
	
	/*
	 * 输入密码框,设置过密码只需要登陆
	 */
	protected void showPasswordInputDialog(){
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		final AlertDialog dialog = builder.create();
		
		View view = View.inflate(this, R.layout.dialog_input_password, null);
		//dialog.setView(view);//自定义的view设置到dialog
		dialog.setView(view, 0, 0, 0, 0);//设置边距为0,兼容android2.3
		
		final EditText etPassword = (EditText) view.findViewById(R.id.et_password);
		
		//获取2个按键
		Button btnOk = (Button)view.findViewById(R.id.btn_ok);
		Button btnCancle = (Button)view.findViewById(R.id.btn_cancle);
		//监听ok按键
		btnOk.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				String password = etPassword.getText().toString();
				
				if(!TextUtils.isEmpty(password)){
					String savePassword = mPref.getString("password", null);
					if(MD5Utils.encode(password).equals(savePassword)){
						Toast.makeText(HomeActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
						//对话框消失
						dialog.dismiss();
						
						//跳转到手机防盗界面
						startActivity(new Intent(HomeActivity.this, LostAndFind.class));
					}else{
						Toast.makeText(HomeActivity.this, "登陆失败", Toast.LENGTH_SHORT).show();
					}
				}else{
					Toast.makeText(HomeActivity.this, "输入框不能是空的", Toast.LENGTH_SHORT).show();
				}
				
			}
			
		});
		//监听取消按键
		btnCancle.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				dialog.dismiss();
				
			}
			
		});
		
		dialog.show();
	}
	
	/*
	 * 设置密码的弹窗,以前没有设置密码
	 */
	protected void showPasswordSetDialog(){
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		final AlertDialog dialog = builder.create();
		
		View view = View.inflate(this, R.layout.dialog_set_password, null);
		//dialog.setView(view);//自定义的view设置到dialog
		dialog.setView(view, 0, 0, 0, 0);//设置边距为0,兼容android2.3
		
		final EditText etPassword = (EditText) view.findViewById(R.id.et_password);
		final EditText etPasswordConfirm = (EditText) view.findViewById(R.id.et_password_confirm);
		
		//获取2个按键
		Button btnOk = (Button)view.findViewById(R.id.btn_ok);
		Button btnCancle = (Button)view.findViewById(R.id.btn_cancle);
		//监听ok按键
		btnOk.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				String password = etPassword.getText().toString();
				String passwordConfirm = etPasswordConfirm.getText().toString();
				//TextUtils.isEmpty(password)判断password是不是空,可以避免都是空格
				if(!TextUtils.isEmpty(password) && !passwordConfirm.isEmpty()){
					if(password.equals(passwordConfirm)){
						Toast.makeText(HomeActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
						//保存密码到mPref
						mPref.edit().putString("password", MD5Utils.encode(password)).commit();
						//对话框消失
						dialog.dismiss();
						
						//跳转到手机防盗界面
						startActivity(new Intent(HomeActivity.this, LostAndFind.class));
					}else{
						Toast.makeText(HomeActivity.this, "两次密码不一致", Toast.LENGTH_SHORT).show();
					}
				}else{
					Toast.makeText(HomeActivity.this, "输入框不能是空的", Toast.LENGTH_SHORT).show();
				}
				
			}
			
		});
		//监听取消按键
		btnCancle.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				dialog.dismiss();
				
			}
			
		});
		
		dialog.show();
	}
	
	class HomeAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mItems.length;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return mItems[position];
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
			View view = View.inflate(HomeActivity.this, R.layout.home_list_item, null);
			ImageView iv_item = (ImageView) view.findViewById(R.id.iv_item);
			TextView tv_item = (TextView) view.findViewById(R.id.tv_item);
			
			iv_item.setImageResource(mPics[position]);
			tv_item.setText(mItems[position]);
			
			return view;
		}
		
	}

}

AppManagerActivity.java手机应用展示的逻辑文件

package com.ldw.safe.Activity;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.text.format.Formatter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.ldw.safe.R;
import com.ldw.safe.bean.AppInfo;
import com.ldw.safe.engine.AppInfos;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

/*
 * 软件管理
 */
public class AppManagerActivity extends Activity {
	//不用findViewById的方法填充
	@ViewInject(R.id.list_view_callsafe)
	private ListView list_view_callsafe;
	@ViewInject(R.id.tv_rom)
	private TextView tv_rom;
	@ViewInject(R.id.tv_sd)
	private TextView tv_sd;
	
	private List<AppInfo> appInfos;
	private AppManagerAdapter adapter;
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initUi();
        initData();
	}
	
	private class AppManagerAdapter extends BaseAdapter{

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

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

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

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view = null;
			ViewHoler holder;
			if(convertView == null){
				view = View.inflate(AppManagerActivity.this, R.layout.item_app_manager, null);
				holder = new ViewHoler();
				holder.iv_icon = (ImageView) view.findViewById(R.id.iv_icon);
				holder.tv_apksize = (TextView) view.findViewById(R.id.tv_apksize);
				holder.tv_location = (TextView) view.findViewById(R.id.tv_location);
				holder.tv_name = (TextView) view.findViewById(R.id.tv_name);
				view.setTag(holder);
			}else{
				view = convertView;
				holder = (ViewHoler) convertView.getTag();
			}
			AppInfo appInfo = appInfos.get(position);
			holder.iv_icon.setBackground(appInfo.getIcon());
			holder.tv_apksize.setText(Formatter.formatShortFileSize(AppManagerActivity.this, appInfo.getApkSize()));
			holder.tv_name.setText(appInfo.getApkName());
			if(appInfo.isRom()){
				//手机内存
				holder.tv_location.setText("手机内存");
			}else{
				//sd卡
				holder.tv_location.setText("sd卡内存");
			}
			return view;
		}
		
	}
	
	static class ViewHoler{
		ImageView  iv_icon;
		TextView tv_apksize;
		TextView tv_location;
		TextView tv_name;
	}
	
	private Handler handler = new Handler(){


		@Override
		public void handleMessage(Message msg){
			adapter = new AppManagerAdapter();
			list_view_callsafe.setAdapter(adapter);
		}
	};

	/*
	 * 初始化apk的数据
	 */
	private void initData() {
		//子线程中初始化,防止anr
		new Thread(){

			@Override
			public void run(){
				//获取到安装的所有的应用程序
				appInfos = AppInfos.getAppInfos(AppManagerActivity.this);
				handler.sendEmptyMessage(0);
				
			}
		}.start();
	}

	//初始化界面
	private void initUi() {
		setContentView(R.layout.activity_app_manager);
		//View viewById = findViewById(R.id.list_view_callsafe);
		//不用findViewById的方法填充
		ViewUtils.inject(this);
		//获取到rom内存的剩余空间
		long rom_freeSpace = Environment.getDataDirectory().getFreeSpace();
		//获取到sd卡的剩余空间
		long sd_freeSpace = Environment.getExternalStorageDirectory().getFreeSpace();
		
		System.out.println("内存可用" + rom_freeSpace + "sd卡可用" + sd_freeSpace);
		
		//格式化存储卡的大小
		tv_rom.setText("内存可用" + Formatter.formatFileSize(this, rom_freeSpace));
		tv_sd.setText("sd卡可用" + Formatter.formatFileSize(this, sd_freeSpace));
		
	}
}

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值