Android 在类内使用上下文扩展BroadcastReceiver
当安卓系统中的警报响起时,我想创建一个AlertDialog。此外,我还想创建一个通知,具体取决于用户在对话框单选按钮中单击的选项。 当我尝试使用Android 在类内使用上下文扩展BroadcastReceiver,android,broadcastreceiver,android-alertdialog,android-alarms,Android,Broadcastreceiver,Android Alertdialog,Android Alarms,当安卓系统中的警报响起时,我想创建一个AlertDialog。此外,我还想创建一个通知,具体取决于用户在对话框单选按钮中单击的选项。 当我尝试使用context或getApplicationContext()时,问题就出现了 这是我的代码: public void onReceive(final Context context, Intent intent) { final CharSequence[] items = {" I'm taking the dose now! "," Re
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
请帮忙 Hm,您已经有了上下文-它是
onReceive()
的参数。您不需要使用getApplicationContext()
编辑:您不能在switch case中使用上下文,因为上下文是在Receiver类中定义的,您正试图在onClickListener
类中使用它。
我建议:
public class %YOUR_RECEIVER_CLASS% {
private Context context;
public onReceive(Context context, ...) {
this.context = context;
}
}
现在您可以随时随地使用上下文使用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;
}
}
};
您正试图从广播接收器启动警报对话框,这是不允许的。看看这个:
您可以使用context.getApplicationContext()。现在,这是另一个问题。如果没有活动,则无法显示对话框。创建新活动并在此处显示对话框。尝试使用“活动cxt=(活动)上下文;”并使用cxt作为您的上下文他可以在onClickListener中使用上下文,因为他的上下文是“final”LOL否。它是否为final,但它只是方法的一个参数,而不是类的一个成员。上下文不需要是类dude的一个成员。。。onClickListener只需要知道上下文不会改变,所以他使用“final”,看起来不太好,但它是正确的。您知道范围吗?onClickListener的未命名子对象中的方法onClick无权访问onReceive的作用域。所以,他无法访问作为onReceive参数的上下文。OnClickListener没有这样的方法。正如我所说,您需要将上下文推送到onClickListener,或者将上下文作为接收方的字段。我需要重复多少次?不要听谁建议使用getContext或getBaseContext或其他东西,因为他们什么都不知道