android - 从后台应用程序启动 Activity

标签 android android-intent android-activity launching-application

我的应用程序在后台运行,我希望在运行以下代码时该应用程序显示在 Android 手机的顶部(已启动)。 (我知道代码肯定会运行)

这看起来很简单,但我在这个网站上花了几个小时,每个人似乎都在建议这样的事情:

Intent intent = new Intent(myActivity.this, myActivity.class);
startActivity(intent);

但是,它不会将应用程序带到前端并启动它。

我通过从通知启动的 PendingIntent 让它工作。我通过下面的代码完成的。但我希望应用程序在用户不点击通知的情况下自行启动。

Intent intent = new Intent(myActivity.this, myActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, REQUEST_CODE, intent, 0);
notification.setLatestEventInfo(this, "title", "msg", contentIntent);

我也试过:

Intent intent = new Intent("android.intent.action.MAIN");
startActivity(intent);

并标记 Intent :

intent.setFlags(Intent.FLAG_FROM_BACKGROUND);
intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

但似乎没有做任何事情,任何帮助表示赞赏。

最佳答案

您应该能够像这样调用您自己的应用程序:

Intent intent = new Intent("android.intent.category.LAUNCHER");
intent.setClassName("com.your.package", "com.your.package.MainActivity");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

编辑:忘记添加 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

关于android - 从后台应用程序启动 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10744923/

相关文章:

Android 和 Eclipse - 发生了什么事?

android - Google Play 权限声明表单无法访问

Android:BroadcastReceiver Intent 检测拍摄的相机照片?

android - 将上下文传递给语音搜索

android - SIP Android 应用程序,如何使用服务和 Activity

android - 如何在成功支付后关闭在线支付 Activity 并将其重定向回android中的调用 Activity

android - 按文件名显示图像

java - 没有 Wifi 无法定位

android - Ksoap2 返回 anytype{} 而不是 null

Android任务管理器源码