2012-08-31 63 views
0

all ..我是android新手。我試着做一個日曆與餘數警報..一切我編碼和工作正常..但'警報事件'只在LogCat作爲system.out.print但它不會能夠顯示在Android模擬器GUI。我使用Thread來喚醒這個鬧鐘功能。 在這個線程我想顯示一個警報框到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) { 
      } 
     } 
    } 

回答

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(); 
+0

感謝兄弟..在那裏我可以插入這一個在線程類 – user1638214

1
Handler handler = new Handler(); 
handler.post(new Runnable(){ 
    public void run(){ 
      //implement your thread code here 
    } 
});