gpt4 book ai didi

android - 在 FirebaseMessagingService 中获取当前位置 GPS

转载 作者:太空狗 更新时间:2023-10-29 14:42:34 24 4
gpt4 key购买 nike

我不明白为什么 SEND_POSITION 通知在我的 HUAWEI p8 lite 中有效,而在 SAMSUNG Note3 中崩溃应用程序。我哪里错了?

With SAMSUNG I get this error:
E/UncaughtException: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:200)
at android.os.Handler.<init>(Handler.java:114)
at android.location.LocationManager$ListenerTransport$1.<init>(LocationManager.java:223)
at android.location.LocationManager$ListenerTransport.<init>(LocationManager.java:223)
at android.location.LocationManager.wrapListener(LocationManager.java:851)
at android.location.LocationManager.requestLocationUpdates(LocationManager.java:864)
at android.location.LocationManager.requestLocationUpdates(LocationManager.java:459)
at com.mifra.mifraqronda.GPSTracker.getLocation(GPSTracker.java:37)
at com.mifra.mifraqronda.FirebaseMessagingService.showNotification(FirebaseMessagingService.java:43)
at com.mifra.mifraqronda.FirebaseMessagingService.onMessageReceived(FirebaseMessagingService.java:30)
at com.google.firebase.messaging.FirebaseMessagingService.handleIntent(Unknown Source)
at com.google.firebase.iid.zzc.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)

代码:

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Vibrator;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.google.firebase.messaging.RemoteMessage;

import java.io.IOException;

public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {

Vibrator vib;
DatabaseHelper database = new DatabaseHelper(this);

String urlSite = "https://qronda.mifra.info/index.php";
String parameterUpdatePositionUser = "";
String apiKeyMifra = "fd57700b-af6c-4ba6-9ad9-cfc592028f35";

@Override
public void onMessageReceived(RemoteMessage remoteMessage){
showNotification(remoteMessage.getData().get("message"));
}

private void showNotification(String message) {

String[] messageSplit = message.split(":");

if(messageSplit[0].equals("SEND_POSITION")){
Log.wtf("Mifra in Notify", "Message: "+message);

// controllo posizione GPS
GPSTracker gps = new GPSTracker(this);
boolean toast = false;
Location location = gps.getLocation(toast);
if (gps.getLocation(toast) == null) {
vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vib.vibrate(1000);
//Toast.makeText(getApplicationContext(), "GPS non agganciato impossibile otterene la tua posizione", Toast.LENGTH_SHORT).show();
Log.d("Mifra SendPosition", "GPS non agganciato impossibile otterene la tua posizione");
} else {

double lat = location.getLatitude();
double lng = location.getLongitude();
String stringLat = Double.toString(lat);
String stringLng = Double.toString(lng);

Login login = database.selectLogin(1);
// task recupero dati sitoWeb login utente
parameterUpdatePositionUser = "option=com_mifrageo&task=app.updatePositionUser&format=json&id_user=" + login.getIduser()
+ "&lat=" + stringLat + "&lng=" + stringLng + "&apiKey=" + apiKeyMifra;
new UpdatePositionUserTask().execute(urlSite);

}
}

// Evento dopo la task updatePositionUsers
private class UpdatePositionUserTask extends AsyncTask<String, Void, String> {

@Override
protected String doInBackground(String... urls) {

try {
return ConnectAppMySql.postDati(urls[0], parameterUpdatePositionUser);
} catch (IOException e) {
return "Impossibile ricevere i dati della pagina web, URL invalido";
}
}

@Override
protected void onPostExecute(String result) {

try {

Log.d("Mifra SendPosition", "Posizione inviata e salvata" + result);

//Toast.makeText(getApplicationContext(), "Posizione inviata e salvata" + result, Toast.LENGTH_LONG).show();

} catch (Throwable t) {
Log.e("Mifra Monitor JSON", "Impossibile analizzare array JSON: \"" + result + "\"");
}
}
}
}

我该如何解决这个问题?

最佳答案

根据 This Question

你必须这样做:

@Override
public void onMessageReceived(RemoteMessage remoteMessage){

// some codes here
new Handler(Looper.getMainLooper()).post(() -> {
// your get location attempt codes
});


}

关于android - 在 FirebaseMessagingService 中获取当前位置 GPS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45378221/

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