如何在不显示根活动的情况下显示来自通知的活动?

时间:2016-02-25 07:43:42

标签: android

在我的应用程序中,我收到通知,其中有一个“关闭”按钮,用于执行操作。

此操作以确认对话框(是/否对话框)开始。

为了做到这一点,我有一个使用透明主题的Activity,当活动开始时,我会显示对话框。

工作正常,单击“通知操作”按钮时会创建“活动”,然后显示“对话框”,但是......如果之前已显示应用程序的MainActivity,并且用户已通过“主页”按钮关闭它而不是在Back按钮上,Application Main Activity显示在Dialog后面。

有可能解决这个问题吗?

透明活动定义:

<activity android:name=".AlertActionReceiverActivity" android:excludeFromRecents="true" android:launchMode="singleTask" android:theme="@style/AppThemeTransparent" />

创建通知操作按钮

            device_notification.addAction(R.drawable.ic_action_dismiss, getString(R.string.dismiss_alert).toUpperCase(), PendingIntent.getActivity(getBaseContext(), getNotificationPendingIntentIdentifier(-1, 2), new Intent(getBaseContext(), AlertActionReceiverActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK).setAction(AlertActionReceiverActivity.ACTION_DISMISS_ALERT).putExtra(AlertActionReceiverActivity.EXTRA_ALARM, active_alarm), PendingIntent.FLAG_UPDATE_CURRENT));

This occurs when the user clicks Home button in the Main Activity and a few minutes ago it clicks the Notification Action Button

This occurs when the user clicks Back button in the Main Activity and a few minutes ago it clicks the Notification Action Button or when the Main Activity hasn't been displayed previously, and is the desired behavior

1 个答案:

答案 0 :(得分:0)

就像你说完成onPause()方法上的活动并不好,所以对我来说,我使用首选项来知道应用程序主要活动是否被打开以及发生以下情况

if(prefs.getBoolean(Constants.APPLICATION_ON,false))
    {
        Log.e("Activity is in background","background");
        try
        {
            Intent bringToForeground=new Intent(context, Main_Activity.class);
            bringToForeground.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_SINGLE_TOP);
            startActivity(bringToForeground);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    else
    {
        try
        {
            Intent activate=new Intent(context,Main_Activity.class);
            activate.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(activate);


        }
        catch (Exception e)
        {
            e.printStackTrace();
            Log.e("Activity is not in background",e.getMessage());
        }
    }