views:

161

answers:

2

Hi. I need to made an activity (without layout) that on start check if a service is running. if it is true it starts Activity2, if it false it starts Activity1. I tried with this code:

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

but when I check, in the onCreate method, if serviceConnect!=null I receive sometime a NullPointerExcption.

I tried also to insert the operation in the method onCreate in an Async Task:

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());};


    }
}

But I have the same problem. Someone knows how I can fix this? Thanks

+1  A: 

bindService() is asynchronous. You have to wait until onServiceConnected() is called on your ServiceConnection before you will know if the service is ready for use.

CommonsWare
Thanks for your reply. I know that, but I don't know how to wait
zerocool87
@zerocool87: You move all your code after your call to `bindService()` from `onCreate()` and into `onServiceConnected()`. You do not look at `serviceConnected` until `onServiceConnected()`. Frankly, I think you need a different approach to the problem, probably merging Activity1 and Activity2 into a single activity, so you don't have to make this decision.
CommonsWare
+1  A: 

@CommonsWare I can't merge Activity1 and Activity2 because are structured in different way. I solved starting another thread that check if the bind has done. Thanks for your reply

zerocool87