首先我希望我能尊重这个论坛的网络礼节,因为这是我第一次使用它。
这是我的问题:我有一个具有以下结构的 Android 应用程序:
- Main Activity(根据 SMS 内容显示一些用户指示和一些警报对话框(参见 2))
- SMS 广播接收器 在启动时运行(它工作正常,在启动时运行,读取 SMS 并以正确的方式解析它们)。
我希望能够在接收者收到正确的 SMS 时激活 Activity 并显示 AlertDialog。 如果我先显示 Activity 然后离开它(因此如果 Activity 进入挂起状态),一切都会正常工作,但如果我从不打开 Activity,我只能显示 Activity 本身而不能激活 AlertDialog。
这里有两小段代码:
接收方(接收特定短信时执行的代码):
//Show/start activity
Intent sec=new Intent(context, SecureMessagesActivity.class);
sec.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
context.startActivity(sec);
// Activate AlertDialog
Intent i = new Intent(context.getString(R.string.intentReceivedSuccessSms)).putExtra("some_msg", "I will be sent!");
context.sendBroadcast(i);
Log.v(TAG, "Sent Intent intentReceivedSuccessSms");
Activity (在 android list 中定义为 singleTop):
public void onCreate(Bundle savedInstanceState)
{
Log.v(TAG, "Performing onCreate");
super.onCreate(savedInstanceState);
setTheme( android.R.style.Theme_Light);
setContentView(R.layout.main);
//--------------------------------------------------------------
// Manage subscription to intent
//--------------------------------------------------------------
IntentFilter intentFilter = new IntentFilter(
getString(R.string.intentReceivedSuccessSms));
mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(final Context context, Intent intent) {
AlertDialog.Builder ad = new AlertDialog.Builder(context);
ad.setTitle(getString(R.string.youFoundIt));
ad.setMessage(getString(R.string.stopTheMusic));
ad.setIcon(R.drawable.tick);
ad.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Log.v(TAG, "Received button pressure to stop ringtone");
};
}
);
ad.show();
}
};
//registering our receiver
this.registerReceiver(mReceiver, intentFilter);
//--------------------------------------------------------------
// End of manage subscription to intent
//--------------------------------------------------------------
}
我认为我的问题与以下事实有关:当应用程序尚未激活时,只有广播接收器处于 Activity 状态,因此我从未运行 Activity 的 OnCreate 方法。通过这种方式,我认为首先启动 Activity (参见注释“显示/启动 Activity ”)并在之后立即发送由 OnCreate 方法接受的广播消息(因此仍未注册,因为当接收者发送 Intents 时,OnCreate 仍然没有启动)。 但我不明白如何修复它,我认为这是一个架构问题。 请注意,如果我启动电话并发送 2 条消息,则会发生这种情况:
- 电话开启
- 第一条短信
- Activity 出现,没有 AlertDialog
- 尽量减少 Activity (或保持全屏,没关系)
- Activity 出现,带有 AlertDialog
任何帮助将不胜感激
祝你新年快乐。
最佳答案
这行不通,因为您已经自己解释过:
If the activity is not yet running, you can't start it and immediately send a broadcast Intent. At the moment you send the broadcast Intent, your activity hasn't yet started so your listener isn't registered.
您应该直接将消息信息添加到您用来启动 Activity 的 Intent 中,如下所示:
//Show/start activity
Intent sec=new Intent(context, SecureMessagesActivity.class);
sec.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
sec.putExtra("some_msg", "I will be sent!");
context.startActivity(sec);
Log.v(TAG, "Sent Intent intentReceivedSuccessSms");
然后,在 onCreate()
和 onNewIntent()
中,您可以获得额外的内容并使用它来显示您的对话框。您的 Activity 中不需要 BroadcastReceiver
。
关于android - 从运行启动的广播接收器使用 alertDialog 启动 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14111906/