由于没有收到线位置数据 [英] Not receiving location data from thread

查看:113
本文介绍了由于没有收到线位置数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图发送一个包含用户的位置,每隔一段时间使用定时器短信。起初,我遇到了一个N​​ullPointerException异常这是由于一个简单的错误我犯了。一旦这种固定一切似乎都运行良好。然而,它从来没有得到我的位置,所以,这一直发送是在说无法接收位置文本。

我所问的是为什么它没有得到我的位置?而我能做些什么来解决这个问题?

有没有logcat的错误,我用两个模拟器来测试这个程序(发送从一个到另一个文本)。如果你能提供任何帮助或解决方案,将大大AP preciated!如果我俯瞰的东西,似乎是很简单的,请提醒我,我这样做。谢谢!

这里的code:

 公共类MessageService扩展服务{
INT计数器= 0;
私人定时器定时器=新的Timer();
公共字符串textTime,phoneNumber的;
公众诠释updateInterval;
诠释纬度,经度;
串坐标,经度,纬度;
的LocationManager的LocationManager;@覆盖
公众的IBinder onBind(意向为arg0){
    返回null;
}@覆盖
公众诠释onStartCommand(意向意图,诠释标志诠释startId){
    //从主叫接收意图的意图演员
    textTime = intent.getStringExtra(textTime);
    phoneNumber的= intent.getStringExtra(手机);    phoneNumber的=5556;    // if语句具有与串textTime转移到可用于一个数字来执行以下
    如果(textTime.equals(15分钟)){
        updateInterval =(15 *(60000));
    }否则如果(textTime.equals(30分钟)){
        updateInterval =(30 *(60000));
    }否则如果(textTime.equals(1小时)){
        updateInterval =(60 *(60000));
    }其他{
        updateInterval =(15 *(60000));
    }    的LocationManager =(的LocationManager)this.getSystemService(Context.LOCATION_SERVICE);    新的Thread(){
        公共无效的run(){
            尺蠖prepare()。
            //定义,响应位置更新的监听器
            LocationListener的LocationListener的=新LocationListener的(){
                公共无效onLocationChanged(地点){
                  //当一个新的位置由网络位置提供商发现调用。
                  // makeUseOfNewLocation(位置);
                    纬度=(INT)(location.getLatitude()* 1E6);
                    LNG =(INT)(location.getLongitude()* 1E6);                    纬度= Integer.toString(LAT);
                    经度= Integer.toString(LNG);                    坐标=坐标:+纬度+,+经度+纬度:+纬度+经度。回应END停止文本+经度+;
                }                公共无效onStatusChanged(字符串提供商,INT地位,捆绑演员){}                公共无效onProviderEnabled(字符串提供商){}                公共无效onProviderDisabled(字符串提供商){}
              };              Looper.loop();              如果(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,LocationListener的);
              }其他{
                    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,LocationListener的);
              }        }
    }。开始();
    //下面的方法应该用一个定时器在定时间隔发送短信。还应该实现使用不同的线程
    doSomethingRepeatedly();    返回START_STICKY;}公共无效doSomethingRepeatedly(){
    timer.scheduleAtFixedRate(新的TimerTask(){
        公共无效的run(){
            //下面的code应该是什么反复做
            //Log.d(\"MessageService,将String.valueOf(++计数器));            sendSmsMessage();
        }
    },0,updateInterval);
}//下面的code处理发送短信
公共无效sendSmsMessage(){
    SmsManager短信= SmsManager.getDefault();
    如果(坐标== NULL || coordinates.equals()){
        坐标=无法接收位置;
    }    sms.sendTextMessage(phoneNumber的,空,协调,NULL,NULL);}公共无效的onDestroy(){
    super.onDestroy();    如果(定时器!= NULL){
        timer.cancel();
    }
}
} //服务端


解决方案

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


 公共类MessageService扩展服务实现LocationListener的{
    INT计数器= 0;
    私人定时器定时器=新的Timer();
    公共字符串textTime,phoneNumber的;
    公众诠释updateInterval;
    诠释纬度,经度;
    串坐标,经度,纬度;
    的LocationManager的LocationManager;    @覆盖
    公众的IBinder onBind(意向为arg0){
        返回null;
    }    @覆盖
    公众诠释onStartCommand(意向意图,诠释标志诠释startId){
        //从主叫接收意图的意图演员
        textTime = intent.getStringExtra(textTime);
        phoneNumber的= intent.getStringExtra(手机);        phoneNumber的=5556;        // if语句具有与串textTime转移到可用于一个数字来执行以下
        如果(textTime.equals(15分钟)){
            updateInterval =(15 *(60000));
        }否则如果(textTime.equals(30分钟)){
            updateInterval =(30 *(60000));
        }否则如果(textTime.equals(1小时)){
            updateInterval =(60 *(60000));
        }其他{
            updateInterval =(15 *(60000));
        }        的LocationManager =(的LocationManager)this.getSystemService(Context.LOCATION_SERVICE);        如果(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,这一点);
        }其他{
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,这一点);
        }        //下面的方法应该用一个定时器在定时间隔发送短信。还应该实现使用不同的线程
        doSomethingRepeatedly();        返回START_STICKY;    }    公共无效doSomethingRepeatedly(){
        timer.scheduleAtFixedRate(新的TimerTask(){
            公共无效的run(){
                //下面的code应该是什么反复做
                //Log.d(\"MessageService,将String.valueOf(++计数器));                sendSmsMessage();
            }
        },0,updateInterval);
    }    //下面的code处理发送短信
    公共无效sendSmsMessage(){
        SmsManager短信= SmsManager.getDefault();
        如果(坐标== NULL || coordinates.equals()){
            坐标=无法接收位置;
        }        sms.sendTextMessage(phoneNumber的,空,协调,NULL,NULL);    }    公共无效的onDestroy(){
        super.onDestroy();        如果(定时器!= NULL){
            timer.cancel();
        }
    }    @覆盖
    公共无效onLocationChanged(地点){
        //当一个新的位置由网络位置提供商发现调用。
        // makeUseOfNewLocation(位置);
        纬度=(INT)(location.getLatitude()* 1E6);
        LNG =(INT)(location.getLongitude()* 1E6);        纬度= Integer.toString(LAT);
        经度= Integer.toString(LNG);        坐标=坐标:+纬度+,+经度+纬度:+纬度+经度。回应END停止文本+经度+;
    }    @覆盖
    公共无效onProviderDisabled(字符串提供商){}    @覆盖
    公共无效onProviderEnabled(字符串提供商){}    @覆盖
    公共无效onStatusChanged(字符串提供商,INT地位,捆绑演员){}
}

I'm trying to send text messages containing the users location every so often using the timer. Initially, I ran into a nullpointerexception which was due to a simple mistake I made. Once this was fixed everything seemed to be running fine. However, it never gets my location, so, the text that keeps sending is saying "Could Not receive location".

What I am asking is why is it not getting my location? And what can I do to fix the problem?

There are no logcat errors and I am using two emulators to test this app (send text from one to the other). If you could provide any help or resolution it would be greatly appreciated! And if I'm overlooking something that may seem to be very simple please alert me that I am doing so. Thanks!

Here's the code:

    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

解决方案

Your LocationListener is probably being destroyed before onLocationChanged() can be called. Your Service should implement the LocationListener itself.


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) {}
}

这篇关于由于没有收到线位置数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆