我想要创建的是一个弹出式应用程序。
我在后台有服务 - 有些东西到达队列,我想要一个活动开始通知用户 - 非常类似于SMSPopup应用程序的功能。
所以我有一些代码,其中某些东西到达队列并且它调用我的活动。
但由于某种原因,活动始终显示在最初启动的活动之上,而不是仅显示在Android设备的主桌面上。
举个例子:
我有运行应用程序时显示的主要活动
我有检查队列的服务
我有一个弹出活动。
当我开始主要活动时,它启动服务 - 我现在可以关闭它。
然后我在队列中有了一些东西,它创建了弹出活动,它通过弹出窗口启动主要活动: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();
}
}
我从广播接收器调用该活动,该接收器每隔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);
}
答案 0 :(得分:3)
这违反了Android建议的设计惯例。见http://developer.android.com/guide/topics/ui/notifiers/notifications.html
后台服务从不自行启动活动以接收用户互动。
您可以在Toast和/或通知中显示消息。从通知中,您可以开始新的意图。