我的问题是: 我有活动A,它启动服务S.A启动另一个应用程序B,它安装在系统中。然后S开始活动B,在这个时刻,B开始,连同A.技术上,A不应该开始,但它确实如此。我对此很困惑。我真的希望有人能帮助我。我花了好几个小时。 这是代码:
public class ActivityA extends Activity{
Intent intent;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.test_b);
intent = new Intent(this, TestService.class);
//start an app in the system
AppManager.getAppManager(this).launch(10);
startService(intent);
}
@Override
public void onDestroy(){
super.onDestroy();
stopService(intent);
}
}
public class TestService extends Service{
@Override
public void onCreate(){
super.onCreate();
IntentFilter onfilter = new IntentFilter(Intent.ACTION_SCREEN_ON);
registerReceiver(screenon, onfilter);
}
@Override
public void onDestroy(){
super.onDestroy();
unregisterReceiver(screenon);
}
BroadcastReceiver screenon = new BroadcastReceiver() {
public static final String TAG = "screenon";
public static final String Screen = "android.intent.action.SCREEN_ON";
@Override
public void onReceive(Context context, Intent intent) {
if (!intent.getAction().equals(Screen))
return;
//start TestLockActivity when screen on
Intent it = new Intent(TestService.this, ActivityB.class);
it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(it);
return;
}
};
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
public class ActivityB extends Activity{
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.test_lock);
}
}
AppManager中的launch()方法:
public void launch(String activityName)
{
ResolveInfo info = getResolve(activityName);
Intent intent = new Intent();
intent.setComponent(new ComponentName(info.activityInfo.applicationInfo.packageName,
activityName));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
public void launch(int idx)
{
String activityName = getActivityName(idx);
launch(activityName);
}
答案 0 :(得分:0)
这是因为活动B已经附加到正在运行的任务(根植于活动A的任务)。它也在FLAG_ACTIVITY_NEW_TASK的文档中指定:
使用此标志时,如果任务已在您正在运行的活动中运行 开始,然后新的活动将不会开始;相反,当前任务将 只需将其带到最后一个状态的屏幕前面即可 FLAG_ACTIVITY_MULTIPLE_TASK用于禁用此行为的标志。