java - Android 弹出式 Activity,位于任何其他应用之上

标签 java android android-layout

我想要创建的是一个弹出式应用程序。

我在后台有一个服务 - 有东西到达队列,我想要一个 Activity 开始通知用户 - 与 SMSPopup 应用程序的功能非常相似。

所以我有这样的代码,其中有东西到达队列并调用我的 Activity 。

但是由于某种原因,该 Activity 始终显示在最初启动的 Activity 之上,而不是仅出现在 Android 设备的主桌面上。

举个例子:

我有应用程序运行时显示的主要 Activity

我有检查队列的服务

我有一个弹出 Activity 。

当我启动主要 Activity 时,它会启动服务 - 我现在可以关闭它。

然后,我在队列中有一些东西,它会创建弹出 Activity ,该 Activity 启动主 Activity ,并在其顶部弹出窗口:S 我如何停止它并让它按照我想要的方式运行...

弹出类是:




public class SMSPopup extends Activity implements OnClickListener{

 public static String msg;


 @Override
 public void onCreate(Bundle bundle){
  super.onCreate(bundle);
 // Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
  this.setContentView(R.layout.popup);
  TextView tv = (TextView)findViewById(R.id.txtLbl);
  Intent intent = getIntent();
  if (intent != null){
      Bundle bb = intent.getExtras();
      if (bb != null){
          msg = bb.getString("com.andy.tabletsms.message");
      }
  }

  if(msg == null){
   msg = "LOLOLOL";
  }
  tv.setText(msg);

  Button b = (Button)findViewById(R.id.closeBtn);
  b.setOnClickListener(this);
 }


 @Override
 public void onClick(View v) {
  this.finish();
 }
}




我从广播接收器调用该 Activity ,该接收器每 30 秒左右检查一次队列:


  if(main.msgs.size()>0){

      Intent testActivityIntent = new Intent(context.getApplicationContext(), com.andy.tabletsms.work.SMSPopup.class);
    testActivityIntent.putExtra("com.andy.tabletsms.message", main.msgs.get(0));
    testActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(testActivityIntent);

      }

布局在这里:http://pastebin.com/F25u6wdM

最佳答案

这违背了 Android 建议的设计实践。请参阅http://developer.android.com/guide/topics/ui/notifiers/notifications.html

A background Service should never launch an Activity on its own in order to receive user interaction.

您可以在 Toast 和/或通知中显示消息。从通知中,您可以开始一个新的 Intent 。

关于java - Android 弹出式 Activity,位于任何其他应用之上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4621145/

相关文章:

java - 钻石运算符(operator)的默认行为是什么

android - 改造,请求get带非必须参数

android - Lollipop 进度条着色

android - Android选中下拉项修改颜色

java - .JAR 文件无法在其他计算机上打开

java - Java中如何通过ResultSet获取行数

java - RESTful - 在 Queryparam 中获取 null

javascript - Web Speech API 无法在 Chrome for Android 中正确加载语音

android - 应用程序被杀死和恢复后重叠的隐藏 fragment

android - 如何以编程方式将内容添加到自定义 XML 布局?