gpt4 book ai didi

java - Android 2.3 无法在未调用 Looper.prepare() (AsyncTask) 的线程内创建处理程序

转载 作者:行者123 更新时间:2023-12-01 21:15:31 24 4
gpt4 key购买 nike

我有一个 FCM 服务类,我在其中获取 token 并使用 AsyncTask 发送到我的服务器。此错误仅发生在使用 Android 2.3 的设备上启动我的应用程序我尝试将 token 发送到服务器,甚至不运行我的 AsyncTaskStracktrace

Fatal Exception: java.lang.ExceptionInInitializerError
at br.lgfelicio.gcm.MyInstanceIDListenerService.sendRegistrationToServer(MyInstanceIDListenerService.java:39)
at br.lgfelicio.gcm.MyInstanceIDListenerService.onTokenRefresh(MyInstanceIDListenerService.java:30)
at com.google.firebase.iid.FirebaseInstanceIdService.zzag(Unknown Source)
at com.google.firebase.iid.FirebaseInstanceIdService.zzm(Unknown Source)
at com.google.firebase.iid.zzb$2.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
at java.lang.Thread.run(Thread.java:1019)
Caused by java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

我不明白为什么 android 2.3 会发生此错误,我如何从该版本将 token 发送到我的服务器?

类 FCM 服务

public class MyInstanceIDListenerService extends FirebaseInstanceIdService {
private static final String SAVE_ABERTURA = "1";
private Armazenamento armazenamento = new Armazenamento(this);

@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();

String tokenFcm = armazenamento.LoadPreferences("tokenFcm");

if (tokenFcm != null && !tokenFcm.equals("") && !refreshedToken.equals(tokenFcm)) {
removeTokenFcm(tokenFcm);
}

// TODO: Implement this method to send any registration to your app's servers.
sendRegistrationToServer(refreshedToken);
}

private void removeTokenFcm(String tokenFcm) {
new RemoveTokenTask(getApplicationContext(), "remove").execute(tokenFcm);
}

private void sendRegistrationToServer(String refreshedToken) {

new RegistroTokenTask(getApplicationContext(), refreshedToken, "", SAVE_ABERTURA).execute();
}
}

异步任务

public class RegistroTokenTask extends AsyncTask<Void, Void, String> {

private Context context;
private String tokenFcm;
private String key;

// save = 1 veio do abertura e save = 2 veio do login
private String save;

public RegistroTokenTask(Context context, String tokenFcm, String key, String save) {
this.tokenFcm = tokenFcm;
this.key = key;
this.save = save;
this.context = context;
}

@Override
protected void onPreExecute() {
Log.i("checkin", "teste a: ");
}

@Override
protected String doInBackground(Void... params) {
String msg = "";

try {

Armazenamento arm = new Armazenamento(context);

if (key.trim().equals("")) {
key = arm.LoadPreferences("key");
}

// url...

DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(link);
msg = doc.getElementsByTagName("status").item(0).getTextContent();

} catch (Exception e) {
Log.i("checkin", "e: " +e.getMessage());
}

return msg;
}

@Override
protected void onPostExecute(String result) {

// se armazenou o token no banco de dados entao salva no storage
if (result.equals("Success") && save.equals("1")) {

Armazenamento tokenFcmStorage = new Armazenamento(context);
tokenFcmStorage.SavePreferences("tokenFcm", tokenFcm);
}
}
}

有人可以帮我吗

最佳答案

我设法找到了解决方案,但我不知道它是否是最好的。从我的谷歌搜索中,我在我的类(class)服务FCM中找到了这段代码:

Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(new Runnable() {
@Override
public void run() {
new RegistroTokenTask(getApplicationContext(), refreshedToken, "", SAVE_ABERTURA).execute();
}
}, 2000);

关于java - Android 2.3 无法在未调用 Looper.prepare() (AsyncTask) 的线程内创建处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40245726/

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