方法,把一个Android应用程序在后台前台 [英] Ways to bring an Android app in background to foreground

查看:172
本文介绍了方法,把一个Android应用程序在后台前台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是该方案:

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

AndroidManifest.xml defines a single Activity with android:launchMode="singleTask". (This means there should be a single activity in the stack throughout the entire application lifecycle, right ?)

Activity.onCreate(),广播接收器编程方式创建并监听进来的短信。接收器即使在 Activity.onPause仍然有效() 设计

During Activity.onCreate(), a broadcast receiver is programmatically created and listens for incomming SMS. The receiver remains active even after Activity.onPause() by design.

当用户与应用程序完成,他presses设备主页按钮,它调用 Activity.onPause()和应用程序消失。该设备显示那么Android主屏幕。

When the user is done with the application, he presses the device Home button which calls Activity.onPause() and the application disappears. The device shows then the Android home screen.

在收到短信,广播接收器接收到的短信,并试图通过展现出来的活动:

Upon receiving SMS, the broadcast receivers receives SMS and tries to show up the Activity via:

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);

然而,该活动是不是出现了用户。

  • 一)为什么
  • B)可能是什么方法,把一个Activty到前台?

推荐答案

MyMainActivity 定义(AndroidManifest.xml中):

In MyMainActivity definition (AndroidManifest.xml):

<intent-filter>
 <action android:name="intent.my.action" />
 <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

以编程方式将应用前景:

Programmatically bringing application to foreground:

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);

注: context.startActivity(它)是行不通的时候,上下文对象是一样的活动一要弹出。

Note: context.startActivity(it) would NOT work when the context object is same as the activity one wants to bring up.

这篇关于方法,把一个Android应用程序在后台前台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆