我想在类的 onHandleIntent()
方法中打开一个自定义对话框IntentService
,但是当我编写用于在此方法中显示 Dialog 的代码时,它会显示错误
The method findViewById(int) is undefined for the type MyAlarmService
谁能建议我如何解决这个问题?
我用过的代码:
public class MyAlarmService extends IntentService {
public void onCreate() {
super.onCreate();
}
public MyAlarmService() {
super("MyAlarmService");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, startId, startId);
Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG).show();
return START_STICKY;
}
@SuppressWarnings("static-access")
@Override
protected void onHandleIntent(Intent intent) {
final Dialog alarmDialog = new Dialog(MyAlarmService.this);
alarmDialog .requestWindowFeature(alarmDialog.getWindow().FEATURE_NO_TITLE);
alarmDialog .getWindow().setBackgroundDrawable(new ColorDrawable(0));
Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.clear_all_warning_dialog, (ViewGroup) findViewById(R.id.layout_warning_dialog));
TextView dialogTitile = (TextView) layout.findViewById(R.id.dialog_title_text);
TextView dialogDesc = (TextView) layout.findViewById(R.id.dialog_desc_text);
Button buttonYes = (Button) layout.findViewById(R.id.button_yes);
Button buttonNo = (Button) layout.findViewById(R.id.button_no);
alarmDialog.setContentView(layout);
alarmDialog.show();
buttonYes.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
alarmDialog.dismiss();
}
});
buttonNo.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
alarmDialog.dismiss();
}
});
}
}