gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 22:31:59 26 4
gpt4 key购买 nike

我需要创建一个 Activity (没有布局),在开始时检查服务是否正在运行。如果为真,则启动 Activity2,如果为假,则启动 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());};


}
}

但是我也遇到了同样的问题。有人知道我该如何解决这个问题?谢谢

最佳答案

bindService() 是异步的。您必须等到 onServiceConnected() 在您的 ServiceConnection 上被调用,然后您才能知道该服务是否已准备好使用。

关于Android Activity 检查服务启动另一个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3685450/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com