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