gpt4 book ai didi

java - 如何在线程中显示消息框?

转载 作者:行者123 更新时间:2023-12-02 07:36:05 25 4
gpt4 key购买 nike

所有..我是android新手。我尝试制作一个带有剩余警报的日历。我编码的所有内容都工作正常。但是“警报事件”仅在 LogCat 中作为 system.out.print 工作,但它无法在 android 模拟器 GUI 中显示。我通过使用Thread在wakeUp中实现了这个闹钟功能。在此线程中,我想向 GUI 显示一个警报框。这里我附上了代码。谁能告诉我如何在线程中显示消息吗?

编码:

public void run() {
for (;;) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmm");
Calendar cal = Calendar.getInstance();
stime = dateFormat.format(cal.getTime());
arrCRInt = Long.parseLong(stime);
try {
i = 0;
c = db1.rawQuery("SELECT * FROM " + CRA, null);
if (c != null) {
if (c.moveToFirst()) {
do {
dbdatetime = c.getString(c.getColumnIndex("rdatetime"));
arrDBInt = Long.parseLong(dbdatetime);
remName = c.getString(c.getColumnIndex("rname"));
rem_id = c.getString(c.getColumnIndex("reminder_id"));
alertId=c.getString(c.getColumnIndex("alert"));
if (arrDBInt < arrCRInt) {
if(alertId.equals("TRUE"))
{
///////////////////// Passing Alert Message to Android Emulator GUI
db1.execSQL("UPDATE "+CRA+" SET "+ALERT+"='FALSE' WHERE reminder_id = " + rem_id );
}
db1.execSQL("UPDATE "+CRA+" SET "+STATUS+"='TRUE' WHERE "+CRDATETIME+"<"+arrCRInt +" and reminder_id = " + rem_id );
}
if (arrDBInt == arrCRInt) {
///////////////////// Passing Alert Message to Android Emulator GUI
System.out.println("ALERTED AS: You Have " + remName + " Remainder as on this Time");
db1.execSQL("UPDATE "+CRA+" SET "+ALERT+"='FALSE' WHERE "+CRDATETIME+"="+arrCRInt +" and reminder_id = " + rem_id );
}
if(arrDBInt>arrCRInt)
{
///////////////////// Passing Alert Message to Android Emulator GUI

/////////////////////这里警报框、消息框、Toast等一切都不起作用

                            }
} while (c.moveToNext());
}
}
Thread.sleep(10000);
} catch (Exception e) {
}
}
}

最佳答案

我建议您使用 AsyncTask :

public class MyAsyncTask extends AsyncTask<Void, Void, Result>{

private Activity activity;
private ProgressDialog progressDialog;

public MyAsyncTask(Activity activity) {
super();
this.activity = activity;
}

@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show(activity, "Loading", "Loading", true);
}

@Override
protected Result doInBackground(Void... v) {
//do your stuff here
return null;

}

@Override
protected void onPostExecute(Result result) {
progressDialog.dismiss();
Toast.makeText(activity.getApplicationContext(), "Finished.",
Toast.LENGTH_LONG).show();
}


}

从 Activity 中调用它:

MyAsyncTask task = new AsyncTask(myActivity.this);
task.execute();

关于java - 如何在线程中显示消息框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12213501/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com