Android 为什么在安卓系统中活动会一直阻塞到服务完成?
我从我的主要活动开始一项服务。直到服务完成,活动才会出现。为什么会这样?这个问题的解决方案是什么 请详细阅读此文档Android 为什么在安卓系统中活动会一直阻塞到服务完成?,android,android-activity,android-service,Android,Android Activity,Android Service,我从我的主要活动开始一项服务。直到服务完成,活动才会出现。为什么会这样?这个问题的解决方案是什么 请详细阅读此文档 服务在UI线程上运行。您需要在不同的线程中启动耗时的服务部分(可能是asynctask)。这是因为,控件已转到服务,该服务在完成之前不会返回。因此,只有在服务完成后,才会呈现活动 解决这个问题的方法是使用。一个简单的示例服务,让您清楚如何使用它 import android.app.Activity; import android.content.Intent; import an
服务在UI线程上运行。您需要在不同的线程中启动耗时的服务部分(可能是asynctask)。这是因为,控件已转到服务,该服务在完成之前不会返回。因此,只有在服务完成后,才会呈现活动
解决这个问题的方法是使用。一个简单的示例服务,让您清楚如何使用它
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;
public class ServiceTimeToast extends Activity {
/** Called when the activity is first created. */
public static MyHandler myHandler;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = new Intent(ServiceTimeToast.this,ServiceManagement.class);
startService(intent);
myHandler = new MyHandler();
}
class MyHandler extends Handler
{
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
if (msg.what == 111) {
//int count = Integer.parseInt(msg.obj);
int count = (Integer) msg.obj;
Log.i("MyLog", "Service Running Started"+count);
Toast.makeText(ServiceTimeToast.this, "Service Running properly::"+count, Toast.LENGTH_SHORT).show();
}
}
}
}
ServiceManagement.java//服务类
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
public class ServiceManagement extends Service {
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Log.v("MyLog", "OnCreate of the Service management called");
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.v("MyLog", "OnDestroy of the Service management called");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Log.v("MyLog", "onStartCommand of the Service management called");
ThreadTime t = new ThreadTime();
Thread thread = new Thread(t);
thread.start();
return super.onStartCommand(intent, flags, startId);
}
}
class ThreadTime implements Runnable
{
int MAX_SEC = 5;
//MyHandler handler = new MyHandler();
@Override
public void run() {
// TODO Auto-generated method stub
Log.i("MyLog", "Thread Started!!");
int count = 0;
for(int i = 0;i<MAX_SEC;i++)
{
try {
Log.i("MyLog", "Inside the For loop!!!!!!");
Message message = ServiceTimeToast.myHandler.obtainMessage(111);
message.obj = ++count;
ServiceTimeToast.myHandler.sendMessage(message);
} catch (Exception e) {
// TODO Auto-generated catch block
}
}
}
}
导入android.app.Service;
导入android.content.Intent;
导入android.os.IBinder;
导入android.os.Message;
导入android.util.Log;
公共类服务管理扩展了服务{
@凌驾
公共IBinder onBind(意向){
//TODO自动生成的方法存根
返回null;
}
@凌驾
public void onCreate(){
//TODO自动生成的方法存根
super.onCreate();
Log.v(“MyLog”,“OnCreate of the Service management called”);
}
@凌驾
公共空间{
//TODO自动生成的方法存根
super.ondestory();
Log.v(“MyLog”,“调用的服务管理的OnDestroy”);
}
@凌驾
公共int onStartCommand(Intent Intent、int标志、int startId){
//TODO自动生成的方法存根
v(“MyLog”,“调用服务管理的onstart命令”);
ThreadTime t=新线程时间();
螺纹=新螺纹(t);
thread.start();
返回super.onStartCommand(intent、flags、startId);
}
}
类ThreadTime实现可运行
{
int MAX_SEC=5;
//MyHandler=新的MyHandler();
@凌驾
公开募捐{
//TODO自动生成的方法存根
Log.i(“MyLog”,“线程已启动!!”;
整数计数=0;
对于(int i=0;i