1

首先我希望我会尊重这个论坛的网络礼仪,因为这是我第一次使用它。

这是我的问题:我有一个具有以下结构的 Android 应用程序:

  1. 主要活动(根据 SMS 内容显示一些用户指示和一些警报对话框(参见 2))
  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 manifest 中定义为 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 的 OnCreate 方法。通过这种方式,我认为首先启动活动(参见评论“显示/启动活动”)并立即发送 OnCreate 方法接受的广播消息(因此仍未注册,因为接收方发送 Intent 时 OnCreate 仍未启动)。但我不明白如何解决它,我认为这是一个架构问题。请注意,如果我启动手机并发送 2 条消息,则会发生这种情况:

  1. 电话开启
  2. 第一条短信
  3. 活动显示,没有警报对话框
  4. 最小化活动(或让它全屏,没关系)
  5. Activity 出现,带有 AlertDialog

任何帮助将不胜感激

祝你新年快乐。

4

2 回答 2

1

由于您已经解释过自己的原因,这不起作用:

如果 Activity 尚未运行,则无法启动它并立即发送广播 Intent。在您发送广播 Intent 的那一刻,您的活动尚未开始,因此您的侦听器尚未注册。

您应该直接将消息信息添加到用于启动活动的 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()你可以得到额外的并用它来显示你的对话框。你不需要BroadcastReceiver在你的活动中。

于 2013-01-02T10:02:03.200 回答
0
Intent sec=new Intent(context, Dialog.class);
            sec.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            context.startActivity(sec);

您需要在活动中使用它!

public class Dialog extends Activity 
{

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);


    this.getWindow()
    .setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);

    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    builder
        .setTitle("Califica la llamada")
        .setMessage("Esto ayudará a brindarte un mejor servicio de telefonía!")

        .setCancelable(true)
        .setPositiveButton("Buena", new DialogInterface.OnClickListener() 
        {
            public void onClick(DialogInterface dialog, int id) 
            {
                dialog.cancel();
                finish();
            }
        })
        .setNegativeButton("Mala", new DialogInterface.OnClickListener() 
        {
            public void onClick(DialogInterface dialog, int id) 
            {
                dialog.cancel();
                finish();
            }
        });


    AlertDialog alert = builder.create();
    alert.show();



}
}
于 2013-10-17T14:21:27.747 回答