gpt4 book ai didi

android - 如何请求和删除从线程到服务的 locationManager 更新

转载 作者:行者123 更新时间:2023-11-30 04:34:29 25 4
gpt4 key购买 nike

我有一个服务使用此接口(interface)的方法实现接口(interface) LocationListener。

当我运行该服务时,我实例化了一个 LocationManager。

然后我启动一个线程,该线程将无限循环不间断地运行。

从这个线程开始,我希望我可以在我的 locationManager 上创建一个 locationManager.removeupdates 在服务开始时实例化。

但是有一个问题,显然我需要一个 Looper,我尝试了很多东西,但我不能使用它。

基本上,这是我的代码,显然,我不知道如何使用 Looper,因为我的代码在 Log.d("GPS", "GPS Activé"); 之后停止了

我在 Looper 上搜索过一些东西,但找到了用我的语言(我是法语)的易于理解的操作方法真的很难。

代码可能看起来很奇怪,因为,因为我删除了很多东西......

public class ServicePrincipal extends Service implements LocationListener {

boolean localisationOn = false;

LocationManager locationManager;

public class MyBinder extends Binder{
ServicePrincipal getService(){
return ServicePrincipal.this;
}
}

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

@Override
public void onCreate() {
super.onCreate();
}

@Override
public void onDestroy() {
locationManager.removeUpdates(this);
super.onDestroy();
}

@Override
public void onStart(Intent intent, int startId) {

MyThread mythread = new MyThread();
MyThread.start();
super.onStart(intent, startId);
}

public class MyThread extends Thread {

int nbInfos;
@Override
public void run() {

for (;;) {

if (localisationOn)
{
localisationOn = false;
Looper.prepare();
stopGPS();
Looper.loop();
}

if (!localisationOn)
{
Looper.prepare();
startGPS();
Looper.loop();
/* On active le flag de localisation */
localisationOn = true;
}
}
try {
Log.d("Boucle for", "~~~~ Fin de boucle ~~~~");
this.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

public void onLocationChanged(Location location) {
if ((location != null) && (localisationOn))
{
Log.d("Localisation", "Envoi des informations de localisation avec :");
Log.d("Latitude", String.valueOf(location.getLatitude()));
Log.d("Longitude", String.valueOf(location.getLongitude()));
}

}

public void onProviderDisabled(String provider) {

}

public void onProviderEnabled(String provider) {

}

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

}

public void startGPS()
{
/* Intent du service de localisation */
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
/* On active l'actualisation par le GPS et par le réseau téléphonique */
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,1,1,this);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1,1,this);

Log.d("GPS", "GPS Activé");
}

public void stopGPS()
{

locationManager.removeUpdates(this);
Log.d("GPS", "GPS Désactivé");
}

}

最佳答案

当从线程调用requestLocationUpdates() 时会发生此问题。您的程序将崩溃。我在工作时解决了该问题。我修改了你的代码。希望现在你的循环问题将得到解决。此外,它将消除你的无限循环问题。希望对你有所帮助。

public class ServicePrincipal extends Service implements LocationListener {

boolean localisationOn = false;

LocationManager locationManager;
private final Handler handler = new Handler();
public class MyBinder extends Binder{
ServicePrincipal getService(){
return ServicePrincipal.this;
}
}

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

@Override
public void onCreate() {
super.onCreate();
}

@Override
public void onDestroy() {
locationManager.removeUpdates(this);
super.onDestroy();
}

@Override
public void onStart(Intent intent, int startId) {

/*MyThread mythread = new MyThread();
MyThread.start();*/
handler.post(getData);
super.onStart(intent, startId);
}

private final Runnable getData = new Runnable() {
public void run() {
getDataFrame();
}
};

private void getDataFrame() {
if (localisationOn){
localisationOn = false;
stopGPS();
}
if (!localisationOn){
startGPS();
/* On active le flag de localisation */
localisationOn = true;
}
handler.postDelayed(getData,10000);

}
public void onLocationChanged(Location location) {
if ((location != null) && (localisationOn))
{
Log.d("Localisation", "Envoi des informations de localisation avec :");
Log.d("Latitude", String.valueOf(location.getLatitude()));
Log.d("Longitude", String.valueOf(location.getLongitude()));
}

}

public void onProviderDisabled(String provider) {

}

public void onProviderEnabled(String provider) {

}

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

}

public void startGPS()
{
/* Intent du service de localisation */
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
/* On active l'actualisation par le GPS et par le réseau téléphonique */
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,1,1,this);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1,1,this);

Log.d("GPS", "GPS Activé");
}

public void stopGPS()
{

locationManager.removeUpdates(this);
Log.d("GPS", "GPS Désactivé");
}

}

关于android - 如何请求和删除从线程到服务的 locationManager 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7106888/

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