如何从服务上带来活动

时间:2014-05-02 15:27:16

标签: android android-activity

我有两个活动1)FrmHome我有senTtoback 2)ScreenLock我想从Service打开。当服务调用屏幕解锁时,活动将在后台打开。当我点击应用程序时,我可以看到Screenlock Activity。我希望屏幕锁定活动应该在不点击应用程序的情况下出现在上面

这是服务 和处理程序

public class FrmHome extends ActionBarActivity {

BroadcastReceiver mybroadcast = new BroadcastReceiver() {

//When Event is published, onReceive method is called
@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
        Log.i("[BroadcastReceiver]", "MyReceiver");

        if(intent.getAction().equals(Intent.ACTION_USER_PRESENT)){
            Log.i("[BroadcastReceiver]", "Screen ON");

            Intent intent1 = new Intent(context, ScreenLock.class);
            intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_SINGLE_TOP);
            startActivity(intent1);



        }
        else if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
            Log.i("[BroadcastReceiver]", "Screen OFF");
        }

  }
   };
  @Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_frm_home);
    // registerReceiver(mybroadcast, new IntentFilter(Intent.ACTION_SCREEN_ON));
     registerReceiver(mybroadcast, new IntentFilter(Intent.ACTION_USER_PRESENT));
}
} 

我的新活动在后台打开。当我点击应用程序时,我可以看到新的Screenlock Activity。但它并没有自动出现

1 个答案:

答案 0 :(得分:0)

您可以再次致电startActivity,将活动置于最前面。在正常情况下,这会启动活动的新实例,这可能是不可取的。您可以通过在launchMode中设置活动的AndroidManifest.xml来解决此问题(解释here)。例如,使用singleTop应解决此问题。这样,下次您的服务调用startActivity时,前一个实例就会显示在最前面。