Android Studio 应用程序管理功能思路与代码

本文介绍了如何在Android Studio中实现程序管理功能,包括创建app_manager、app_item及popup_window布局,使用AppInfo类存储应用信息,通过适配器绑定ListView,利用线程和AppInfoService获取并分类应用,通过Handler处理加载完成后的显示,以及实现点击反馈和自定义对话框功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

程序管理功能:
点击按钮进入程序管理页面,在onCreate方法中绑定app_manager界面,因为该页面内有ListView,所以还需要一个app_item页面进行设置列表,因为点击列表对象需要弹出自定义卸载、打开、分享按钮的对话框,所以还要一个popup_window,layout所以总共需要三个。分别如下

<?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="match_parent">

    <TextView android:id="@+id/tv_title"
        style="@style/text_title_style"
        android:text="所  有  程  序"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="17dp" />

    <View style="@style/view_divide_line_style"
        android:layout_alignBottom="@id/tv_title"
        android:layout_alignParentStart="true" />

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/tv_title"
        android:layout_alignParentStart="true"
        android:layout_marginTop="15dp">


        <ListView
            android:id="@+id/lv_appmanage"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:cacheColorHint="#00000000"
            android:divider="#fff"
            android:fastScrollEnabled="true"
            android:dividerHeight="1.0dip"
            android:paddingLeft="3.0dip"
            android:paddingRight="3.0dip" />

        <RelativeLayout
            android:id="@+id/rl_loading"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_centerInParent="true">

                <ProgressBar
                    android:id="@+id/pb"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dip"
                    android:text="正在加载应用程序。。"
                    android:textColor="#fff"
                    android:textSize="22sp" />
            </LinearLayout>
        </RelativeLayout>
    </FrameLayout>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingBottom="8.0dip"
    android:paddingLeft="6.0dip"
    android:paddingRight="5.0dip"
    android:paddingTop="8.0dip" >

    <ImageView
        android:id="@+id/iv_appicon"
        android:layout_width="45dip"
        android:layout_height="45dip"
        android:layout_centerVertical="true"
        android:src="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/tv_appname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="4.0dip"
        android:layout_toRightOf="@id/iv_appicon"
        android:textColor="#fff"
        android:text="程序名称"
        android:textSize="16.0dip" />

    <TextView
        android:id="@+id/tv_appversion"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="4.0dip"
        android:textColor="#fff"
        android:layout_marginRight="10dp"
        android:text="1.0"
        android:textSize="16.0dip" />

</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:id="@+id/ll_popup_window"
    android:layout_height="wrap_content"
    android:background="@drawable/local_popup_bg"
    android:orientation="horizontal">

    <LinearLayout android:id="@+id/ll_uninstall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dip"
        android:layout_marginRight="15dip"
        android:layout_gravity="center_vertical"
        android:orientation="vertical" >

        <ImageView
            android:layout_width="35dip"
            android:layout_height="35dip"
            android:src="@drawable/img1" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="卸载"
            android:layout_gravity="center"
            android:textColor="#fff"
            android:textSize="12sp" />
    </LinearLayout>

    <View
        android:layout_width="1dip"
        android:layout_height="fill_parent"
        android:background="@drawable/local_popup_divider" />

    <LinearLayout android:id="@+id/ll_start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="15dip"
        android:layout_marginRight="15dip"
        android:orientation="vertical" >

        <ImageView
            android:layout_width="35dip"
            android:layout_height="35dip"
            android:src="@drawable/img2" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="启动"
            android:layout_gravity="center"
            android:textColor="#fff"
            android:textSize="12sp" />
    </LinearLayout>

    <View
        android:layout_width="1dip"
        android:layout_height="fill_parent"
        android:background="@drawable/local_popup_divider" />

    <LinearLayout android:id="@+id/ll_share"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="15dip"
        android:layout_marginRight="15dip"
        android:orientation="vertical" >

        <ImageView
            android:layout_width="35dip"
            android:layout_height="35dip"
            android:src="@drawable/img3" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="分享"
            android:layout_gravity="center"
            android:textColor="#fff"
            android:textSize="12sp" />
    </LinearLayout>

</LinearLayout>

接下里由于需要每个应用程序的各个数据对象,所以需要一个AppInfo类

public class AppInfo {    
    private Drawable app_icon;
    private String app_name;
    private String app_version;
    private String packagename;
    private boolean isUserApp;

    public AppInfo() {
        super();
        // TODO Auto-generated constructor stub
        }
        public AppInfo(Drawable app_icon, String app_name, String app_version,
                   String packagename) {
            super();
            this.app_icon = app_icon;
            this.app_name = app_name;
            this.app_version = app_version;
            this.packagename = packagename;
        }


        public AppInfo(Drawable app_icon, String app_name, String app_version,String packagename, boolean isUserApp) {
            super();
            this.app_icon = app_icon;
            this.app_name = app_name;
            this.app_version = app_version;
            this.packagename = packagename;
            this.isUserApp = isUserApp;
        }
        public Drawable getApp_icon() {
            return app_icon;
        }
        public void setApp_icon(Drawable app_icon) {
            this.app_icon = app_icon;
        }
        public String getApp_name() {
            return app_name;
        }
        public void setApp_name(String app_name) {
            this.app_name = app_name;
        }
        public String getApp_version() {
            return app_version;
        }
        public void setApp_version(String app_version){
            this.app_version = app_version;
        }
        public String getPackagename() {
            return packagename;
        }
        public void setPackagename(String packagename) {
            this.packagename = packagename;
        }

        public boolean isUserApp() {
            return isUserApp;
        }
        public void setUserApp(boolean isUserApp) {
            this.isUserApp = isUserApp;
        }

        @Override
        public String toString() {
            return "AppInfo [app_icon=" + app_icon + ", app_name=" + app_name+ ", app_version=" + app_version + ", packagename="+ packagename + ", isUserApp=" + isUserApp + "]";
        }
  }

因为是ListView,所以需要一个适配器来绑定列表内容

public class AppManageAdapter extends BaseAdapter {

    private Context context;
    private LayoutInflater mInflater;
    private List<AppInfo> appInfos;

    public void setAppInfos(List<AppInfo> appInfos) {
        this.appInfos = appInfos;
    }


    public AppManageAdapter(Context context, List<AppInfo> appInfos){
        this.context = context;
        this.appInfos = appInfos;
        mInflater = LayoutInflater.from(context);
    }

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

    @Override
    public Object getItem(int position) {
        return appInfos.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        /*
        * 1.得到控件
        * 2.得到数据
        * 3.绑定数据
        * */

        View view = null;
        if (convertView != null){
            view = convertView;
        }else{
            view = mInflater.inflate(R.layout.app__item,null);
        }

        ImageView iv_appicon = (ImageView) view.findViewById(R.id.iv_appicon);
        TextView tv_appname = (TextView) view.findViewById(R.id.tv_appname);
        TextView tv_appversion = (TextView) view.findViewById(R.id.tv_appversion);

        AppInfo appInfo = appInfos.get(position);
        iv_appicon.setImageDrawable(appInfo.getApp_icon());
        tv_appname.setText(appInfo.getApp_name());
        tv_appversion.setText(appInfo.getApp_version());

        return view;
    }
}

接下来就是AppManager里面的工作了,在onCreate方法中调用initData方法来进行列表加载更新,当没有加载出来时,中心显示进度条和正在加载,之后定义一个线程,生成一个AppInfo类型的泛型集合AppInfos,再写一个AppInfoService类,里面专门写获取应用程序信息和给AppInfos填充数据的方法,由于我们有个功能是点击标题更换程序类型显示,所以在该类中再写一个方法用来判断是否是用户程序。这时候在线程中就可以去分类用户程序和系统程序了。接下来就是判断程序信息是否加载完成了,所以用到了Handler,然后在handleMessage中判断接收到的信息,当加载完成时隐藏进度条和中心文字,这时显示事件基本完成,剩下的就是点击反馈内容了,类来继承OnItemClickListener,重写onItemClickfangfa,点击显示自定义对话框,通过ScaleAnimation类添加动画,然后给卸载、启动、分享三个按钮绑定监听器,进行点击反馈。下面就是服务类和主页面类

public class AppInfoService {
    private Context context;
    private PackageManager packageManager;

    public AppInfoService(Context context){
        this.context = context;
        packageManager = context.getPackageManager();
    }

    //得到所有的应用程序
    public List<AppInfo> getAppInfos(){
        List<AppInfo> appInfos = new ArrayList<AppInfo>();
        List<ApplicationInfo> applicationInfos = packageManager.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);
                for(ApplicationInfo info:applicationInfos){
                    AppInfo appInfo = new AppInfo();

                    Drawable app_icon = info.loadIcon(packageManager);
                    appInfo.setApp_icon(app_icon);

                    String app_name = info.loadLabel(packageManager).toString();
                    appInfo.setApp_name(app_name);

                    String packageName = info.packageName;
                    appInfo.setPackagename(packageName);
                    try {
                        PackageInfo packageInfo = packageManager.getPackageInfo(packageName,0);
                        String app_version = packageInfo.versionName;
                        appInfo.setApp_version(app_version);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    boolean isUserApp = filterApp(info);
                    appInfo.setUserApp(isUserApp);
                    appInfos.add(appInfo);




                }
        return  appInfos;
    }
    //判断应用程序是否是用户程序
    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;
    }
}
public class AppManager extends Activity implements View.OnClickListener {

    protected static final int SUCCESS_GET_APPLICATION = 0;

    private RelativeLayout rl_loading;
    private ListView lv_appmanage;
    private PackageManager packageManager;
    private AppInfoService appInfoService;
    private List<AppInfo> appInfos;
    private List<AppInfo> userAppInfos;
    private boolean isAllApp = true;
    private AppManageAdapter mAdapter;
    private TextView tv_title;

    private PopupWindow mPopupWindow;


    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case SUCCESS_GET_APPLICATION:
                    if(mAdapter != null){
                        if(isAllApp){
                            mAdapter.setAppInfos(appInfos);
                        }else{
                            mAdapter.setAppInfos(userAppInfos);
                        }
                        mAdapter.notifyDataSetChanged();
                        rl_loading.setVisibility(View.GONE);
                    }else{
                        //给Listview绑定数据,隐藏加载的进度条
                        mAdapter = new AppManageAdapter(getApplicationContext(),appInfos);
                        lv_appmanage.setAdapter(mAdapter);
                        /*setVisibility方法中可以放置三个参数
                        1.View.GONE控件消失不占控件
                        2.View.INVISIBLE 控件消失占据空间
                        3.View.VISIBLE控件显示*/
                        rl_loading.setVisibility(View.GONE);
                    }

                    break;

                default:
                    break;
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.app_manager);
        rl_loading = (RelativeLayout) findViewById(R.id.rl_loading);
        lv_appmanage = (ListView) findViewById(R.id.lv_appmanage);
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setOnClickListener(this);

        appInfoService = new AppInfoService(this);
        //得到包管理器
        packageManager = getPackageManager();

        initData();

        //给ListView绑定项目点击监听器
        lv_appmanage.setOnItemClickListener(new MyOnItemClickListener());
    }

    private void initData(){
        rl_loading.setVisibility(View.VISIBLE);
        new Thread(){
            public void run() {
                appInfos = appInfoService.getAppInfos();

                userAppInfos = new ArrayList<AppInfo>();
                for(AppInfo appInfo:appInfos){
                    if(appInfo.isUserApp()){
                        userAppInfos.add(appInfo);
                    }
                }
                Message msg = new Message();
                msg.what = SUCCESS_GET_APPLICATION;
                mHandler.sendMessage(msg);
            };
        }.start();

    }

    private final class MyOnItemClickListener implements AdapterView.OnItemClickListener{
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            View contentView = View.inflate(getApplicationContext(), R.layout.popup_window, null);
            LinearLayout ll_uninstall = (LinearLayout) contentView.findViewById(R.id.ll_uninstall);
            LinearLayout ll_start = (LinearLayout) contentView.findViewById(R.id.ll_start);
            LinearLayout ll_share = (LinearLayout) contentView.findViewById(R.id.ll_share);

            LinearLayout ll_popup_window = (LinearLayout) contentView.findViewById(R.id.ll_popup_window);

            //动画效果,前四个数据是设置对话框大小,后四个数据设置动画样式
            ScaleAnimation animation = new ScaleAnimation(0, 1.0f, 0, 1.0f, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0.5f);
            animation.setDuration(200);
            ll_popup_window.startAnimation(animation);

            MyOnClickListener l = new MyOnClickListener(position);
            ll_uninstall.setOnClickListener(l);
            ll_start.setOnClickListener(l);
            ll_share.setOnClickListener(l);


            mPopupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT, DensityUtil.px2dip(getApplicationContext(),68));
            int[] arrayOfInt = new int[2];
            view.getLocationInWindow(arrayOfInt);
            int x = arrayOfInt[0] +  DensityUtil.px2dip(getApplicationContext(), 150);
            int y = arrayOfInt[1];

            //1 指定popupwindow的背景   2 popupwindow能够获得焦点
            mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
            mPopupWindow.setFocusable(true);
            mPopupWindow.showAtLocation(view, Gravity.LEFT|Gravity.TOP, x, y);
        }
    }
    private final class MyOnClickListener implements View.OnClickListener {

        private int position;

        public MyOnClickListener(int position) {

            this.position = position;
        }

        public void onClick(View v) {

            AppInfo appInfo = (AppInfo) mAdapter.getItem(position);
            String packageName = appInfo.getPackagename();
            int id = v.getId();
            switch (id) {
                case R.id.ll_uninstall:
                    //系统应用不能被卸载
                    if(!appInfo.isUserApp()){
                        Toast.makeText(getApplicationContext(), "系统应用不能被卸载", Toast.LENGTH_SHORT).show();
                    }else{
                        //自身不能被卸载
                        if(packageName.equals(getPackageName())){
                            Toast.makeText(getApplicationContext(), "自身应用不能被卸载", Toast.LENGTH_SHORT).show();
                        }else{
                            Intent uninstall_intent = new Intent();
                            uninstall_intent.setAction(Intent.ACTION_DELETE);
                            uninstall_intent.setData(Uri.parse("package:" + packageName));
                        //startActivity(uninstall_intent);
                            startActivityForResult(uninstall_intent, 100);
                        }
                    }
                    break;
                case R.id.ll_start:
                    try {
                        PackageInfo packgeInfo = packageManager.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
                        ActivityInfo[] activites = packgeInfo.activities;
                        if(activites == null || activites.length == 0){
                            Toast.makeText(getApplicationContext(), "该应用程序不能被启动", Toast.LENGTH_SHORT).show();
                        }else{
                            ActivityInfo activityInfo = activites[0];
                            String name = activityInfo.name;
                            ComponentName component = new ComponentName(packageName, name);
                            Intent start_intent = new Intent();
                            start_intent.setComponent(component);
                            startActivity(start_intent);
                        }
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        Toast.makeText(getApplicationContext(), "该应用程序不能被启动", Toast.LENGTH_SHORT).show();
                    }
                    break;
                case R.id.ll_share:
                    Intent share_intent = new Intent();
                    share_intent.setAction(Intent.ACTION_SEND);
                    share_intent.setType("text/plain");
                    share_intent.putExtra(Intent.EXTRA_SUBJECT, "f分享");
                    share_intent.putExtra(Intent.EXTRA_TEXT, "HI 推荐您使用一款软件:" + appInfo.getApp_name());
                    share_intent = Intent.createChooser(share_intent, "分享");
                    startActivity(share_intent);
                    break;

                default:
                    break;
            }
            mPopupWindow.dismiss();
        }

    }

    //因为卸载完反馈的是100,所以用此方法来进行列表更新
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 100){
            initData();
        }
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch(id){
            case R.id.tv_title:
                if (isAllApp){
                    mAdapter.setAppInfos(userAppInfos);
                    mAdapter.notifyDataSetChanged();
                    tv_title.setText("用  户  程  序");
                    isAllApp = false;
                }else {
                    mAdapter.setAppInfos(appInfos);
                    mAdapter.notifyDataSetChanged();
                    tv_title.setText("所  有  程  序");
                    isAllApp = true;
                }
                break;
            default:

                break;
        }
    }
}

最后不要忘了在AndroidMainifest中进行注册。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值