1

我正在使用非常有用的 CWAC-LocationPoller.jar 库(Mark Murphy 的帽子提示),它提供了“com.commonsware.cwac.locpoll.LocationPoller”类。问题是,似乎每次位置轮询警报触发时,我都会收到此警告。

08-09 16:37:06.421: W/MessageQueue(31220): Handler{4055c2e8} sending message to a Handler on a dead thread
08-09 16:37:06.421: W/MessageQueue(31220): java.lang.RuntimeException: Handler{4055c2e8} sending message to a Handler on a dead thread
08-09 16:37:06.421: W/MessageQueue(31220):  at android.os.MessageQueue.enqueueMessage(MessageQueue.java:196)
08-09 16:37:06.421: W/MessageQueue(31220):  at android.os.Handler.sendMessageAtTime(Handler.java:457)
08-09 16:37:06.421: W/MessageQueue(31220):  at android.os.Handler.sendMessageDelayed(Handler.java:430)
08-09 16:37:06.421: W/MessageQueue(31220):  at android.os.Handler.sendMessage(Handler.java:367)
08-09 16:37:06.421: W/MessageQueue(31220):  at android.location.LocationManager$ListenerTransport.onStatusChanged(LocationManager.java:206)
08-09 16:37:06.421: W/MessageQueue(31220):  at android.location.ILocationListener$Stub.onTransact(ILocationListener.java:75)
08-09 16:37:06.421: W/MessageQueue(31220):  at android.os.Binder.execTransact(Binder.java:320)
08-09 16:37:06.421: W/MessageQueue(31220):  at dalvik.system.NativeStart.run(Native Method)

即使使用作为 CWAC-LocationPoller github 下载的一部分提供的简单“演示”项目,我也会收到此警告。以下是演示项目中两个 java 文件的副本,以供记录(为简洁起见,已截断版权声明)。

LocationPollerDemo.java

package com.commonsware.cwac.locpoll.demo;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.Toast;
import com.commonsware.cwac.locpoll.LocationPoller;

public class LocationPollerDemo extends Activity {
  private static final int PERIOD=1800000;  // 30 minutes
  private PendingIntent pi=null;
  private AlarmManager mgr=null;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mgr=(AlarmManager)getSystemService(ALARM_SERVICE);

    Intent i=new Intent(this, LocationPoller.class);

    i.putExtra(LocationPoller.EXTRA_INTENT,
           new Intent(this, LocationReceiver.class));
    i.putExtra(LocationPoller.EXTRA_PROVIDER,
           LocationManager.GPS_PROVIDER);

    pi=PendingIntent.getBroadcast(this, 0, i, 0);
    mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                  SystemClock.elapsedRealtime(),
                  PERIOD,
                  pi);

    Toast
      .makeText(this,
            "Location polling every 30 minutes begun",
            Toast.LENGTH_LONG)
     .show();
  }

  public void omgPleaseStop(View v) {
    mgr.cancel(pi);
    finish();
  }
}

LocationReceiver.java

package com.commonsware.cwac.locpoll.demo;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import com.commonsware.cwac.locpoll.LocationPoller;

public class LocationReceiver extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {
    File log=
        new File(Environment.getExternalStorageDirectory(),
                 "LocationLog.txt");

    try {
      BufferedWriter out=
          new BufferedWriter(new FileWriter(log.getAbsolutePath(),
                                        log.exists()));

      out.write(new Date().toString());
      out.write(" : ");

      Bundle b=intent.getExtras();
      Location loc=(Location)b.get(LocationPoller.EXTRA_LOCATION);
      String msg;

      if (loc==null) {
        loc=(Location)b.get(LocationPoller.EXTRA_LASTKNOWN);

        if (loc==null) {
          msg=intent.getStringExtra(LocationPoller.EXTRA_ERROR);
        }
        else {
          msg="TIMEOUT, lastKnown="+loc.toString();
        }
      }
      else {
         msg=loc.toString();
      }

      if (msg==null) {
        msg="Invalid broadcast received!";
      }

      out.write(msg);
      out.write("\n");
      out.close();
    }
    catch (IOException e) {
      Log.e(getClass().getName(), "Exception appending to log file", e);
    }
 }
}

环境细节

  • Eclipse IDE(4.2.0 版)
  • 构建目标:Android-8
  • 目标设备 API 版本:Android-10 (2.3.5)
  • 目标设备型号:三星 Galaxy Y (GT-S5360)

我已经尝试使用预构建的 jar 文件(可从 github 下载)和本地构建的库版本,结果相同。

任何想法/想法将不胜感激。我真的很想使用这个功能(我已经在我的应用程序中使用了其他 CWAC 优点),但担心重复警告。

提前致谢),

西德

4

0 回答 0