Android:等待对象出现

发布于 2024-10-16 20:21:41 字数 943 浏览 4 评论 0原文

我想不出等待对象出现的正确方法。我正在编写一个相机应用程序。拍照后,我将 GPS 数据写入 exif 标签中。在写入之前,我必须等待位置对象出现。我的快速而肮脏的解决方法是启动一个新线程并使用 while 循环来“等待”对象:

      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 块现在已经过时了 ^^)

I cannot think of a proper way to wait for an object to appear. I am writing a camera app. After taking a picture, I am writing GPS Data into the exif tags. I have to wait for the location object to appear, before writing. My quick and dirty fix is to start a new thread and use a while loop to "wait" for the object:

      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");
             }
         }
      }

This works but the empty while-loop looks very ugly. I think of some kind of handler for the object but cannot think of a way to use handlers to check for the existence of mCurrentLocation. Anyone with a flash of wit? :) (Yes the try/ catch block is obsolete now ^^)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盗琴音 2024-10-23 20:21:41

您可以尝试使用 AsyncTask 吗?

You could try using an AsyncTask?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文