0

私はカスタムシナリオを実行したいBroadcastReceiverを使用してGridViewアダプタでプログレスバーを更新するには?

私はカスタムを使用していますTouchyGridView すべてうまくいきました。

今、私は新しい機能を追加したいと思います。

APKファイルをバックグラウンドでダウンロードします。だからダウンロードを開始するときに、新しいグレードのアイテムにグリッドビューとプログレスバーに新しいアイコンを表示したい。

*私はサーバーからダウンロードする方法を知っています。

そしてProgressBarが、私はこれを行うことができますどのようにprogressbar.setProgress(progress);

enter image description here

を使用して更新する必要がありますか?

ブロードキャストがMainActivityにあり、Viewがアダプタにあるので、成功しませんでした。BroadCastReceiverを使用してアダプタクラスのビューを更新するにはどうすればよいですか?

grid_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:padding="5dp"> 

    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 

     <ImageView 
      android:id="@+id/grid_app_image" 
      android:layout_width="@dimen/icon_width" 
      android:layout_height="@dimen/icon_height" 
      android:layout_gravity="center_horizontal" 
      android:src="@drawable/ic_launcher" /> 

     <ProgressBar 
      android:id="@+id/progressBar" 
      style="@style/Widget.AppCompat.ProgressBar.Horizontal" 
      android:layout_width="@dimen/icon_width" 
      android:layout_height="@dimen/icon_height" 
      android:progressDrawable="@drawable/circular" /> 
    </RelativeLayout> 

    <TextView 
     android:id="@+id/grid_app_label" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_marginTop="5px" 
     android:gravity="center_horizontal" 
     android:text="app" 
     android:textColor="@color/content" 
     android:textSize="18sp" /> 

</LinearLayout> 

HomeScreenAppGridAdapter.java

public class HomeScreenAppGridAdapter extends BaseAdapter implements 
     OnClickListener { 

    private Context mContext; 
    private List<KioskAppDetail> mAppList; 

    private PackageManager mPackageManager; 
    private String tag = "GridAdapter"; 

    // Constructor to initialize values 
    public HomeScreenAppGridAdapter(Context context, 
            List<KioskAppDetail> appList) { 
     mContext = context; 
     mAppList = appList; 

     mPackageManager = mContext.getPackageManager(); 
    } 

    @Override 
    public int getCount() { 
     // Number of times getView method call depends upon gridValues.length 
     return mAppList.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return null; 
    } 

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

    // Number of times getView method call depends upon gridValues.length 
    public View getView(final int position, View convertView, ViewGroup parent) { 

     LayoutInflater inflater = (LayoutInflater) mContext 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View gridView; 

//   if (convertView == null) { 

     gridView = inflater.inflate(R.layout.grid_item, null); 
     final TextView textView = (TextView) gridView 
       .findViewById(R.id.grid_app_label); 
     final ImageView imageView = (ImageView) gridView 
       .findViewById(R.id.grid_app_image); 
     ProgressBar progressBar = (ProgressBar) gridView 
       .findViewById(R.id.progressBar); 
     if (!mAppList.get(position).getAppLable().toString() 
       .equalsIgnoreCase("Peripheral Settings")) { 

      /*int color = Color.parseColor(Util.getStringDefaultPrefValue(
        mContext, Util.TEXT_COLOR, Util.TEXT_COLOR_DEFAULT));*/ 

      progressBar.setVisibility(View.GONE); 
      String iconSizePer = Util.getStringDefaultPrefValue(mContext, Util.ICON_SIZE_DP, Util.DEF_ICON_SIZE_DP); 
      if(iconSizePer != null) { 
       if(!iconSizePer.trim().isEmpty()) { 
        int originalIconSize = 60; // This is 100% Icon Size 
        int iconSize = (Integer.parseInt(iconSizePer) * originalIconSize)/100; 
        int pixel = dpToPx(iconSize); 
        imageView.getLayoutParams().height = pixel; 
        imageView.getLayoutParams().width = pixel; 
        imageView.requestLayout(); 
       } 
      } 

      String textSizePer = Util.getStringDefaultPrefValue(mContext, Util.TEXT_SIZE_SP, Util.DEF_TEXT_SIZE_SP); 
      if(textSizePer != null) { 
       if(!textSizePer.trim().isEmpty()) { 
        int originalTextSize = 18; // This is 100% text Size 
        int textSize = (Integer.parseInt(textSizePer) * originalTextSize)/100; 
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize); 
       } else { 
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15); 
       } 
      } 

      int color; 
      /*= Color.parseColor(Util.getStringDefaultPrefValue(
        mContext, Util.TEXT_COLOR, Util.TEXT_COLOR_DEFAULT)); 
*/ 
      String selectedColor = Util.getStringDefaultPrefValue(mContext, Util.TEXT_COLOR, Util.TEXT_COLOR_DEFAULT); 
      if(selectedColor.equalsIgnoreCase("")) { 
       color = Color.parseColor(mContext.getString(R.string.black)); 
      } else if (selectedColor.equalsIgnoreCase("Black")) { 
       color = Color.parseColor(mContext.getString(R.string.black)); 
      } else if (selectedColor.equalsIgnoreCase("Blue")) { 
       color = Color.parseColor(mContext.getString(R.string.blue)); 
      } else if (selectedColor.equalsIgnoreCase("Cyan")) { 
       color = Color.parseColor(mContext.getString(R.string.cyan)); 
      } else if (selectedColor.equalsIgnoreCase("Dark Grey")) { 
       color = Color.parseColor(mContext.getString(R.string.dark_grey)); 
      } else if (selectedColor.equalsIgnoreCase("Grey")) { 
       color = Color.parseColor(mContext.getString(R.string.grey)); 
      } else if (selectedColor.equalsIgnoreCase("Light Grey")) { 
       color = Color.parseColor(mContext.getString(R.string.light_grey_text)); 
      } else if (selectedColor.equalsIgnoreCase("Green")) { 
       color = Color.parseColor(mContext.getString(R.string.green)); 
      } else if (selectedColor.equalsIgnoreCase("Magenta")) { 
       color = Color.parseColor(mContext.getString(R.string.magenta)); 
      } else if (selectedColor.equalsIgnoreCase("Red")) { 
       color = Color.parseColor(mContext.getString(R.string.red)); 
      } else if (selectedColor.equalsIgnoreCase("White")) { 
       color = Color.parseColor(mContext.getString(R.string.white)); 
      } else if (selectedColor.equalsIgnoreCase("Yellow")) { 
       color = Color.parseColor(mContext.getString(R.string.yellow)); 
      } else { 
       color = Color.parseColor(mContext.getString(R.string.black)); 
      } 

      textView.setTextColor(color); 
      textView.setText(mAppList.get(position).getAppLable()); 
      // Log.d(tag, mAppList.get(position).getAppLable() + "App lable" 
      // + mAppList.size() + "App list size" + position); 

      // if (TextUtils.isEmpty(mAppList.get(position).getAppIcon())) { 
      // no custom icon, use app's icon 
      imageView.setImageDrawable(mAppList.get(position).getDefaultAppIcon()); 
      // } 

      final OnClickListener onClickListener = new OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        Log.i(tag, "app icon clicked [" 
          + mAppList.get(position).getPackageName()); 

        Intent i = mPackageManager 
          .getLaunchIntentForPackage(mAppList.get(position) 
            .getPackageName()); 
        mContext.startActivity(i); 

       } 
      }; 
      imageView.setOnClickListener(onClickListener); 

     } else { 

      Log.d(tag, "In If Home Screen Adapter"); 

      int color; 
      /*= Color.parseColor(Util.getStringDefaultPrefValue(
        mContext, Util.TEXT_COLOR, Util.TEXT_COLOR_DEFAULT)); 
*/ 
      String selectedColor = Util.getStringDefaultPrefValue(mContext, Util.TEXT_COLOR, Util.TEXT_COLOR_DEFAULT); 
      if(selectedColor.equalsIgnoreCase("")) { 
       color = Color.parseColor(mContext.getString(R.string.black)); 
      } else if (selectedColor.equalsIgnoreCase("Black")) { 
       color = Color.parseColor(mContext.getString(R.string.black)); 
      } else if (selectedColor.equalsIgnoreCase("Blue")) { 
       color = Color.parseColor(mContext.getString(R.string.blue)); 
      } else if (selectedColor.equalsIgnoreCase("Cyan")) { 
       color = Color.parseColor(mContext.getString(R.string.cyan)); 
      } else if (selectedColor.equalsIgnoreCase("Dark Grey")) { 
       color = Color.parseColor(mContext.getString(R.string.dark_grey)); 
      } else if (selectedColor.equalsIgnoreCase("Grey")) { 
       color = Color.parseColor(mContext.getString(R.string.grey)); 
      } else if (selectedColor.equalsIgnoreCase("Light Grey")) { 
       color = Color.parseColor(mContext.getString(R.string.light_grey_text)); 
      } else if (selectedColor.equalsIgnoreCase("Green")) { 
       color = Color.parseColor(mContext.getString(R.string.green)); 
      } else if (selectedColor.equalsIgnoreCase("Magenta")) { 
       color = Color.parseColor(mContext.getString(R.string.magenta)); 
      } else if (selectedColor.equalsIgnoreCase("Red")) { 
       color = Color.parseColor(mContext.getString(R.string.red)); 
      } else if (selectedColor.equalsIgnoreCase("White")) { 
       color = Color.parseColor(mContext.getString(R.string.white)); 
      } else if (selectedColor.equalsIgnoreCase("Yellow")) { 
       color = Color.parseColor(mContext.getString(R.string.yellow)); 
      } else { 
       color = Color.parseColor(mContext.getString(R.string.black)); 
      } 
      textView.setTextColor(color); 
      textView.setText(mAppList.get(position).getAppLable()); 

      // if 
      // (TextUtils.isEmpty(mAppList.get(position).getAppIcon())) 
      // { 
      // no custom icon, use app's icon 
      imageView.setImageResource(R.drawable.icon_settings); 
      // } 

      final OnClickListener onClickListenerr = new OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        Log.i(tag, "app icon clicked [" 
          + mAppList.get(position).getPackageName()); 
        // Intent i = 
        // mPackageManager.getLaunchIntentForPackage(mAppList 
        // .get(position).getPackageName()); 
        // mContext.startActivity(i); 
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
         boolean status_for_useage_access_on_app_click = Util 
           .systemSettingsOnUsage(mContext); 
         if (status_for_useage_access_on_app_click == true) { 
          // Intent i = mPackageManager 
          // .getLaunchIntentForPackage(mAppList 
          // .get(position) 
          // .getPackageName()); 
          // mContext.startActivity(i); 
          Intent intent = new Intent(
            mContext, 
            net.intricare.allowedsettings.PeripheralSettingsActivity.class); 
          mContext.startActivity(intent); 
         } else { 
          Util.launchUsageAccessDialog(mContext); 
         } 
        } else { 
         Intent intent = new Intent(
           mContext, 
           net.intricare.allowedsettings.PeripheralSettingsActivity.class); 
         mContext.startActivity(intent); 
        } 
       } 
      }; 
      // --------------------------------------------- // 
      imageView.setOnClickListener(onClickListenerr); 

     } 

     // } else { 
     // gridView = (View) convertView; 
     // } 
     /** 
     * duplicate code 
     */ 
     // --------------------------------------------- // 

     /*if (!mAppList.get(position).getAppLable().toString() 
       .equalsIgnoreCase("Peripheral Settings")) { 
      int color = Color.parseColor(Util.getStringDefaultPrefValue(
        mContext, "text_color", "black")); 

      textView.setTextColor(color); 
      textView.setText(mAppList.get(position).getAppLable()); 
      // Log.d(tag, mAppList.get(position).getAppLable() + "App lable" 
      // + mAppList.size() + "App list size duplicate code" 
      // + position); 

      // if (TextUtils.isEmpty(mAppList.get(position).getAppIcon())) { 
      // no custom icon, use app's icon 
      imageView.setImageDrawable(mAppList.get(position) 
        .getDefaultAppIcon()); 
      // } 

      final OnClickListener onClickListener = new OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        Log.i(tag, "app icon clicked [" 
          + mAppList.get(position).getPackageName()); 
        try { 
         Intent i = mPackageManager 
           .getLaunchIntentForPackage(mAppList.get(position) 
             .getPackageName()); 
         mContext.startActivity(i); 
        } catch (NullPointerException ex) { 
         ex.printStackTrace(); 
        } 

       } 
      }; 
      imageView.setOnClickListener(onClickListener); 
     } else { 

      TextView textVieww = (TextView) gridView 
        .findViewById(R.id.grid_app_label); 

      int color = Color.parseColor(Util.getStringDefaultPrefValue(
        mContext, "text_color", "black")); 
      textVieww.setTextColor(color); 
      textVieww.setText(mAppList.get(position).getAppLable()); 

      ImageView imageVieww = (ImageView) gridView 
        .findViewById(R.id.grid_app_image); 

      imageVieww.setImageResource(R.drawable.icon_settings); 

      final OnClickListener onClickListenerr = new OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        Log.i(tag, "app icon clicked [" 
          + mAppList.get(position).getPackageName()); 

        if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) { 
         boolean status_for_useage_access_on_app_click = Util 
           .systemSettingsOnUsage(mContext); 
         if (status_for_useage_access_on_app_click == true) { 

          Intent intent = new Intent(
            mContext, 
            net.intricare.allowedsettings.PeripheralSettingsActivity.class); 
          mContext.startActivity(intent); 
         } else { 
          Util.launchUsageAccessDialog(mContext); 
         } 
        } else { 
         Intent intent = new Intent(
           mContext, 
           net.intricare.allowedsettings.PeripheralSettingsActivity.class); 
         mContext.startActivity(intent); 
        } 
       } 
      }; 
      // --------------------------------------------- // 
      imageVieww.setOnClickListener(onClickListenerr); 

     }*/ 

     return gridView; 
    } 

    public static int pxToDp(int px) { 
     return (int) (px/Resources.getSystem().getDisplayMetrics().density); 
    } 

    public static int dpToPx(int dp) { 
     return (int) (dp * Resources.getSystem().getDisplayMetrics().density); 
    } 

    @Override 
    public void onClick(View v) { 
     Log.i(tag, "touch event"); 
    } 
} 

HomeActivity.java

TouchyGridView mGridView; 
mGridView = (TouchyGridView) findViewById(R.id.apps_grid); 


LocalBroadcastManager bManager = LocalBroadcastManager.getInstance(this); 
     IntentFilter intentFilter = new IntentFilter(); 
     intentFilter.addAction(MESSAGE_PROGRESS); 
     bManager.registerReceiver(broadcastReceiver, intentFilter); 


mGridView.setAdapter(new HomeScreenAppGridAdapter(HomeActivity.this, mKioskAppsList)); 
     mGridView.setOnNoItemClickListener(this); 
     mGridView.setOnItemClickListener(this); 



     private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 

      if(intent.getAction().equals(MESSAGE_PROGRESS)){ 
       //initHomeScreenUI(true); 

       /*if(download.getProgress() == 100){ 
        mProgressText.setText("File Download Complete"); 

       } else { 
        mProgressText.setText(String.format("Downloaded (%d/%d) MB",download.getCurrentFileSize(),download.getTotalFileSize())); 
       }*/ 
      } 
     } 
    }; 


    // When Download Start, I am sending broadcast using below code 

    Intent intent = new Intent(HomeActivity.MESSAGE_PROGRESS); 
     intent.putExtra("download",download); 
     LocalBroadcastManager.getInstance(DownloadService.this).sendBroadcast(intent); 
+0

apkがダウンロードされているうちに、 –

+0

の背景にgifイメージを配置しようとしましたが、progressbar.setProgress(progress);を使用して進行状況ダイアログを更新します。 –

+0

アダプター用に別のクラスを作成しました。 –

答えて

1

以下の手順で問題を解決してください。

最初に、アダプタクラスとアクティビティコードが同じクラスにあることを確認してください。

  1. ダウンロードを開始すると、アップデートUI用のブロードキャストレシーバーが1つ送信され、それに応じてフラグが管理されます。
  2. プログレスバーの可視性をtrueに設定します。
  3. 進捗バーを更新します。
  4. ダウンロードが完了したらプログレスバーを非表示にします。
+0

これは可能です。私はあなたにチェックの後にお知らせします。 –

関連する問題