0

所有..我是android新手。我尝试制作带有剩余警报的日历..我编写的所有内容都可以正常工作..但是“警报事件”仅在 LogCat 中作为 system.out.print 工作,但它无法在 android Emulator GUI 中显示。我通过使用线程在唤醒中制作了这个警报功能。在这个线程中,我想向 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

//////////////////这里警报框,消息框,吐司等一切都不起作用

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

2 回答 2

1
Handler handler = new Handler();
handler.post(new Runnable(){
     public void run(){
          //implement your thread code here
     }
 });
于 2012-09-28T05:02:32.417 回答
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();
于 2012-08-31T10:31:30.280 回答