Android本地app操作相关基础

wang0335 8年前
   <p>实战开发中时不时会涉及到有关本地app的操作,在此奉上一些渣文字and渣代码~~</p>    <h3><strong>PackageManager类</strong></h3>    <p>本地app主要是通过 <strong>PackageManager</strong> 这个类来管理的,它的功能包括——</p>    <ul>     <li> <p>安装,卸载,查询应用</p> </li>     <li> <p>查询应用组件(就是四大组件啦,学Android的都知道)信息</p> </li>     <li> <p>添加,删除,查询应用权限</p> </li>     <li> <p>清除用户数据,缓存,代码段</p> </li>    </ul>    <p>PackageManager类可以通过 <strong>getPackageManager()</strong> 方法获取,需要一个上下文(Context)环境。</p>    <h3><strong>PackageManager类的常用方法</strong></h3>    <p>这些方法涉及到 <strong>PackageInfo</strong> 和 <strong>ApplicationInfo</strong> 类,后面会讲)——</p>    <ul>     <li> <p><strong>ApplicationInfo getApplicationInfo(String packageName, int flags)</strong></p> </li>    </ul>    <p>参数为app包名+flag标记(通常0即可)</p>    <p>返回与包名对应的ApplicationInfo对象;需要处理NameNotFoundException异常</p>    <ul>     <li> <p><strong>PackageInfo getPackageInfo(String packageName, int flags)</strong></p> </li>    </ul>    <p>参数为包名+标记</p>    <p>返回对应的PackageInfo对象;需要处理NameNotFoundException异常</p>    <ul>     <li> <p><strong>List<PackageInfo> getInstalledPackages(int flags)</strong></p> </li>    </ul>    <p>参数为标记,有时你应该根据需要,对其进行过滤</p>    <p>如果不过滤直接传0,那会返回所有(系统+非系统)的PackageInfo对象集合</p>    <p>另外就是还有一些有关 <strong>ResolveInfo</strong> 类(集合)的方法,此类直接指向<activity>,<receiver>,<service>等节点!</p>    <p>已经超出本文范围就不详述了~~(哼,懒就直说嘛)</p>    <h3><strong>PackageInfo类</strong></h3>    <p>此类用于手动获取AndroidManifest.xml文件信息</p>    <p>注意它已实现Parcelable接口,因此可直接通过Intent或者Bundle传递!</p>    <p>常用方法——</p>    <p><strong>String packageName()</strong></p>    <p>返回此Info对应的包名</p>    <p><strong>ApplicationInfo applicationInfo()</strong></p>    <p>返回对应的ApplicationInfo对象</p>    <h3><strong>ApplicationInfo类</strong></h3>    <p>此类继承自 <strong>PackageItemInfo</strong> 类(AndroidManifest.xml里所有文件的基类哦),可以调用<label>,<icon>,<meta-data>等节点的信息</p>    <p>注意它已实现Parcelable接口。</p>    <p>常用方法——</p>    <p><strong>Drawable loadIcon(PackageManager pm)</strong></p>    <p>参数为PackageManager对象,返回对应app的图标(Drawable对象)</p>    <p><strong>CharSequence loadLabel(PackageManager pm)</strong></p>    <p>参数为PackageManager对象,返回对应app的应用名</p>    <h3><strong>Demo</strong></h3>    <p>废话8完上代码,这是一个列表显示本机所有app(过滤掉了系统自带的)的demo。点击条目,可启动条目对应的app。</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/f933ed4a1b5a98242ad2017f3a43dc51.png"></p>    <p style="text-align:center">Screenshot_2016-11-08-13-09-11.png</p>    <p>注意这只是一个demo而已,没有优化(主要是图像加载这一块),跑起来略卡!</p>    <p>真正的app管理应用,应该引入 <strong>UIL</strong> 或者 <strong>Picasso</strong> 一类的加载库进行图标加载,有时还需要重写 <strong>RecycleView</strong> 类的 <strong>onScrollStateChanged()</strong> 方法;应用列表要按一定规则排序,点击打开应用时最好再添个切换动画。在此就不赘述了~~(又他喵懒了)</p>    <h3><strong>gradle依赖</strong></h3>    <p>注意appcompat-v7包和design包的版本号不能照抄,它的值应不大于buildToolsVersion的版本号</p>    <pre>  <code class="language-java">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'</code></pre>    <p><strong>主布局activity_main.xml</strong></p>    <pre>  <code class="language-java"><?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></code></pre>    <p><strong>条目布局item_main.xml</strong></p>    <pre>  <code class="language-java"><?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></code></pre>    <p><strong>条目中箭头的代码item_arrow.xml</strong></p>    <pre>  <code class="language-java"><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></code></pre>    <p><strong>主界面MainActivity.java</strong></p>    <pre>  <code class="language-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);//获取已安装的全部应用                  for(PackageInfo info : list) {                          if ((info.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {                                  mList.add(info);//只添加系统应用                          }                  }                  mainRcv.setLayoutManager(new LinearLayoutManager(this));                  mainRcv.setHasFixedSize(true);                  mainRcv.setAdapter(new AppAdapter(this, mList));          }  }</code></pre>    <p><strong>适配器AppAdapter.java</strong></p>    <pre>  <code class="language-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));//应用图标          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));//根据包名启动此应用                  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;          }      }  }</code></pre>    <p> </p>    <p>来自:http://www.jianshu.com/p/19529489a166</p>    <p> </p>