【问题标题】:Android message notification dialog on top of other activities其他活动之上的 Android 消息通知对话框
【发布时间】:2012-01-06 05:12:51
【问题描述】:

如何在其他活动之上获取 Android 消息通知对话框?

我正在寻找此问题的解决方案,但仍未找到解决方案。现在,我正在开发一个社交网络应用程序,当用户从另一个人那里收到一些消息时,我需要在其中显示一个通知消息对话框,并实现我使用广播接收器并且它工作正常。

问题是如何在另一个应用程序之上显示通知对话框。

【问题讨论】:

  • 你真的不应该尝试构建这样的东西。如果有任何应用敢在我在手机上做其他事情时打扰我,我会真的生气。 Android 有一个内置的通知系统,它足够突出以被识别,但允许用户决定何时做出反应。使用它。
  • 但我的要求是,任何建议如何做到这一点。
  • 我为你添加了代码,检查一下。

标签: android dialog notifications message


【解决方案1】:

是的,这是可能的。 Main.xml 布局有一个编辑文本和按钮。 Messagebox 布局有一个按钮。在这里,您可以将消息布局更改为您想要的任何内容。

文件 MyScheduledReceiver.java:

public class MyScheduledReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

        Intent scheduledIntent = new Intent(context, MessageBox.class);
        scheduledIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(scheduledIntent);
    }
}

主要活动:

public class AndroidMessageBoxActivity extends Activity implements OnClickListener
{
    private EditText time;
    private Button btn;
    private AlarmManager alarm;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        time = (EditText) findViewById(R.id.editText1);
        btn = (Button) findViewById(R.id.button1);
        alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

        btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        int x = Integer.parseInt(time.getText().toString());

        Intent intent = new Intent(this, MyScheduledReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(
                this.getApplicationContext(), 234324243, intent, 0);

        alarm.set(AlarmManager.RTC_WAKEUP,
                  System.currentTimeMillis() + (x * 1000),
                  pendingIntent);
        Toast.makeText(this,
                       "Alarm set in " + x + " seconds",
                       Toast.LENGTH_LONG).show();
    }
}

消息框:

public class MessageBox extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.messagebox);

        Button btn = (Button) findViewById(R.id.Ok);
        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                finish();
            }
        });
    }
}

并在 Android 清单 XML 文件中添加这两行:

<receiver android:name="MyScheduledReceiver"></receiver>

<activity android:name="MessageBox" android:theme="@style/Theme.Transparent"></activity>

文件样式.xml:

<resources>
    <style name="Theme.Transparent" parent="android:Theme">
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:backgroundDimEnabled">false</item>
    </style>
</resources>

【讨论】:

  • 我也以同样的方式实现了它,但是,考虑到这种情况,我登录了我的应用程序,我在我的应用程序主页,然后我按下主页按钮,然后启动其他一些应用程序,例如画廊.查看画廊时,如果我的通知活动已启动,那么我将被带到我的应用主页,但在此之上我有我需要的消息通知,我只需要显示我的通知活动,关闭它应该允许用户查看已经在使用的画廊。我想你明白我说的......
  • +1 的答案和感谢支持终于通过将“taskAffinity”属性添加到我在清单文件中的对话活动中得到了解决方案。
  • 这个例子非常有用。我想补充一点,如果您希望您的“对话框”显示在当前正在运行的任何其他应用程序之上,您可以在清单中设置 launchMode="singleTask",或者可以在代码中完成如下:Intent scheduledIntent = new Intent(context, AlertAlarmActivity.class); scheduledIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); scheduledIntent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK); context.startActivity(scheduledIntent);Of当然,请注意更改启动模式以及使用其他应用程序的后果!
【解决方案2】:

Status bar notifications 上的文档所述:

后台服务应该从不自行启动活动以接收用户交互。

因此,我强烈建议您不要这样做,但您应该改用状态栏通知。

【讨论】:

    【解决方案3】:

    正如其他人在回复中所说,不鼓励从后台启动 Activity 以引起用户的注意。但是,有些情况需要这样做; Android 本身的示例包括时钟闹钟、来电和低电量警报。

    在 Gingerbread 中,fullScreenIntent 字段已添加到通知对象;这是发布通知的标准且方便的方式,该通知还启动活动以真正引起用户的注意。从 Gingerbread 开始,我上面列出的平台组件(警报等)都使用这种技术来显示它们的警报。这是按照您的要求做的推荐方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多