我想要创建的是一个弹出式应用程序。
我在后台有一个服务 - 有东西到达队列,我想要一个 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);
}
最佳答案
这违背了 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/