編輯:關於 Android 程式設計
前言:前幾天電腦怎麼樣都打不開 CSDN,折騰好久,今天下班回來突然又能開了,順手把週末做出來的效果貼上來。
這個 Demo 的功能包括:取得手機中所有安裝的應用程式,顯示圖示、名稱、更新時間與佔用空間,支援側滑刪除,以及點擊圖示分享 APK。
目標效果:

技術組合:RecyclerView + SwipeRevealLayout。用到的第三方元件有 Glide(圖片載入)、EventBus(元件通訊)與 SwipeRevealLayout(側滑動畫)。
核心是 PackageManager 的 getInstalledPackages:
private ArrayList<AppInfoModel> getData() {
ArrayList<AppInfoModel> list = new ArrayList<>();
PackageManager mPackageManager = getApplicationContext().getPackageManager();
packageInfoList = (ArrayList<PackageInfo>) mPackageManager.getInstalledPackages(0);
for (PackageInfo packageInfo : packageInfoList) {
AppInfoModel model = new AppInfoModel();
model.setName(getApplicationName(packageInfo.applicationInfo.packageName, mPackageManager));
model.setIcon(getAppliactionIcon(packageInfo, mPackageManager));
model.setTime(getDate(packageInfo.lastUpdateTime));
model.setSize(new File(packageInfo.applicationInfo.sourceDir).length() / 1024 / 1024 + "MB");
list.add(model);
}
return list;
}
三個輔助方法分別負責取名稱、取圖示與格式化時間:
/** 取得應用程式名稱 */
public String getApplicationName(String packageName, PackageManager packageManager) {
String applicationName = null;
try {
ApplicationInfo applicationInfo = packageManager.getApplicationInfo(packageName, 0);
applicationName = (String) packageManager.getApplicationLabel(applicationInfo);
} catch (PackageManager.NameNotFoundException e) {
// 忽略
}
return applicationName;
}
/** 取得應用程式圖示 */
public Drawable getAppliactionIcon(PackageInfo packageInfo, PackageManager packageManager) {
return packageInfo.applicationInfo.loadIcon(packageManager);
}
/** 把時間戳格式化為可讀字串 */
public static String getDate(long time) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date(time);
return simpleDateFormat.format(date);
}
提醒一個實務細節:getInstalledPackages 拿到的清單包含系統預安裝程式,數量動輒上百,直接丟進列表不加分類,使用者翻到手軟。實作時通常會用 applicationInfo.flags 的 FLAG_SYSTEM 過濾掉系統應用。
點擊事件從 Adapter 拋出,Activity 在主執行緒接收後啟動分享:
// 事件接收者:在主執行緒接收
public void onEventMainThread(ClickEvent event) {
switch (event.type) {
case ClickEvent.CLICK_ITEM:
int position = (int) event.data;
File apkFile = new File(packageInfoList.get(position).applicationInfo.sourceDir);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(apkFile));
startActivity(intent);
break;
}
}
public class DemoAdapter extends RecyclerView.Adapter<DemoAdapter.DemoViewHolder> {
private ArrayList<AppInfoModel> list = new ArrayList<>();
private final ViewBinderHelper viewBinderHelper = new ViewBinderHelper();
public void getData(ArrayList<AppInfoModel> data) {
list.clear();
if (data != null) {
list.addAll(data);
}
notifyDataSetChanged();
}
@Override
public DemoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item, parent, false);
return new DemoViewHolder(view);
}
@Override
public void onBindViewHolder(DemoViewHolder holder, int position) {
holder.setData(list.get(position));
}
public void saveStates(Bundle outState) {
viewBinderHelper.saveStates(outState);
}
public void restoreStates(Bundle inState) {
viewBinderHelper.restoreStates(inState);
}
@Override
public int getItemCount() {
return list != null ? list.size() : 0;
}
public class DemoViewHolder extends RecyclerView.ViewHolder {
private TextView name, time, size;
private ImageView icon;
private SwipeRevealLayout swipeRevealLayout;
private View deleteLayout;
public DemoViewHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.name);
time = (TextView) itemView.findViewById(R.id.time);
size = (TextView) itemView.findViewById(R.id.size);
icon = (ImageView) itemView.findViewById(R.id.icon);
swipeRevealLayout = (SwipeRevealLayout) itemView.findViewById(R.id.swipe_layout);
deleteLayout = itemView.findViewById(R.id.delete_layout);
icon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EventBus.getDefault().post(
new ClickEvent(ClickEvent.CLICK_ITEM, getAdapterPosition()));
}
});
deleteLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
list.remove(getAdapterPosition());
notifyItemRemoved(getAdapterPosition());
}
});
}
void setData(AppInfoModel model) {
if (model != null) {
// 以名稱作為 key,讓每列的側滑狀態各自獨立
viewBinderHelper.bind(swipeRevealLayout, model.getName());
name.setText(model.getName());
time.setText(model.getTime());
size.setText(model.getSize());
icon.setImageDrawable(model.getIcon());
}
}
}
}
這裡有個值得注意的設計:SwipeRevealLayout 在 RecyclerView 中會 recycling,如果不處理,滑到下面的列會繼承上面列的展開狀態。作者用 ViewBinderHelper 以名稱當 key 綁定狀態,正是為了解決這個問題。
<com.chauthai.swipereveallayout.SwipeRevealLayout
xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
android:id="@+id/swipe_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
app:dragEdge="right"
app:mode="same_level">
<!-- 底下藏著的刪除區塊 -->
<FrameLayout
android:id="@+id/delete_layout"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:background="#ffcc0000">
<TextView
android:layout_width="70dp"
android:layout_height="match_parent"
android:background="@android:color/holo_red_dark"
android:gravity="center"
android:text="Delete"
android:textColor="@android:color/white" />
</FrameLayout>
<!-- 上層正常顯示的內容 -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@mipmap/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_toRightOf="@+id/icon"
android:orientation="vertical">
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="middle"
android:singleLine="true"
android:textColor="@android:color/black"
android:textStyle="bold" />
<TextView
android:id="@+id/size"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textSize="10sp" />
<TextView
android:id="@+id/time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textColor="#aaaaaa"
android:textSize="10sp" />
</LinearLayout>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#E0E0E0" />
</LinearLayout>
</FrameLayout>
</com.chauthai.swipereveallayout.SwipeRevealLayout>
SwipeRevealLayout 的兩個子層順序不能顛倒:第一個是被藏在後面的內容(刪除按鈕),第二個才是前景內容。app:dragEdge 決定往哪個方向滑開,app:mode 則控制滑動行為。
元件專案頁面:
https://github.com/chthai64/SwipeRevealLayout
實作成果:

側滑效果:

分享效果:

另外補充一點:這裡的「刪除」只是把項目從清單中移除,並不會真的反安裝程式——那是系統保留給使用者的權限,第三方 App 無法代勞。真實的 App 管理工具通常只做「跳轉到系統設定頁」或「移除自己」這類事。
原始碼下載:
https://pan.baidu.com/s/1gftwfon
用 Android Studio 讀懂 Android 5.x 源碼:從 idegen 到索引完成的完整實踐
做整機開發,最磨人的往往不是寫程式,而是「讀程式」。AOSP 那棵龐大的源碼樹在傳統工具裡幾乎寸步難行,本文要聊的,是如何把這件事交給 Android Studio。 G
Redis 架構設計解析:啟動流程、持久化與鍵空間實作
一、前言 近期的專案開始導入 Redis。要用得好,不能只停在 API 層面,必須理解它「為什麼這樣設計」。本文以 main 函式為起點,順著初始化流程往下追,藉此理解各
Retrofit 入門到進階:用注解把網路請求寫成介面
先前整理過 OkHttp 與 Volley,這個系列的最後一篇留給 Retrofit。可以把它理解為 OkHttp 的「升級版」——它內部預設就是基於 OkHttp 封裝
Android Studio 技巧:自定義 Logcat 顯示顏色
Logcat 是我們日常調試 Android 最常用的工具之一。不過 Android Studio 默認的 Logcat 各分類顏色都一樣,verbose、debug、e