我使用一個 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();
}
}
我創建了一個blank activity MYExtraActivity,然後在 Receiver 中調用
Intent newIntent = new Intent(context, MYExtraActivity .class);
context.startActivity(newIntent);