简体   繁体   English

创建弹出窗口,而不是来自BroadcastReceiver的通知

[英]create pop up instead of notification from BroadcastReceiver

i need to display pop up message in the center of screen , after reading in few forums i found out that it can be achieved by starting a new transparent activity . 在几个论坛上阅读后,我需要在屏幕中央显示弹出消息,我发现可以通过启动一个新的透明活动来实现。

this is my code : 这是我的代码:

@Override
public void onReceive(Context context, Intent intent) {

    Intent i = new Intent();
    i.setClassName(context,
            "com.ui.activity.NotificationActivity");
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(i);

}

this work's but : 这项工作是:

if application is in background than the application will come to the foreground with the pop up on it (if the application is not on background this work's great ). 如果应用程序在后台,则该应用程序将弹出并显示在前台 (如果应用程序不在后台,则效果很好)。

If you don't want your app to be brought to the foreground when this popup is shown, you need to ensure that NotificationActivity has a different task affinity than the rest of your application. 如果您不希望在显示此弹出窗口时将应用程序带到前台,则需要确保NotificationActivity与其他应用程序具有不同的任务关联。

In the manifest, add this to the <activity> definition for NotificationActivity : 在清单中,将其添加到NotificationActivity<activity>定义中:

android:taskAffinity=""

Without this, the default task affinity for all Activities is the same, so that when Android looks for a task to launch NotificationActivity into, and your app is already running, Android launches NotificationActivity into that task and brings it to the foreground. 如果没有此选项,则所有Activity的默认任务关联性都是相同的,因此,当Android查找要启动NotificationActivity的任务并且您的应用已在运行时,Android会将NotificationActivity启动至该任务并将其置于前台。

If you change the task affinity, Android won't look for an existing task to launch NotificationActivity into, and will just create a new task. 如果您更改任务关联,Android将不会寻找现有的任务来将NotificationActivity启动到其中,而只会创建一个新任务。

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

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