2011-08-11 47 views
0

我在服务中工作,在设备启动时,我的服务随应用程序一起启动。 现在,如果服务发现一些受限制的应用程序启动,它会为我的应用程序提供背景,显示回家,杀死应用程序,并重新开始活动到前台。但它在启动启动时创建了多个实例。如何避免从服务开始的多个活动实例

我想,如果它存在,每次启动之前启动的活动中,我使用下面的代码:

//activity to background  
    Intent startMain = new Intent(Intent.ACTION_MAIN); 
    startMain.addCategory(Intent.CATEGORY_HOME); 
    startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    startActivity(startMain); 

    //killing restricted app  
    Appmgr.killBackgroundProcesses(RunningP.processName); 

    //again start activity  
    Intent intent = new Intent(); 
    intent.setAction(Intent.ACTION_MAIN); 
    intent.addCategory(Intent.CATEGORY_LAUNCHER); 
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    ComponentName cn = new ComponentName(this, TaxiPlexer.class); 
    intent.setComponent(cn); 
    startActivity(intent); 

我已经使用FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY和其他人,但他们给我强制关闭错误。上面的代码仅创建多个实例在引导

+0

做出意向对象的静态和最终 – Pratik

回答

0

添加此体现:

<activity android:name="yourActivity" launchMode = "singleTask" ></activity>