2011-02-09 133 views
1

我想不出一个适当的方式来等待对象出现。我正在写一个相机应用程序。拍摄照片后,我将GPS数据写入exif标签。在写之前,我必须等待位置对象出现。我的快速和肮脏的解决办法是开始一个新的线程,并使用while循环“等待”对象:Android:等待对象出现

 private static class MyRunnable implements Runnable { 
     private final String imagePath; 
     private final String thumbPath; 
     MyRunnable(final String anImagePath, String aThumbPath) { 
      this.imagePath = anImagePath; 
      this.thumbPath = aThumbPath; 
     } 

     public void run() { 
      while (mCurrentLocation == null) { 
       //do nothing 
      } 
      try { 
      writeExifTags(imagePath); 
      writeExifTags(thumbPath); 
      } 
      catch (NullPointerException e) { 
       Log.i(TAG, "NullPointerException"); 
      } 
     } 
     } 

这工作,但空while循环看起来非常难看。我想到对象的某种处理程序,但不能想出一种方法来使用处理程序来检查mCurrentLocation的存在。任何人都有机智的闪光? :)(是的try/catch块现在已经过时^^)

回答

0

你可以尝试使用AsyncTask?

+0

嗯,我可以。但我不认为我现有的方法有什么优势。我的“主要”问题就是如何定期检查一个对象是否为null。一个优势可能是睡眠/等待while循环... – Redfox 2011-02-10 17:12:19