简体   繁体   English

在Android广播接收器中将活动置于最前面

[英]Bring an activity to front in android Broadcast Reciever

I'm trying to launch an activity , when i got an incoming call for my android mobile. 当我接到Android手机的来电时,我正在尝试启动一项活动。 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. 我无法解决此问题。

Thanks in advance, Lokesh. 在此先感谢Lokesh。

Set android:launchMode="singleTop" to your activity in manifest and set your flags as: android:launchMode="singleTop"设置为清单中的活动,并将标志设置为:

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

* at least it works well for me in my project * 至少在我的项目中对我来说效果很好


Update 更新资料

Increase of priority in manifest for your broadcast's intent-filter may also help you in this case: 在这种情况下,增加广播的Intent过滤器清单的优先级也可能对您有帮助:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM