Bring an activity to front in android Broadcast Reciever

青春壹個敷衍的年華 提交于 2019-12-10 00:05:05

问题


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.

Thanks in advance, Lokesh.


回答1:


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

*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:

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


来源:https://stackoverflow.com/questions/9873560/bring-an-activity-to-front-in-android-broadcast-reciever

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!