Android:如果服务运行,则启动活动A,否则启动活动B
我在这方面遇到了麻烦,无法让它工作。这是空的主类别启动器活动的代码,如果服务未运行或用户未通过身份验证,则必须显示启动屏幕,否则启动对话活动Android:如果服务运行,则启动活动A,否则启动活动B,android,android-activity,Android,Android Activity,我在这方面遇到了麻烦,无法让它工作。这是空的主类别启动器活动的代码,如果服务未运行或用户未通过身份验证,则必须显示启动屏幕,否则启动对话活动 import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
public class EntryPoint extends Activity {
private IAppManager imService;
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
imService = ((IMService.IMBinder) service).getService();
// this is not starting activity :(
// Start converstion activity if service running and user ok
if (imService.isUserAuthenticated() == true) {
try {
Intent i = new Intent(EntryPoint.this, Conversations.class);
startActivity(i);
finish();
} catch (Exception e) {
e.printStackTrace();
}
}
}
// this is not working
// start login activity if service disconnected
public void onServiceDisconnected(ComponentName className) {
imService = null;
try {
Intent intent = new Intent(getBaseContext(), Splash.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
finish();
} catch (Exception e) {
e.printStackTrace();
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Start and bind the imService
startService(new Intent(EntryPoint.this, IMService.class));
}
@Override
protected void onPause() {
super.onPause();
try {
unbindService(mConnection);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onResume() {
super.onResume();
try {
bindService(new Intent(EntryPoint.this, IMService.class),
mConnection, Context.BIND_AUTO_CREATE);
} catch (Exception e) {
e.printStackTrace();
}
}
}
当我运行应用程序时,既不运行对话
,也不运行启动
活动,但我看到的是空活动:(也没有错误,只运行空入口点
活动,实际上应该启动其他活动之一
有人知道我做错了什么吗?很可能您的服务没有连接,因此无法与您的活动交互
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
public class EntryPoint extends Activity {
private IAppManager imService;
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
imService = ((IMService.IMBinder) service).getService();
// this is not starting activity :(
// Start converstion activity if service running and user ok
if (imService.isUserAuthenticated() == true) {
try {
Intent i = new Intent(EntryPoint.this, Conversations.class);
startActivity(i);
finish();
} catch (Exception e) {
e.printStackTrace();
}
}
}
// this is not working
// start login activity if service disconnected
public void onServiceDisconnected(ComponentName className) {
imService = null;
try {
Intent intent = new Intent(getBaseContext(), Splash.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
finish();
} catch (Exception e) {
e.printStackTrace();
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Start and bind the imService
startService(new Intent(EntryPoint.this, IMService.class));
}
@Override
protected void onPause() {
super.onPause();
try {
unbindService(mConnection);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onResume() {
super.onResume();
try {
bindService(new Intent(EntryPoint.this, IMService.class),
mConnection, Context.BIND_AUTO_CREATE);
} catch (Exception e) {
e.printStackTrace();
}
}
}
单独创建一个intent,然后在startService&bindService分配它,而不是每次为startService&bindService创建一个新的intent实例
另外,为什么要在onResume()上绑定服务?请查看此链接,了解您为什么应该尽可能避免这种情况-
您好,谢谢您的回答。我试过了,但它仍然显示空白活动,尽管没有错误。splash not conversation活动也没有启动。我不知道如何等待服务连接,然后执行以下操作:(