Android Activity检查服务以启动另一个活动

时间:2010-09-10 14:33:33

标签: android service android-activity

我需要在开始检查服务是否正在运行时进行一项活动(没有布局)。如果是,则启动Activity2,如果为false,则启动Activity1。 我试过这段代码:

public class FirstActivity extends Activity{

    private final Intent serviceIntent = new Intent(ServiceConnected.class
                    .getName());

    private ServiceConnected serviceConnected;

    private final ServiceConnection serviceConnection = new ServiceConnection() {

            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {


                    serviceConnected = ServiceConnected.Stub.asInterface(service);
                    Log.e("Connessione al Servizio", "onServiceConnected");
                    Toast.makeText(FirstActivity.this, "Connessione con il Service Effettuata", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                    Log.e("Disconnessione dal servizio", "onServiceDisconnected");
                    Toast.makeText(FirstActivity.this, "Scollegamento effettuato", Toast.LENGTH_SHORT).show();
            }

    };


    @Override
    public void onCreate(Bundle savedInstanceState){
            setContentView(R.layout.firstactivity);
            super.onCreate(savedInstanceState);
            bindRemoteService();
            Log.e("Dopo Bind onCreate","FirstActivity");
            if(serviceConnected!=null)
                    try {
                            boolean temp=serviceConnected.getConnected();
                            Log.e("ServiceConnected",Boolean.toString(temp));
                            if(temp==true){
                                    Log.e("Salto alla seconda","FirstActivity");
                                    step2();
                            }
                            else{
                                    Log.e("Salto alla prima","FirstActivity");
                                    step1();

                            }

                    } catch (RemoteException e) {
                            // TODO Auto-generated catch block
                            Log.e("Errore Step","FirstActivity");
                    }

    }




    public void bindRemoteService() {
            if (serviceConnected == null) {
                    if( bindService(serviceIntent, serviceConnection,
                                    Context.BIND_AUTO_CREATE) ){

                            Log.e("Bind Effettuato", "bindRemoteService");
                            Toast.makeText(this, "Service Binded", Toast.LENGTH_SHORT).show();

                    }
            }
    }


    private void step2(){

            Intent intent;
            intent=new Intent(getApplicationContext(), Activity2.class);
            startActivity(intent);

    }

    private void step1(){

            Intent intent;
            intent=new Intent(getApplicationContext(), Activity1.class);
            startActivity(intent);

    }


enter code here

但是当我检查时,在onCreate方法中,如果serviceConnect!= null,我有时会收到NullPointerExcption。

我还尝试在异步任务中的方法onCreate中插入操作:

private class BackgroundWorker extends AsyncTask<Void,Integer, Void> {




    @SuppressWarnings("unchecked")
    @Override
    protected void onPreExecute () {

        Log.e("BackgroundWorker","onPreExecute");
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground ( Void... params ) {

        Log.e("Dentro Background Worker", "DOIN");


        bindRemoteService();
        publishProgress(1);
        //publishProgress(2);




        return null;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void onProgressUpdate ( Integer... values ) {


        try{    

            do{

                try {

                    if (serviceConnected!=null&&checked==true){

                        boolean temp=serviceConnected.getConnected();

                        if(temp==true){
                            Log.e("Salto alla seconda","FirstActivity");
                            Log.e("ServiceConnected",Boolean.toString(temp));
                            step2();
                        }

                        else if(temp==false){
                            Log.e("Salto alla prima","FirstActivity");
                            Log.e("ServiceConnected",Boolean.toString(temp));
                            step1();

                        }
                    }

                } catch (RemoteException e) {
                    // TODO Auto-generated catch block
                    Log.e("Errore Step",e.toString());
                }

            }while(checked==false);


        }catch(Exception e){Log.e("Errore do",e.toString());};


    }
}

但我有同样的问题。有人知道如何解决这个问题吗? 谢谢

2 个答案:

答案 0 :(得分:1)

bindService()是异步的。您必须等到onServiceConnected()调用ServiceConnection,然后才能知道该服务是否可以使用。

答案 1 :(得分:1)

@CommonsWare我无法合并Activity1和Activity2,因为它们的结构方式不同。我解决了另一个检查绑定是否完成的线程。谢谢你的回复