在Android广播接收器中将活动置于最前面 [英] Bring an activity to front in android Broadcast Reciever

查看:108
本文介绍了在Android广播接收器中将活动置于最前面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我接到Android手机的来电时,我正在尝试启动一项活动。为此,在响铃状态下,我启动了一个活动,并设置了一些标志将此活动置于最前面。代码如下:

I'm trying to launch an activity , when i got an incoming call for my android mobile. For that, in ringing state, i started an activity and sets some flags for bring this activity to front. Here is the Code:

       Intent intent2open = new Intent(ctx,  Main.class);
    intent2open.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK |       Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP); 
    intent2open.setAction("android.intent.action.VIEW");
    intent2open.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent2open.setAction(Intent.ACTION_MAIN); 
    intent2open.addCategory(Intent.CATEGORY_LAUNCHER); 
        intent2open.putExtra(name, value);
    startActivity(intent2open);

通过使用以上代码,我没有得到所需的输出,某些情况下,当我接受时,应用程序将启动或拒绝了我的电话。
在其他情况下,它将在状态响起时启动。我无法解决此问题。.我该如何把我的活动放在前面。

By using above code, i didnt get my required output, some cases app will be launched when i accepted or rejected my call. In some other cases,it will be launched when the state in ringing. I am unable to addressed this issue.. How can i bring my activity to front.

在此先感谢
Lokesh。

Thanks in advance, Lokesh.

推荐答案

android:launchMode = singleTop 设置为清单中的活动,并将标志设置为:

Set android:launchMode="singleTop" to your activity in manifest and set your flags as:

intent2open.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT
    |Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
    |Intent.FLAG_ACTIVITY_NEW_TASK);

* 至少在我的项目中对我来说效果很好

更新

广播的意图过滤器清单在这种情况下也可能对您有帮助:

Increase of priority in manifest for your broadcast's intent-filter may also help you in this case:

<!-- Higher numbers have a higher priority -->
<intent-filter android:priority="100">

这篇关于在Android广播接收器中将活动置于最前面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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