applist

package com.proj.applist;


import android.content.Intent;
import android.graphics.drawable.Drawable;


public class AppInfo {
private String appLabel;    
   private Drawable appIcon ;  
   private Intent intent ;     
   private String pkgName ;    
   
   public AppInfo(){}
   
   public String getAppLabel() {
       return appLabel;
   }
   public void setAppLabel(String appName) {
       this.appLabel = appName;
   }
   public Drawable getAppIcon() {
       return appIcon;
   }
   public void setAppIcon(Drawable appIcon) {
       this.appIcon = appIcon;
   }
   public Intent getIntent() {
       return intent;
   }
   public void setIntent(Intent intent) {
       this.intent = intent;
   }
   public String getPkgName(){
       return pkgName ;
   }
   public void setPkgName(String pkgName){
       this.pkgName=pkgName ;
   }

}


package com.proj.applist;


import java.util.List;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class BrowseApplicationInfoAdapter extends BaseAdapter {


private List<AppInfo> mlistAppInfo = null;
    
    LayoutInflater infater = null;
    
    public BrowseApplicationInfoAdapter(Context context,  List<AppInfo> apps) {
        infater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mlistAppInfo = apps ;
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        System.out.println("size" + mlistAppInfo.size());
        return mlistAppInfo.size();
    }
    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return mlistAppInfo.get(position);
    }
    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }
    @Override
    public View getView(int position, View convertview, ViewGroup arg2) {
        System.out.println("getView at " + position);
        View view = null;
        ViewHolder holder = null;
        if (convertview == null || convertview.getTag() == null) {
            view = infater.inflate(R.layout.browse_app_item, null);
            holder = new ViewHolder(view);
            view.setTag(holder);
        } 
        else{
            view = convertview ;
            holder = (ViewHolder) convertview.getTag() ;
        }
        AppInfo appInfo = (AppInfo) getItem(position);
        holder.appIcon.setImageDrawable(appInfo.getAppIcon());
        holder.tvAppLabel.setText(appInfo.getAppLabel());
        holder.tvPkgName.setText(appInfo.getPkgName());
        return view;
    }


    class ViewHolder {
        ImageView appIcon;
        TextView tvAppLabel;
        TextView tvPkgName;


        public ViewHolder(View view) {
            this.appIcon = (ImageView) view.findViewById(R.id.imgApp);
            this.tvAppLabel = (TextView) view.findViewById(R.id.tvAppLabel);
            this.tvPkgName = (TextView) view.findViewById(R.id.tvPkgName);
        }
    }


}

package com.proj.applist;


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;


public class MainActivity extends Activity implements OnItemClickListener{


private ListView listview = null;


   private List<AppInfo> mlistAppInfo = null;


   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.broswe_app_list);


       listview = (ListView) findViewById(R.id.listviewApp);
       mlistAppInfo = new ArrayList<AppInfo>();
       queryAppInfo(); // 鏌ヨ鎵€鏈夊簲鐢ㄧ▼搴忎俊鎭?
       BrowseApplicationInfoAdapter browseAppAdapter = new BrowseApplicationInfoAdapter(
               this, mlistAppInfo);
       listview.setAdapter(browseAppAdapter);
       listview.setOnItemClickListener(this);
   }
   // 鐐瑰嚮璺宠浆鑷宠搴旂敤绋嬪簭
   public void onItemClick(AdapterView<?> arg0, View view, int position,
           long arg3) {
       // TODO Auto-generated method stub
       Intent intent = mlistAppInfo.get(position).getIntent();
       startActivity(intent);
   }
   // 鑾峰緱鎵€鏈夊惎鍔ˋctivity鐨勪俊鎭紝绫讳技浜嶭aunch鐣岄潰
   public void queryAppInfo() {
       PackageManager pm = this.getPackageManager(); //鑾峰緱PackageManager瀵硅薄
       Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
       mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
       // 閫氳繃鏌ヨ锛岃幏寰楁墍鏈塕esolveInfo瀵硅薄.
       List<ResolveInfo> resolveInfos = pm
               .queryIntentActivities(mainIntent, 0);//0 Launcher涓殑搴旂敤,PackageManager.MATCH_DEFAULT_ONLY鎵€鏈夊簲鐢?


            Log.d("count","appList_count:"+resolveInfos.size());
       // 璋冪敤绯荤粺鎺掑簭 锛?鏍规嵁name鎺掑簭
       // 璇ユ帓搴忓緢閲嶈锛屽惁鍒欏彧鑳芥樉绀虹郴缁熷簲鐢紝鑰屼笉鑳藉垪鍑虹涓夋柟搴旂敤绋嬪簭
       Collections.sort(resolveInfos,new ResolveInfo.DisplayNameComparator(pm));
       if (mlistAppInfo != null) {
           mlistAppInfo.clear();
           for (ResolveInfo reInfo : resolveInfos) {
               String activityName = reInfo.activityInfo.name; // 鑾峰緱璇ュ簲鐢ㄧ▼搴忕殑鍚姩Activity鐨刵ame
               String pkgName = reInfo.activityInfo.packageName; // 鑾峰緱搴旂敤绋嬪簭鐨勫寘鍚?
               String appLabel = (String) reInfo.loadLabel(pm); // 鑾峰緱搴旂敤绋嬪簭鐨凩abel
               Drawable icon = reInfo.loadIcon(pm); // 鑾峰緱搴旂敤绋嬪簭鍥炬爣
               // 涓哄簲鐢ㄧ▼搴忕殑鍚姩Activity 鍑嗗Intent
               Intent launchIntent = new Intent();
               launchIntent.setComponent(new ComponentName(pkgName,
                       activityName));
               // 鍒涘缓涓€涓狝ppInfo瀵硅薄锛屽苟璧嬪€?
               AppInfo appInfo = new AppInfo();
               appInfo.setAppLabel(appLabel);
               appInfo.setPkgName(pkgName);
               appInfo.setAppIcon(icon);
               appInfo.setIntent(launchIntent);
               mlistAppInfo.add(appInfo); // 娣诲姞鑷冲垪琛ㄤ腑
           }
       }
   }


}

<?xml version="1.0"?>


-<RelativeLayout tools:context=".MainActivity" android:paddingTop="@dimen/activity_vertical_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingBottom="@dimen/activity_vertical_margin" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android">


<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/hello_world"/>


</RelativeLayout>

<?xml version="1.0" encoding="UTF-8"?>


-<LinearLayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">

<ListView android:layout_height="fill_parent" android:layout_width="fill_parent" android:id="@+id/listviewApp"/>


</LinearLayout>


<?xml version="1.0" encoding="UTF-8"?>


-<LinearLayout android:layout_height="50dip" android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">


<ImageView android:layout_height="fill_parent" android:layout_width="wrap_content" android:id="@+id/imgApp"/>




-<RelativeLayout android:layout_height="40dip" android:layout_width="fill_parent" android:layout_marginLeft="10dip">


<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/tvLabel" android:text="AppLable : "/>


<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/tvAppLabel" android:layout_marginLeft="3dip" android:text="Label" android:textColor="#FFD700" android:layout_toRightOf="@id/tvLabel"/>


<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/tvName" android:text="包名:" android:layout_below="@id/tvLabel"/>


<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/tvPkgName" android:textColor="#FFD700" android:layout_below="@id/tvAppLabel" android:layout_alignLeft="@id/tvAppLabel"/>


</RelativeLayout>


</LinearLayout>

### 关于 `applist` 的数据结构与处理 #### 数据结构定义 在 Kotlin 中,如果涉及 `applist` 的数据结构设计,通常会基于具体的应用场景来构建模型。例如,在引用中提到的 `AppBean` 类[^3],它是一个简单的数据容器类,用于表示应用程序的基本信息: ```kotlin class AppBean { var packageName: String = "" var name: String = "" } ``` 此数据结构可以作为 `applist` 的基础单元。假设 `applist` 是一个列表形式的数据集合,则其可能的声明方式如下所示: ```kotlin val applist: MutableList<AppBean> = mutableListOf() ``` 这里使用了 Kotlin 提供的可变列表 `MutableList` 来存储多个 `AppBean` 对象。 --- #### 数据处理方法 对于 `applist` 的数据处理,常见的操作包括但不限于以下几个方面: 1. **数据初始化** 可以通过循环或其他逻辑向 `applist` 添加数据项。以下是示例代码: ```kotlin val app1 = AppBean().apply { packageName = "com.example.app1" name = "Example App 1" } val app2 = AppBean().apply { packageName = "com.example.app2" name = "Example App 2" } applist.add(app1) applist.add(app2) ``` 2. **数据查询** 查询特定条件下的应用数据是一项常见需求。例如,可以根据包名筛选符合条件的应用程序: ```kotlin val filteredApps = applist.filter { it.packageName.contains("example") } println(filteredApps.map { "${it.name} (${it.packageName})" }) ``` 3. **数据更新** 更新现有数据可以通过索引访问或过滤器完成。以下是对某个应用名称进行修改的例子: ```kotlin applist.find { it.packageName == "com.example.app1" }?.name = "Updated Example App 1" ``` 4. **数据分析** 如果需要统计某些属性的信息,比如计算总共有多少个应用或者按照某种规则分类,可以利用内置函数实现: ```kotlin val totalApps = applist.size val groupedByNameLength = applist.groupBy { it.name.length } println("Total apps: $totalApps") println("Grouped by name length: $groupedByNameLength") ``` 5. **数据导出** 将处理后的数据保存到文件、数据库或者其他持久化介质上也是重要的一步。下面展示如何将 `applist` 转换为 JSON 字符串以便后续传输或存储: ```kotlin import com.google.gson.Gson val gson = Gson() val jsonString = gson.toJson(applist) println(jsonString) ``` --- #### 结合其他技术栈扩展功能 除了上述基本操作外,还可以结合其他工具和技术进一步增强 `applist` 的功能性。例如: - 使用 Vue.js 和 JEECG Boot 实现前端界面动态渲染背景图片的功能[^4]。这可以通过绑定样式属性的方式完成,如以下代码片段所示: ```html <div v-for="item in applist" :key="item.packageName" :style="{ 'background-image': 'url(' + item.url + ')' }"> {{ item.name }} </div> ``` - 配置定时任务定期抓取最新应用数据并存入数据库[^1]。这种情况下,可以借助 Spring Batch 或 Quartz Scheduler 完成自动化流程管理。 --- #### 总结 综上所述,`applist` 的数据结构及其处理主要围绕着定义清晰的实体对象(如 `AppBean`)、合理运用集合类型以及灵活调用各种算法展开。无论是本地调试还是实际项目部署阶段,都应注重代码质量与性能优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值