0

我有一個FCM服務類,其中我拿起令牌並使用AsyncTask發送到我的服務器。此錯誤只發生在具有Android 2.3的設備上以啓動我的應用程序我嘗試將令牌發送到服務器,甚至不運行我的AsyncTaskStracktraceAndroid 2.3無法在未調用Looper.prepare()(AsyncTask)的線程中創建處理程序

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

的AsyncTask

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

有人可以幫我

+0

的[無法創建內螺紋已不叫Looper.prepare()裏面的AsyncTask的ProgressDialog處理]可能的複製(HTTP:/ /stackoverflow.com/questions/3614663/cant-create-handler-inside-thread-that-has-not-called-looper-prepare-inside-a) – Onik

+0

你好,感謝評論。我嘗試過使用你說過的解決方案,但沒有執行我的任務。 – ruitercomp

回答

3

我設法找到一個解決方案,但我不知道它是最好。從我GOOGLE了我的一流服務FCM得到這個代碼:

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