android– 在类中扩展BroadcastReceiver使用上下文

android– 在类中扩展BroadcastReceiver使用上下文,第1张

概述当android中的警报响起时,我想创建一个AlertDialog.此外,我想创建一个通知,具体取决于用户在对话框上的单选按钮中单击的选项.当我尝试使用context或getApplicationContext()时出现问题.这是我的代码:publicvoidonReceive(finalContextcontext,Intentintent){final

当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();}   

我尝试在switch case中使用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

请帮忙!

解决方法:

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

编辑:你不能在switch case中使用context,因为在Receiver类中定义了context并且你试图在onClickListener类中使用它.
我建议这个:

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

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

总结

以上是内存溢出为你收集整理的android – 在类中扩展BroadcastReceiver使用上下文全部内容,希望文章能够帮你解决android – 在类中扩展BroadcastReceiver使用上下文所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1110260.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存