- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
在服务准备就绪时显示 ProgressDialog 时,我遇到了严重的问题...该服务需要一些时间才能准备好,因为它有点重,所以我想在它启动时显示 ProgressDialog。
问题是它在下一个 Activity 开始之前显示 ProgressDialog...我真的不知道它是什么...
package org.pfc;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class ConnectActivity extends Activity {
// FIELDS------------------------------------------------------------------
protected LocalService mSmeppService;
private ProgressDialog progressDialog;
private Thread tt;
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
// Gets the object to interact with the service
mSmeppService = ((LocalService.LocalBinder) service).getService();
}
public void onServiceDisconnected(ComponentName className) {
// This is called when the connection with the service has been
// unexpectedly disconnected -- that is, its process crashed.
// Because it is running in our same process, we should never
// see this happen.
mSmeppService = null;
}
};
// For getting confirmation from the service
private BroadcastReceiver serviceReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "receiver onReceive...");
if (progressDialog.isShowing())
progressDialog.dismiss();
// Change activity
Intent groupsActivityIntent = new Intent(ConnectActivity.this,
GroupsActivity.class);
startActivity(groupsActivityIntent);
}
};
// METHODS ----------------------------------------------------------------
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (LocalService.isRunning) {
// TODO start ListActivity
Log.i(TAG, "Starting GroupsScreen");
Intent i = new Intent(ConnectActivity.this, GroupsActivity.class);
startActivity(i);
} else {
setContentView(R.layout.connect_screen);
// Add listener to the button
Button buttonConnect = (Button) findViewById(R.id.button_connect);
buttonConnect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
processThread();
}
});
}
}
// PRIVATE METHODS --------------------------------------------------------
private void processThread() {
progressDialog = ProgressDialog.show(ConnectActivity.this, "",
"Loading. Please wait...", true, false);
tt = new Thread() {
public void run() {
// Register broadcastReceiver to know when the service finished
// its creation
ConnectActivity.this.registerReceiver(serviceReceiver,
new IntentFilter(Intent.ACTION_VIEW));
// Starts the service
startService(new Intent(ConnectActivity.this,
LocalService.class));
Log.i(TAG, "Receiver registered...");
}
};
tt.start();
}
}
服务在 onStart 方法结束时执行:
// Send broadcast so activities take it
Intent i = new Intent(Intent.ACTION_VIEW);
sendOrderedBroadcast(i, null);
因此 onReceive 方法运行,我们进入下一个 Activity
最佳答案
问题是您没有在 UI 线程中运行 ProgressDialog。
添加一个处理程序来处理您的 UI 线程中的消息。
private static final int UPDATE_STARTED = 0;
private static final int UPDATE_FINISHED = 1;
private Handler handler = new Handler(){
@Override public void handleMessage(Message msg) {
switch (msg.what) {
case UPDATE_STARTED:
progressDialog = ProgressDialog.show(ConnectActivity.this, "",
"Loading. Please wait...", true, false);
break;
case UPDATE_FINISHED:
if(progressDialog.isShowing()){
progressDialog.dismiss();
}
break;
}
}
};
private void processThread() {
Message m = new Message();
m.what = UPDATE_STARTED;
handler.sendMessage(m);
//Your working code
m = new Message();
m.what = UPDATE_FINISHED;
handler.sendMessage(m);
}
祝你好运!
关于android - 在启动服务时显示 ProgressDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4327709/
当用户点击登录按钮时,我想在我的应用程序中使用 ProgressDialog。我遇到的问题是未找到 ProgressDialog - AS 中的红色文本。我错过了什么图书馆吗? 渐变 compile
我使用进度条创建了自定义进度对话框,但自定义进度对话框中的进度消息没有更新 最佳答案 您可以使用 ProgressBar实现相同的行为进度对话框 CustomProgressBar progressB
给出下面的代码。我想将标题“请等待调用被激活”转换为印地语(我在 Assets 文件夹中提供的字体).. public ShakeEventsListener(Context context, OnS
当我的设备是 4.4.4 时,我的 ProgressDialog 工作正常,我最近没有使用这个应用程序,同时设备升级到 Lillipop。我不确定这是否对这个问题造成了任何影响,只是提一下。 问题 1
ProgressDialog 在我的代码中添加 progressDialog.dismiss() 方法时无法显示 我还尝试添加Thread.sleep(),以便它的执行将 hibernate 一段时间
我只是想创建一个旋转的进度对话框。没有文字,没有边框,没有背景。只是一个旋转的通知,轻轻地位于内容顶部的屏幕中心。 我在创建这个自定义对话框时看到了两个不同的 Stack Overflow 问题。 他
我的意思是,ProgressDialog 静态方法 show() 的返回值与该类实例的非静态方法 show 之间有什么区别? 有什么理由选择这种策略 ProgressDialog pd = new P
我在进程运行时使用 ProgressDialog 时遇到问题。我已经尝试了所有可能的不正确方法,并查看了许多网站,这些网站提供了我正在尝试做的事情的示例,但是,我仍然遇到了线程在 ProgressDi
我开发了一个 Flutter 应用程序,我使用了 ProgressDialog小部件 ( progress_dialog: ^1.2.0 )。首先,我展示了 ProgressDialog小部件和一些代
在应用程序中创建ProgressDialog时,出现BadTokenException错误。我尝试阅读有关该问题的几篇文章,但我仍然无法真正弄清这是什么原因。单击按钮后,我调用对话框函数。 p
以下是我的代码: public void onClick(View view) { dialog=new ProgressDialog(view.getContext());
我有一个进度对话框,可以工作,但不符合我的外部需求: 我想把它做成这样: 我尝试过的: 1 - 主题化(仅使用样式和样式+主题方法),如此处所述 结果很糟糕(我在对话框周围有一个相同的蓝色边框颜色和标
我正在尝试从互联网获取数据,解析它,然后将数据显示给用户。该操作通常需要 3 秒左右,我希望旋转进度对话框显示一条消息,说明我正在加载数据。问题是 ProgressDialog 仅在加载即将完成时才显
我制作了一个食谱应用程序,我一直在解决一些问题,因为我无法运行该应用程序,但似乎每次修复错误时都会出现另一个错误,我的项目无法解决方法解雇()我不知道该怎么办,我已经根据 Youtube channe
我想从外部类中显示当前 Activity 的 ProgressDialog,而不是将变量传递到外部类或像其他已回答的问题一样使用静态变量。是否可以?这是一些简单的测试代码,可能有助于说明我想要做什么:
在“onCreate”中,我正在从网络下载数据。下载数据的持续时间为 10 秒。我不想在下载数据时使用 ProgressDialog。这是我的代码,但 ProgressDialog 没有出现: pub
我的应用程序正在从网络服务器加载一些数据。这次我想要一个进度对话框以避免应用程序用户出现黑屏。 Button ok = (Button) findViewById(R.id.ok);
我在我的 android 应用程序中创建了一个 ProgressDialog。但我遇到的问题是在它实际完成工作时停止旋转轮子。这是我的代码。我怎样才能让它在我进行其他工作时不断旋转轮子? button
我能做到: ((Activity) mContext).setProgressBarIndeterminateVisibility(true); 但不是这个: ((Activity) mConte
我想从服务内的线程增加一个进度对话框,我真的很难做到,这是我的代码,请帮助我。 我尝试了很多不同的方法,包括 asyncTask(我遇到了上下文问题)并尝试使用静态函数但它无法正常工作,我是 andr
我是一名优秀的程序员,十分优秀!