示例#1
0
 private void getAllGameApp() {
   List<PackageInfo> apps = new ArrayList<PackageInfo>();
   PackageManager pManager = getPackageManager();
   // 获取手机内所有应用
   List<PackageInfo> packlist = pManager.getInstalledPackages(0);
   for (int i = 0; i < packlist.size(); i++) {
     PackageInfo pak = packlist.get(i);
     // if()里的值如果==0则为自己装的程序,否则为系统工程自带
     if ((pak.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
       // 添加自己已经安装的应用程序
       apps.add(pak);
       String appName = pManager.getApplicationLabel(pak.applicationInfo).toString();
       if (MyApp.gnList.contains(appName)) {
         AppsItemInfo aii = new AppsItemInfo();
         aii.setIcon(pManager.getApplicationIcon(pak.applicationInfo));
         aii.setLabel(pManager.getApplicationLabel(pak.applicationInfo).toString());
         aii.setPackageName(pak.applicationInfo.packageName);
         String filePath = pak.applicationInfo.publicSourceDir;
         aii.setSize(
             Util.round(new File(filePath).length() * 1.0 / (1024 * 1024), 2, BigDecimal.ROUND_UP)
                 + "MB");
         appList.add(aii);
       }
     }
   }
 }
示例#2
0
    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {
      // TODO Auto-generated method stub
      ViewHolder holder;
      if (convertView == null) {
        holder = new ViewHolder();
        convertView = mInflater.inflate(R.layout.item_download, null, false);
        holder.iv = (ImageView) convertView.findViewById(R.id.item_download_icon);
        holder.tv_name = (TextView) convertView.findViewById(R.id.item_download_name);
        holder.tv_size = (TextView) convertView.findViewById(R.id.item_download_size);
        holder.startBtn = (Button) convertView.findViewById(R.id.item_download_start);
        convertView.setTag(holder);
      } else {
        holder = (ViewHolder) convertView.getTag();
      }
      final GameInfo gi = downloadList.get(position);
      if (gi != null) {
        imageLoader.displayImage(gi.getGame_ico(), holder.iv, options);
        holder.tv_name.setText(gi.getGame_name());
        holder.tv_size.setText(gi.getSize() + "M");
        final String filePath = gi.getFilePath();
        // final String filePath = "mnt/sdcard/test.apk";
        final String pName = Util.getPnameByApk(UserDownloadActivity.this, filePath);
        final boolean isInstalled = Util.isAppInstalled(UserDownloadActivity.this, pName);
        if (isInstalled) {
          holder.startBtn.setText("启动");
        } else {
          holder.startBtn.setText("安装");
        }
        holder.startBtn.setOnClickListener(
            new OnClickListener() {

              @Override
              public void onClick(View v) {
                if (isInstalled) {
                  Util.startAppByPname(UserDownloadActivity.this, pName);
                } else {
                  Util.installApk(UserDownloadActivity.this, filePath);
                }
              }
            });
      }
      convertView.setOnLongClickListener(
          new OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {
              new AlertDialog.Builder(UserDownloadActivity.this)
                  .setTitle("提示")
                  .setMessage("确认删除" + "?") // 二次提示
                  .setNegativeButton(
                      "取消",
                      new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {}
                      })
                  .setPositiveButton(
                      "确定",
                      new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                          // 删除本地文件
                          File df = new File(gi.getFilePath());
                          System.out.println(
                              "file~~~~~~" + df.getPath() + " file exist() " + df.exists());
                          if (df.exists()) {
                            // 如果文件存在
                            df.delete();
                          }
                          downloadList.remove(gi);
                          notifyDataSetChanged();
                          // 删除数据库中的内容
                          new FinalDBChen(getParent(), DBNAME)
                              .deleteItem(
                                  TABNAME_DOWNLOADTASK,
                                  "game_name=?",
                                  new String[] {gi.getGame_name()});
                          // 发送一个删除文件的广播.让主页的下载按钮变为可下载
                          Intent i = new Intent();
                          i.putExtra(DOWNLOAD_TYPE, DOWNLOAD_STATE_DELETE);
                          i.setAction(DOWNLOAD_TYPE);
                          getMyApplication().setDownloadSuccess(gi);
                          sendBroadcast(i);
                        }
                      })
                  .show();
              return false;
            }
          });
      return convertView;
    }