如何将Android应用程序在后台运行到前台

Dav*_*tti 9 android android-activity

这是场景:

AndroidManifest.xml定义了一个Activity android:launchMode="singleTask".(这意味着整个应用程序生命周期中堆栈中应该有一个活动,对吧?)

在此期间Activity.onCreate(),以编程方式创建广播接收器并监听接收SMS.即使经过Activity.onPause() 设计,接收器仍保持活动状态.

当用户完成应用程序时,他按下设备主页按钮,该按钮调用Activity.onPause()并且应用程序消失.然后设备显示Android主屏幕.

收到短信后,广播接收机接收短信并尝试通过以下方式显示活动:

Intent it = new Intent(context, Akami.class);
it.setAction(Intent.ACTION_MAIN);
it.addCategory(Intent.CATEGORY_LAUNCHER);
it.setComponent(new ComponentName(context.getPackageName(), "MyActivity"));
it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(it);
Run Code Online (Sandbox Code Playgroud)

但是,活动不会显示给用户.

  • a)为什么?
  • b)将Activty带到前台的可能方法有哪些?

Dav*_*tti 18

MyMainActivity定义(AndroidManifest.xml)中:

<intent-filter>
 <action android:name="intent.my.action" />
 <category android:name="android.intent.category.DEFAULT" />
</intent-filter>
Run Code Online (Sandbox Code Playgroud)

以编程方式将应用程序引入前台

Intent it = new Intent("intent.my.action");
it.setComponent(new ComponentName(context.getPackageName(), MyMainActivity.class.getName()));
it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.getApplicationContext().startActivity(it);
Run Code Online (Sandbox Code Playgroud)

注意:context.startActivity(it)context对象与想要提出的活动相同时,它将不起作用.