Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 未从线程接收位置数据_Java_Android_Multithreading_Timer_Location - Fatal编程技术网

Java 未从线程接收位置数据

Java 未从线程接收位置数据,java,android,multithreading,timer,location,Java,Android,Multithreading,Timer,Location,我尝试每隔一段时间使用计时器发送包含用户位置的文本消息。最初,我遇到了一个nullpointerexception,这是由于我犯了一个简单的错误。一旦修好了,一切似乎都很好。但是,它从未获取我的位置,因此,不断发送的文本是“无法接收位置” 我想问的是,为什么它不知道我的位置?我能做些什么来解决这个问题 没有logcat错误,我正在使用两个模拟器来测试这个应用程序(从一个向另一个发送文本)。如果您能提供任何帮助或解决方案,我们将不胜感激!如果我忽略了一些看起来很简单的事情,请提醒我我正在这样做。谢

我尝试每隔一段时间使用计时器发送包含用户位置的文本消息。最初,我遇到了一个nullpointerexception,这是由于我犯了一个简单的错误。一旦修好了,一切似乎都很好。但是,它从未获取我的位置,因此,不断发送的文本是“无法接收位置”

我想问的是,为什么它不知道我的位置?我能做些什么来解决这个问题

没有logcat错误,我正在使用两个模拟器来测试这个应用程序(从一个向另一个发送文本)。如果您能提供任何帮助或解决方案,我们将不胜感激!如果我忽略了一些看起来很简单的事情,请提醒我我正在这样做。谢谢

代码如下:

    public class MessageService extends Service{
int counter = 0;
private Timer timer = new Timer();
public String textTime, phoneNumber;
public int updateInterval;
int lat, lng;
String coordinates, latitude, longitude;
LocationManager locationManager;

@Override
public IBinder onBind(Intent arg0) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId){
    //receives the intent extras from the calling intent
    textTime = intent.getStringExtra("textTime");
    phoneNumber = intent.getStringExtra("phone");

    phoneNumber = "5556";

    //the following if statement has to do with transferring the string textTime into a number that can be used
    if (textTime.equals("15 Minutes")) {
        updateInterval = (15 * (60000));
    }else if (textTime.equals("30 Minutes")) {
        updateInterval = (30 * (60000));
    }else if (textTime.equals("1 Hour")){
        updateInterval = (60 * (60000));
    }else {
        updateInterval = (15 * (60000));
    }

    locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

    new Thread(){
        public void run(){
            Looper.prepare();
            // Define a listener that responds to location updates
            LocationListener locationListener = new LocationListener() {
                public void onLocationChanged(Location location) {
                  // Called when a new location is found by the network location provider.
                  //makeUseOfNewLocation(location);
                    lat = (int) (location.getLatitude() * 1E6);
                    lng = (int) (location.getLongitude() * 1E6);

                    latitude = Integer.toString(lat);
                    longitude = Integer.toString(lng);

                    coordinates = "Coordinates: " + latitude + ", " + longitude + ". Latitude: " + latitude + " Longitude: " + longitude + ". Respond 'END' to stop texts."; 
                }

                public void onStatusChanged(String provider, int status, Bundle extras) {}

                public void onProviderEnabled(String provider) {}

                public void onProviderDisabled(String provider) {}
              };

              Looper.loop();

              if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
              }else{
                    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
              }

        }
    }.start();


    //the following method should use a timer to send a sms message in a timed interval. It also should implement using a different thread
    doSomethingRepeatedly();

    return START_STICKY;

}

public void doSomethingRepeatedly(){
    timer.scheduleAtFixedRate(new TimerTask() {
        public void run() {
            //the following code should be what is done repeatedly
            //Log.d("MessageService", String.valueOf(++counter));

            sendSmsMessage();
        }
    }, 0, updateInterval);
}

//the following code handles sending the text message
public void sendSmsMessage(){
    SmsManager sms = SmsManager.getDefault();
    if (coordinates == null || coordinates.equals("")){
        coordinates = "Could Not Receive Location";
    }

    sms.sendTextMessage(phoneNumber, null, coordinates , null, null);

}

public void onDestroy() {
    super.onDestroy();

    if (timer != null) {
        timer.cancel();
    }
}




}//end of service

我不能完全理解您的代码,但是如果您希望一个线程更新
坐标
字段,然后另一个线程读取
坐标
的内容,则需要使
坐标
字段
易变


线程之间共享的任何值都需要同步或以某种方式跨a才能显示。

在调用
onLocationChanged()
之前,您的LocationListener可能已被销毁。您的服务应该实现LocationListener本身



是的,那是我最初的帖子。这个问题现在已经解决了,但是,它现在无法获取我的位置是的,我试图在一个线程中更新变量,然后当计时器关闭时,它会发送带有相同变量的sms消息。那么,您的意思是,为了在线程之间使用变量,我只需要声明它为volatile?让我试试……我试过了,但还是不行。我找到了原因,我必须让服务更新位置侦听器本身,否则,它在得到我的坐标之前就被破坏了。另一位用户指出了这一点,但他似乎出于某种原因删除了自己的帖子。但我感谢你的帮助!我以前不知道volatile或者如何使用它,谢谢@user1478764我把我的答案放了回去,因为你一开始是这么想的,我觉得我的答案太离谱了。。。
public class MessageService extends Service implements LocationListener {
    int counter = 0;
    private Timer timer = new Timer();
    public String textTime, phoneNumber;
    public int updateInterval;
    int lat, lng;
    String coordinates, latitude, longitude;
    LocationManager locationManager;

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId){
        //receives the intent extras from the calling intent
        textTime = intent.getStringExtra("textTime");
        phoneNumber = intent.getStringExtra("phone");

        phoneNumber = "5556";

        //the following if statement has to do with transferring the string textTime into a number that can be used
        if (textTime.equals("15 Minutes")) {
            updateInterval = (15 * (60000));
        }else if (textTime.equals("30 Minutes")) {
            updateInterval = (30 * (60000));
        }else if (textTime.equals("1 Hour")){
            updateInterval = (60 * (60000));
        }else {
            updateInterval = (15 * (60000));
        }

        locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
        }else{
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
        }

        //the following method should use a timer to send a sms message in a timed interval. It also should implement using a different thread
        doSomethingRepeatedly();

        return START_STICKY;

    }

    public void doSomethingRepeatedly(){
        timer.scheduleAtFixedRate(new TimerTask() {
            public void run() {
                //the following code should be what is done repeatedly
                //Log.d("MessageService", String.valueOf(++counter));

                sendSmsMessage();
            }
        }, 0, updateInterval);
    }

    //the following code handles sending the text message
    public void sendSmsMessage(){
        SmsManager sms = SmsManager.getDefault();
        if (coordinates == null || coordinates.equals("")){
            coordinates = "Could Not Receive Location";
        }

        sms.sendTextMessage(phoneNumber, null, coordinates , null, null);

    }

    public void onDestroy() {
        super.onDestroy();

        if (timer != null) {
            timer.cancel();
        }
    }

    @Override
    public void onLocationChanged(Location location) {
        // Called when a new location is found by the network location provider.
        //makeUseOfNewLocation(location);
        lat = (int) (location.getLatitude() * 1E6);
        lng = (int) (location.getLongitude() * 1E6);

        latitude = Integer.toString(lat);
        longitude = Integer.toString(lng);

        coordinates = "Coordinates: " + latitude + ", " + longitude + ". Latitude: " + latitude + " Longitude: " + longitude + ". Respond 'END' to stop texts."; 
    }

    @Override
    public void onProviderDisabled(String provider) {}

    @Override
    public void onProviderEnabled(String provider) {}

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {}
}