gpt4 book ai didi

java - 未从线程接收位置数据

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:12:39 24 4
gpt4 key购买 nike

我经常尝试使用计时器发送包含用户位置的短信。最初,我遇到了一个空指针异常,这是由于我犯了一个简单的错误。一旦解决了这个问题,一切似乎都运行良好。但是,它永远无法获取我的位置,因此,不断发送的文本显示“无法接收位置”。

我想问的是为什么它没有获取我的位置?我该怎么做才能解决这个问题?

没有 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

最佳答案

您的 LocationListener 可能在调用 onLocationChanged() 之前被销毁。您的服务应该自己实现 LocationListener。


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

关于java - 未从线程接收位置数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12983431/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com