所有..我是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
/////////////////////这里警告Box,消息框,Toast等一切都不起作用 < / em>的
}
} while (c.moveToNext());
}
}
Thread.sleep(10000);
} catch (Exception e) {
}
}
}
答案 0 :(得分:1)
我建议你使用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();
}
}
从活动中调用它:
MyAsyncTask task = new AsyncTask(myActivity.this);
task.execute();
答案 1 :(得分:1)
Handler handler = new Handler();
handler.post(new Runnable(){
public void run(){
//implement your thread code here
}
});