實戰(zhàn)開發(fā)中時不時會涉及到有關(guān)本地 app 的操作嗓化,在此奉上一些渣文字 and 渣代碼~~
PackageManager類
本地app主要是通過PackageManager這個類來管理的蟆湖,它的功能包括——
- 安裝,卸載劲室,查詢應(yīng)用
- 查詢應(yīng)用組件(就是四大組件啦很洋,學(xué)Android的都知道)信息
- 添加喉磁,刪除协怒,查詢應(yīng)用權(quán)限
- 清除用戶數(shù)據(jù)孕暇,緩存隧哮,代碼段
PackageManager類可以通過getPackageManager()方法獲取沮翔,需要一個上下文(Context)環(huán)境鉴竭。
PackageManager類的常用方法
這些方法涉及到PackageInfo和ApplicationInfo類,后面會講——
**ApplicationInfo getApplicationInfo(String packageName, int flags) **
參數(shù)為app包名+flag標(biāo)記(通常0即可)
返回與包名對應(yīng)的ApplicationInfo對象璧眠;需要處理NameNotFoundException異常**PackageInfo getPackageInfo(String packageName, int flags) **
參數(shù)為包名+標(biāo)記
返回對應(yīng)的PackageInfo對象;需要處理NameNotFoundException異常List<PackageInfo> getInstalledPackages(int flags)
參數(shù)為標(biāo)記灾螃,有時你應(yīng)該根據(jù)需要腰鬼,對其進(jìn)行過濾
如果不過濾直接傳0,那會返回所有(系統(tǒng)+非系統(tǒng))的PackageInfo對象集合
另外就是還有一些有關(guān)ResolveInfo類(集合)的方法彼硫,此類直接指向<activity>拧篮,<receiver>,<service>等節(jié)點赏参!
已經(jīng)超出本文范圍就不詳述了~~(哼纫溃,懶就直說嘛)
PackageInfo類
此類用于手動獲取AndroidManifest.xml文件信息
注意它已實現(xiàn)Parcelable接口紊浩,因此可直接通過Intent或者Bundle傳遞坊谁!
常用方法——
String packageName()
返回此Info對應(yīng)的包名ApplicationInfo applicationInfo()
返回對應(yīng)的ApplicationInfo對象
ApplicationInfo類
此類繼承自PackageItemInfo類(AndroidManifest.xml里所有文件的基類哦),可以調(diào)用<label>,<icon>关划,<meta-data>等節(jié)點的信息
注意它已實現(xiàn)Parcelable接口裤翩。
常用方法——
Drawable loadIcon(PackageManager pm)
參數(shù)為PackageManager對象,返回對應(yīng)app的圖標(biāo)(Drawable對象)CharSequence loadLabel(PackageManager pm)
參數(shù)為PackageManager對象择份,返回對應(yīng)app的應(yīng)用名
Demo
廢話8完上代碼凤价,這是一個列表顯示本機(jī)所有app(過濾掉了系統(tǒng)自帶的)的demo利诺。點擊條目立倍,可啟動條目對應(yīng)的app。
注意這只是一個demo而已,沒有優(yōu)化(主要是圖像加載這一塊),跑起來略卡乐导!
真正的app管理應(yīng)用,應(yīng)該引入UIL或者Picasso一類的加載庫進(jìn)行圖標(biāo)加載,有時還需要重寫RecycleView類的onScrollStateChanged()方法;應(yīng)用列表要按一定規(guī)則排序规丽,點擊打開應(yīng)用時最好再添個切換動畫。在此就不贅述了~~(又他喵懶了)
gradle依賴
注意appcompat-v7包和design包的版本號不能照抄艘狭,它的值應(yīng)不大于buildToolsVersion的版本號
compile 'com.android.support:appcompat-v7:24.0.0'
compile 'com.android.support:design:24.0.0'
compile 'com.makeramen:roundedimageview:2.2.1'
compile 'com.jakewharton:butterknife:7.0.1'
主布局activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="com.example.jin.localapp.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/main_rcv"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
條目布局item_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp">
<com.makeramen.roundedimageview.RoundedImageView
android:id="@+id/item_icon_iv"
android:layout_margin="12dp"
android:layout_centerVertical="true"
android:layout_width="32dp"
android:layout_height="32dp" />
<TextView
android:id="@+id/item_name_tv"
android:textSize="17dp"
android:layout_toRightOf="@+id/item_icon_iv"
android:layout_marginTop="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/item_package_tv"
android:textSize="14dp"
android:layout_toRightOf="@+id/item_icon_iv"
android:layout_alignParentBottom="true"
android:layout_marginBottom="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:src="@drawable/item_arrow"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<View
android:background="#dddddd"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="1dp"/>
</RelativeLayout>
條目中箭頭的代碼item_arrow.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="32dp"
android:height="32dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#dddddd"
android:pathData="M9.59,16.34l4.58,-4.59 -4.58,-4.59L10,5.75l6,6 -6,6z"/>
</vector>
主界面MainActivity.java
package com.example.jin.localapp;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
import butterknife.Bind;
import butterknife.ButterKnife;
public class MainActivity extends AppCompatActivity {
@Bind(R.id.main_rcv)
RecyclerView mainRcv;
private List<PackageInfo> mList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
initData();
}
private void initData() {
mList = new ArrayList<>();
List<PackageInfo> list = getPackageManager().getInstalledPackages(0);//獲取已安裝的全部應(yīng)用
for (PackageInfo info : list) {
if ((info.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
mList.add(info);//只添加非系統(tǒng)應(yīng)用
}
}
mainRcv.setLayoutManager(new LinearLayoutManager(this));
mainRcv.setHasFixedSize(true);
mainRcv.setAdapter(new AppAdapter(this, mList));
}
}
適配器AppAdapter.java
package com.example.jin.localapp;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.makeramen.roundedimageview.RoundedImageView;
import java.util.List;
import butterknife.Bind;
import butterknife.ButterKnife;
/**
* Created by Jin on 2016/11/8.
*/
public class AppAdapter extends RecyclerView.Adapter<AppAdapter.AppHolder> {
private Context context;
private List<PackageInfo> appList;
private LayoutInflater inflater;
private PackageManager manager;
public AppAdapter(Context context, List<PackageInfo> appList) {
this.context = context;
this.appList = appList;
inflater = LayoutInflater.from(context);
manager = context.getPackageManager();
}
@Override
public AppHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new AppHolder(inflater.inflate(R.layout.item_app, parent, false));
}
@Override
public int getItemCount() {
return appList.size();
}
@Override
public void onBindViewHolder(AppHolder holder, int position) {
final PackageInfo info = appList.get(position);
holder.itemIconIv.setBackground(info.applicationInfo.loadIcon(manager));//應(yīng)用圖標(biāo)
holder.itemNameTv.setText(info.applicationInfo.loadLabel(manager));//名稱
holder.itemPackageTv.setText(info.packageName);//包名
holder.view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(manager.getLaunchIntentForPackage(info.packageName));//根據(jù)包名啟動此應(yīng)用
context.startActivity(intent);
}
});
}
static class AppHolder extends RecyclerView.ViewHolder {
@Bind(R.id.item_icon_iv)
RoundedImageView itemIconIv;
@Bind(R.id.item_name_tv)
TextView itemNameTv;
@Bind(R.id.item_package_tv)
TextView itemPackageTv;
View view;
AppHolder(View view) {
super(view);
ButterKnife.bind(this, view);
this.view = view;
}
}
}