显示ProgressDialog而服务正在启动 [英] Showing ProgressDialog while a Service is being started
问题描述
我有严重的问题......该服务需要一定的时间来准备,因为它是一个有点沉重,所以我想同时显示ProgressDialog它的开始。
的事情是,它显示了ProgressDialog右边的下一个活动开始前...我真的不觉得它是什么......
包org.pfc;
进口android.app.Activity;
进口android.app.ProgressDialog;
进口android.content.BroadcastReceiver;
进口android.content.ComponentName;
进口android.content.Context;
进口android.content.Intent;
进口android.content.IntentFilter;
进口android.content.ServiceConnection;
进口android.os.Bundle;
进口android.os.IBinder;
进口android.util.Log;
进口android.view.Menu;
进口android.view.MenuItem;
进口android.view.View;
进口android.widget.Button;
公共类ConnectActivity延伸活动{
//场----------------------------------------------- -------------------
保护本地服务mSmeppService;
私人ProgressDialog progressDialog;
私人螺纹TT;
私人ServiceConnection mConnection =新ServiceConnection(){
公共无效onServiceConnected(组件名的className,服务的IBinder){
//获取与服务进行交互的对象
mSmeppService =((LocalService.LocalBinder)服务).getService();
}
公共无效onServiceDisconnected(组件名的className){
//这被称为当与服务连接已
//意外断开 - 也就是说,它的进程崩溃。
//因为这是在我们的同一进程中运行,我们应该永远
//看到这种情况发生。
mSmeppService = NULL;
}
};
//对于从服务得到确认
私人的BroadcastReceiver serviceReceiver =新的BroadcastReceiver(){
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
Log.i(TAG,接收器的onReceive ......);
如果(progressDialog.isShowing())
progressDialog.dismiss();
//更改活动
意图groupsActivityIntent =新的意图(ConnectActivity.this,
GroupsActivity.class);
startActivity(groupsActivityIntent);
}
};
//方法----------------------------------------------- -----------------
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
如果(LocalService.isRunning){
// TODO开始ListActivity
Log.i(TAG启动GroupsScreen);
意图I =新的意图(ConnectActivity.this,GroupsActivity.class);
startActivity(ⅰ);
} 其他 {
的setContentView(R.layout.connect_screen);
//添加侦听器的按钮
按钮buttonConnect =(按钮)findViewById(R.id.button_connect);
buttonConnect.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
processThread();
}
});
}
}
//私有方法---------------------------------------------- ----------
私人无效processThread(){
progressDialog = ProgressDialog.show(ConnectActivity.this,,
Loading请稍候......,真,假);
TT =新的Thread(){
公共无效的run(){
//注册的BroadcastReceiver知道当服务完成
//它的创作
ConnectActivity.this.registerReceiver(serviceReceiver,
新的IntentFilter(Intent.ACTION_VIEW));
//启动服务
startService(新意图(ConnectActivity.this,
LocalService.class));
Log.i(TAG,接收器注册的......);
}
};
tt.start();
}
}
该服务的OnStart方法本月底执行
//发送广播这样的活动把它
意图I =新的意图(Intent.ACTION_VIEW);
sendOrderedBroadcast(我,NULL);
所以的onReceive方法运行,我们去到下一个活动
现在的问题是,你是不是在UI线程中运行ProgressDialog。
添加一个处理程序,将处理在UI线程的消息。
私有静态最终诠释UPDATE_STARTED = 0;
私有静态最终诠释UPDATE_FINISHED = 1;
私人处理程序处理程序=新的处理程序(){
@覆盖公共无效的handleMessage(信息MSG){
开关(msg.what){
案例UPDATE_STARTED:
progressDialog = ProgressDialog.show(ConnectActivity.this,,
Loading请稍候......,真,假);
打破;
案例UPDATE_FINISHED:
如果(progressDialog.isShowing()){
progressDialog.dismiss();
}
打破;
}
}
};
私人无效processThread(){
消息m =新的Message();
m.what = UPDATE_STARTED;
handler.sendMessage(米);
//你的工作code
M =新的Message();
m.what = UPDATE_FINISHED;
handler.sendMessage(米);
}
祝你好运!
I'm having serious problems when showing a ProgressDialog while a service is getting ready... The service takes time to get ready as it's a bit heavy, so I want to show the ProgressDialog meanwhile it's started.
The thing is that it shows the ProgressDialog right before the next activity starts... I really don't find what it is...
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();
}
}
The service executes by the end of the onStart method this:
// Send broadcast so activities take it
Intent i = new Intent(Intent.ACTION_VIEW);
sendOrderedBroadcast(i, null);
So the onReceive method runs and we go to the next activity
The problem is that you are not running ProgressDialog in a UI thread.
Add a handler that will handle messages in your UI thread.
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);
}
good luck!
这篇关于显示ProgressDialog而服务正在启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!