3

当android中的警报响起时,我想创建一个AlertDialog。另外,我想创建一个通知,具体取决于用户在对话框上的单选按钮中单击的选项。当我尝试使用context或时出现问题getApplicationContext()

这是我的代码:

public void onReceive(final Context context, Intent intent)
{
    final CharSequence[] items = {" I'm taking the dose now! "," Remind again in ten minutes. "," Ignore for now. "};
    String doseName = intent.getStringExtra("doseName");
    Toast.makeText(context, "Take medicine: " + doseName, Toast.LENGTH_LONG).show();

    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle("It's time for your medicine.");
    builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {
            switch(item)
            {
            case 0:
                Toast.makeText(context, "Good.", Toast.LENGTH_SHORT).show();
                break;
            case 1:
                Toast.makeText(context, "Reminder set in ten minutes.", Toast.LENGTH_SHORT).show();
                break;
            case 2:
                Intent service1 = new Intent(context, DoseAlarmService.class);
                service1.putExtra("doseName", doseName);
                context.startService(service1);
                break;
            }
        }
    });
    levelDialog = builder.create();
    levelDialog.show();
}   

我尝试过使用getApplicationContext而不是context在开关盒内,但这是我得到的确切错误:

The method getApplicationContext() is undefined for the type new DialogInterface.OnClickListener(){}

关于如何前进的任何建议?

编辑:

到目前为止,这些是我尝试过的:

public void onReceive(final Context context, Intent intent)
{
        ctx = context;
    final CharSequence[] items = {" I'm taking the dose now! "," Remind again in ten minutes. "," Ignore for now. "};
    String doseName = intent.getStringExtra("doseName");
    Toast.makeText(ctx, "Take medicine: " + doseName, Toast.LENGTH_LONG).show();

    AlertDialog.Builder builder = new AlertDialog.Builder(ctx.getApplicationContext());
    builder.setTitle("It's time for your medicine.");
    builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {
            switch(item)
            {
            case 0:
                Toast.makeText(ctx, "Good.", Toast.LENGTH_SHORT).show();
                break;
            case 1:
                Toast.makeText(ctx, "Reminder set in ten minutes.", Toast.LENGTH_SHORT).show();
                break;
            case 2:
                Intent service1 = new Intent(ctx.getApplicationContext(), DoseAlarmService.class);
                service1.putExtra("doseName", doseName);
                ctx.startService(service1);
                break;
            }
        }
    });
    levelDialog = builder.create();
    levelDialog.show();
}   

另外,我没有使用ctx,而是直接使用context.getApplicationContext()并检查了。它不起作用。

此外,当我注释掉所有有问题的区域并运​​行以验证对话框是否出现时,我得到了这个异常:

07-23 13:26:21.316: E/AndroidRuntime(1756): java.lang.RuntimeException: Unable to start receiver com.dosemanager.ui.DoseAlarmReceiever: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

请帮忙!

4

3 回答 3

3

您正在尝试从广播接收器启动警报对话框,这是不允许的。看一下这个:

系统重新启动后在广播接收器中显示警报对话框

于 2013-07-23T08:05:50.617 回答
3

嗯,你已经有了你的上下文——它的onReceive()参数。你不需要使用getApplicationContext().

编辑:您不能在 switch case 中使用上下文,因为上下文是在 Receiver 类中定义的,而您正试图在onClickListener类中使用它。我建议这样做:

  public class %YOUR_RECEIVER_CLASS% {
       private Context context;
       public onReceive(Context context, ...) {
            this.context = context;
       }
  }

现在您可以在任何地方使用上下文

于 2013-07-23T07:40:49.553 回答
0

使用 getBaseContext() 而不是 getApplicationContext()

示例广播接收器::

 @Override
     public void onCreate() 
     {        
         Toast.makeText(this, "SmsSenderService()", Toast.LENGTH_LONG).show();

         sendBroadcastReceiver = new BroadcastReceiver()
         {
                public void onReceive(Context arg0, Intent intent)
               {
                 switch (getResultCode())
                 {
                  case Activity.RESULT_OK:                  

                    Toast.makeText(getBaseContext(), "SMS Sent", Toast.LENGTH_SHORT).show();        

                    break;

                }
            }
        };
于 2013-07-23T07:52:38.097 回答