【问题标题】:How to use Progress dialog in Application class in android studio如何在android studio的应用程序类中使用进度对话框
【发布时间】:2017-11-26 17:16:19
【问题描述】:

我有一个名为 myApp 的类,它扩展了 android studio 中的 Application 类

我的意思是每个应用程序启动时间都需要从本地调用数据

假设我在本地有 100 条数据..每次应用启动时都必须调用这些数据

为此创建了一个名为 myApp extends Application 的类

这样我就可以调用每个应用程序的打开时间

所以我在这里调用数据检索方法。

但用户需要知道诸如数据之类的东西正在加载背景..

所以我想在这个应用程序类中使用进度对话框

      public class myApp extends Application {

      Context mContext;


      //so i used like this 

     //1 st way

  mContext = getApplicationContext();

  ProgressDialog dialog=ProgressDialog.show(mContext, "Data", 
  "Synching");
  dialog.show();

  //2 nd way

  mContext = this;

  ProgressDialog dialog=ProgressDialog.show(mContext, "Data", 
  "Synching");
  dialog.show();

  //3 rd way

 ProgressDialog dialog=ProgressDialog.show(getApplicationContext(), 
 "Data", 
  "Synching");
  dialog.show();

 //4 th way

 ProgressDialog dialog=ProgressDialog.show((myApp.this, "Data", 
  "Synching");
  dialog.show();

  }

它给出了这个例外。

     android.view.WindowManager$BadTokenException: Unable to add 
     window -- token null is not for an application 

我尝试了这些方法,但我失败了

所有案例都失败了......

那么我如何在完成数据后获得时间时显示进度对话框如何关闭进度对话框

提前致谢

【问题讨论】:

  • 你应该考虑重写你的答案,老实说我不知道​​你在说什么,除了几句话。据我了解,您需要检索显示进度的数据:通常的过程是创建自定义 AsyncTask,在 doInBackground 中加载数据并为 onProgressUpdate 发布进度更新。然后,一旦完成,您可以使用 onPostExecute 停止对话。
  • 请尝试增强问题的格式以及句子的表述。
  • 只是我说我想在应用程序类中使用进度对话框我尝试了我在问题中提到的那些方法你现在可以给我答案吗

标签: android database class progress extends


【解决方案1】:

您可以为所有子活动创建一个 SuperActivity 来代替这种做法

class SuperActivity extends AppCompatActivity{
// here you can put you data which is common for all activities
    public void showProgressBar(){
        // write your code here to show progress bar
    }
    public void hideProgressBar(){
        // write your code here to dismiss progress bar
    }
}
class Activity1 extends SuperActivity{
}
class Activity2 extends SuperActivity{
}
class Activity3 extends SuperActivity{
}

【讨论】:

  • 看看这是我的课 public class MyApplication extends Application { //这里我应该使用进度条...因为我在上面解释了我的场景}
  • 不要为你的数据使用应用程序类,使用superActivity,无论你使用的方法是错误的,你不能从应用程序类的ui元素更新
【解决方案2】:

是的,可以在Application类中使用ProgressDialog

   public class ApplicationClass extends Application {
    ProgressDialog pd;
    @Override
    public void onCreate() {
        super.onCreate();
        pd = new ProgressDialog(this.getApplicationContext(),R.style.MyTheme);
        pd.setCancelable(false);
        pd.setProgressStyle(android.R.style.Widget_ProgressBar_Small);
        pd.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        pd.show();
    }
}

别忘了添加权限

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

【讨论】:

  • 不适用于 Android 6.0.1 Unable to add window android.view.ViewRootImpl$W@3ba724e -- permission denied for this window type。是的,我添加了权限。
猜你喜欢
  • 2013-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多