我使用一个 receiver 每周来创建一个 alertbox。但是确获得错误:Unable to add window -- token null is not for an application代码如下:public class AlarmReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
try {
displayAlert("Have you seen your chiropractor this month?", "Alert!", context);
}
catch (Exception e)
{
Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
public void displayAlert(final String error, String title, final Context context)
{
new AlertDialog.Builder(context.getApplicationContext()).setMessage(error)
.setTitle(title)
.setCancelable(true)
.setNeutralButton("Continue",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton){
dialog.cancel();
Intent newIntent = new Intent(context, Appointment.class);
context.startActivity(newIntent);
}
})
.show();
}
}
2 回答
慕桂英4014372
TA贡献1871条经验 获得超13个赞
context.getApplicationContext() 不需要使用这个 直接context就行了 Dialog是依托于Activity的生命周期,不是ApplicationContext
翻阅古今
TA贡献1780条经验 获得超5个赞
我创建了一个blank activity MYExtraActivity,然后在 Receiver 中调用
Intent newIntent = new Intent(context, MYExtraActivity .class); context.startActivity(newIntent);
添加回答
举报
0/150
提交
取消