【问题标题】:Android 2.3 Can't create handler inside thread that has not called Looper.prepare() (AsyncTask)Android 2.3 无法在未调用 Looper.prepare() (AsyncTask) 的线程内创建处理程序
【发布时间】:2017-03-07 20:13:30
【问题描述】:

我有一个 FCM 服务类,我在其中获取令牌并使用 AsyncTask 发送到我的服务器。此错误仅发生在具有 Android 2.3 的设备上以启动我的应用程序我尝试将令牌发送到服务器,甚至没有运行我的 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会出现这个错误,我怎样才能从那个版本将令牌发送到我的服务器?

类 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);
    }
}
}

谁能帮帮我

【问题讨论】:

标签: java android firebase android-asynctask firebase-cloud-messaging


【解决方案1】:

我设法找到了解决方案,但我不知道它是否是最好的。从我在我的课堂服务 FCM 中搜索到的代码:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-28
    • 1970-01-01
    • 1970-01-01
    • 2011-10-17
    相关资源
    最近更新 更多