为什么Activity阻止直到服务在android中完成?

时间:2013-02-20 07:10:09

标签: android android-activity android-service

我从我的主要活动开始提供服务。活动似乎不会出现,直到服务完成。为什么会这样?这个问题的解决方案是什么?

3 个答案:

答案 0 :(得分:1)

请详细阅读本文档。 http://developer.android.com/guide/components/processes-and-threads.html

服务在UI线程上运行。你需要在不同的线程中开始耗费部分服务(可能是asynctask)。

答案 1 :(得分:0)

这是因为,控件已转到服务,在完成之前不会返回。因此,只有在您的服务完成后,才会呈现您的活动。 More Info

解决方法是使用AsyncTask

答案 2 :(得分:0)

一个简单的示例服务,可以帮助您明确如何使用它。

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

            }
        }
    }

}