相信大家手機中都有應用管理器,很多第三方軟件也是應用管理,但是感覺他們做的功能太多,附加內容太多,不是那種簡潔的應用,於是自己就在網上看了看一些人做的,結合自己的思想,做了一個自己喜歡的那種簡潔的應用管理器,非常的使用。我先介紹一下這個應用管理器,主要有哪些功能吧!
功能主要分爲四塊:運行、分享、加鎖、卸載。點擊標題欄可以切換所有應用和用戶應用,非常的簡潔實用,當然了實現起來也不是很難。下面我們就先看看運行的效果圖吧:
主界面 第一次加鎖設置密碼
手機運行加鎖程序時驗證密碼 解除應用加鎖
上面就是系統最後的效果,由於實現起來步驟也不少,我們接下來將一步步的去實現,今天我們的任務就是實現將手機裏面的所有應用程序取出來,放到列表並顯示出來。
下面先看一下項目的目錄結構:
一、我們先說一下佈局文件:
主界面佈局文件main_layout.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="vertical"
android:background="@drawable/logo"
>
<LinearLayout
android:id="@+id/ll_app_logo"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:gravity="center_vertical|center_horizontal"
android:background="@drawable/blue_btn"
android:orientation="vertical"
>
<TextView
android:id="@+id/tv_app_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:textSize="22sp"
android:text="@string/app_list_lago"
android:focusable="false"/>
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/list_back"
>
<ListView
android:id="@+id/lv_app_manager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="@android:color/transparent"
android:layout_marginTop="10dip" />
<LinearLayout
android:id="@+id/ll_app_manager_progress"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical"
android:visibility="gone"
android:background="@drawable/logo">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_list_loading"
android:textSize="18sp"
android:textColor="#ffbc04e5"/>
</LinearLayout>
</FrameLayout>
</LinearLayout>
建一個佈局文件用於保存應用程序的信息,最後在列表中顯示app_manager_item.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"
android:gravity="center_vertical" >
<!-- android:scaleType="fitXY"把圖片按照指定的大小在View中顯示,
拉伸顯示圖片,不保持原比例,填滿View. -->
<ImageView
android:id="@+id/iv_app_manager_icon"
android:layout_width="60dip"
android:layout_height="60dip"
android:scaleType="fitXY"
android:contentDescription="HELLO"/>
<TextView
android:id="@+id/tv_app_manager_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:textSize="21sp"
android:textColor="#ff000000"/>
<ImageView
android:id="@+id/iv_app_lock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:scaleType="fitXY"
android:src="@drawable/unlock"
android:contentDescription="@string/hello_world"/>
</LinearLayout>
二、下面我們就要寫java文件,來處理並顯示出來效果:
先建一個AppInfo類,代碼如下:
package com.xh.ui;
import android.graphics.drawable.Drawable;
/**
* 類名稱:AppInfo
* 類描述:應用程序類,包括了程序相關屬性
* 創建人:LXH
* 創建時間:創建時間:2013-11-5 下午3:31:00
*/
public class AppInfo {
private Drawable icon;
private String appName;
private String packageName;
private boolean isSystemApp;
private long codesize;
public long getCodesize() {
return codesize;
}
public void setCodesize(long codesize) {
this.codesize = codesize;
}
public Drawable getIcon() {
return icon;
}
public void setIcon(Drawable icon) {
this.icon = icon;
}
public String getAppName() {
return appName;
}
public void setAppName(String appName) {
this.appName = appName;
}
public String getPackageName() {
return packageName;
}
public void setPackageName(String packageName) {
this.packageName = packageName;
}
public boolean isSystemApp() {
return isSystemApp;
}
public void setSystemApp(boolean isSystemApp) {
this.isSystemApp = isSystemApp;
}
}
建一個獲取應用程序信息的AppInfoProvider類,代碼如下:
package com.xh.ui;
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;
/**
* 類名稱:AppInfoProvider
* 類描述:獲取應用程序的相關信息
* 創建人:LXH
* 創建時間:創建時間:2013-11-5 下午3:31:00
*/
public class AppInfoProvider {
private PackageManager packageManager;
//獲取一個包管理器
public AppInfoProvider(Context context){
packageManager = context.getPackageManager();
}
/**
*獲取系統中所有應用信息,
*並將應用軟件信息保存到list列表中。
**/
public List<AppInfo> getAllApps(){
List<AppInfo> list = new ArrayList<AppInfo>();
AppInfo myAppInfo;
//獲取到所有安裝了的應用程序的信息,包括那些卸載了的,但沒有清除數據的應用程序
List<PackageInfo> packageInfos = packageManager.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);
for(PackageInfo info:packageInfos){
myAppInfo = new AppInfo();
//拿到包名
String packageName = info.packageName;
//拿到應用程序的信息
ApplicationInfo appInfo = info.applicationInfo;
//拿到應用程序的圖標
Drawable icon = appInfo.loadIcon(packageManager);
//拿到應用程序的大小
//long codesize = packageStats.codeSize;
//Log.i("info", "-->"+codesize);
//拿到應用程序的程序名
String appName = appInfo.loadLabel(packageManager).toString();
myAppInfo.setPackageName(packageName);
myAppInfo.setAppName(appName);
myAppInfo.setIcon(icon);
if(filterApp(appInfo)){
myAppInfo.setSystemApp(false);
}else{
myAppInfo.setSystemApp(true);
}
list.add(myAppInfo);
}
return list;
}
/**
*判斷某一個應用程序是不是系統的應用程序,
*如果是返回true,否則返回false。
*/
public boolean filterApp(ApplicationInfo info){
//有些系統應用是可以更新的,如果用戶自己下載了一個系統的應用來更新了原來的,它還是系統應用,這個就是判斷這種情況的
if((info.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0){
return true;
}else if((info.flags & ApplicationInfo.FLAG_SYSTEM) == 0){//判斷是不是系統應用
return true;
}
return false;
}
}
最後在MainActivity類填寫代碼,處理結果,代碼如下:
package com.xh.ui;
import java.util.List;
import com.example.appmanager.R;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.annotation.SuppressLint;
import android.app.Activity;
/**
* 類名稱:MainActivity
* 類描述:系統主頁面
* 創建人:LXH
* 創建時間:2013-11-5 下午3:31:00
*/
public class MainActivity extends Activity {
private static final int GET_ALL_APP_FINISH = 1;
private ListView lv_app_manager;//應用信息列表
private LinearLayout ll_app_manager_progress; //進度條
private AppInfoProvider provider;
private AppManagerAdapter adapter;
private List<AppInfo> list;
@SuppressLint("HandlerLeak")
private Handler handler = new Handler()
{
public void handleMessage(Message msg)
{
switch(msg.what)
{
case GET_ALL_APP_FINISH :
//進度條設置爲不可見
ll_app_manager_progress.setVisibility(View.GONE);
adapter = new AppManagerAdapter();
lv_app_manager.setAdapter(adapter);
break;
default :
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
lv_app_manager = (ListView) findViewById(R.id.lv_app_manager);
ll_app_manager_progress = (LinearLayout) findViewById(R.id.ll_app_manager_progress);
ll_app_manager_progress.setVisibility(View.VISIBLE);
/**
* //開一個線程用於完成對所有應用程序信息的搜索
* 當搜索完成之後,就把一個成功的消息發送給Handler,
* 然後handler把搜索到的數據設置進入listview裏面 .
* */
new Thread()
{
public void run()
{
provider = new AppInfoProvider(MainActivity.this);
list = provider.getAllApps();
Message msg = new Message();
msg.what = GET_ALL_APP_FINISH;
handler.sendMessage(msg);
};
}.start();
}
//======================================================================
private class AppManagerAdapter extends BaseAdapter
{
@Override
public int getCount()
{
return list.size();
}
@Override
public Object getItem(int position)
{
return list.get(position);
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
AppInfo info = list.get(position);
if(convertView == null)
{
View view = View.inflate(MainActivity.this, R.layout.app_manager_item, null);
AppManagerViews views = new AppManagerViews();
views.iv_app_icon = (ImageView) view.findViewById(R.id.iv_app_manager_icon);
views.tv_app_name = (TextView) view.findViewById(R.id.tv_app_manager_name);
views.iv_app_icon.setImageDrawable(info.getIcon());
views.tv_app_name.setText(info.getAppName());
view.setTag(views);
return view;
}
else
{
AppManagerViews views = (AppManagerViews) convertView.getTag();
views.iv_app_icon.setImageDrawable(info.getIcon());
views.tv_app_name.setText(info.getAppName());
return convertView;
}
}
}
/**
* 用來優化listview的類
* */
private class AppManagerViews
{
ImageView iv_app_icon;
TextView tv_app_name;
}
}
好了,到此主要步驟和核心的代碼就寫完了,大家自己的項目上稍加修改,就可以了,資源文件大家可以從下面的鏈接中下載。今天的任務就結束了,由於內容比較簡單,就不做詳細說明了,下一篇我們將利用popupWindow類實現提示對話框!歡迎大家繼續關注!(如下載資源後,遇到問題,請在博客裏面留言,這樣就可以大家一起交流了!)