GCM使用Thread工作但不能使用AsyncTask

时间:2016-05-22 04:18:28

标签: java android android-studio android-asynctask google-cloud-messaging

我是Android开发的新手

我正在使用名为GCMHelper的类来获取GCM代码

在我的登录界面中,我曾经通过Thread调用此类,但它只能在代码末尾运行,不允许我获取GCM代码并使用它

很多人建议AsyncTask是正确的做法,因为它会在被调用时立即执行

我试过了,但在Thread中使用的完全相同的方法在AsyncTask中不起作用,我得到" SERVICE_NOT_AVAILABLE "例外

我想知道为什么以及如何解决这个问题?

这是我的代码

private void LogMeIn(String LogReg, String EncEmail, String EncPwd, String EncUserID, String EncUserName) throws ExecutionException, InterruptedException {


    String gcmRegID = "";

    gcmRegID = new GCM().execute(null,null,null).get(); //----  AsyncTask

    GetGCM(); //----- Thread
  }



private void GetGCM() {
    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                GCMHelper gcmRegistrationHelper = new GCMHelper(
                        getApplicationContext());
                String gcmRegID = "";
                gcmRegID = gcmRegistrationHelper.GCMRegister("12345678");
                gcmRegID = gcmRegID; // -------- Here I get code.. OK..
            } catch (Exception bug) {
                bug.printStackTrace();
            }
        }
    });

    thread.start();
}



private class GCM extends AsyncTask<String, String, String> {

    private String resp;
    private Context context;
    //private ProgressDialog dialog;
    //private List<Message> messages;


    @Override
    protected String  doInBackground(String... params) {
        GCMHelper  gcmRegistrationHelper = new GCMHelper (
                getApplicationContext());
        String gcmRegID = "";
        try {
            gcmRegID = gcmRegistrationHelper.GCMRegister("12345678");
             //----------- Here is exception SERVICE_NOT_AVAILABLE
        } catch (Exception e) {
            e.printStackTrace();
        }

        return gcmRegID;
    }



}

这是我的GCMHelper类

package com.asmgx.schlogger.app;

import android.content.Context;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.gcm.GoogleCloudMessaging;

/**
 * Created by ASMGX on 15/05/2016.
 */
public final  class GCMHelper {


    static GoogleCloudMessaging gcm = null;

    static Context context= null;

    public GCMHelper (Context context)
    {
        this.context= context;
    }

    public String GCMRegister (String SENDER_ID) throws Exception
    {
        String regid = "";
        //Check if Play store services are available.
        if(!checkPlayServices())
            throw new Exception("Google Play Services not supported. Please install and configure Google Play Store.");

        if (gcm == null) {
            gcm = GoogleCloudMessaging.getInstance(context);
        }
        regid = gcm.register(SENDER_ID);

        return regid;
    }


    private static boolean checkPlayServices() {
        int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(context);
        if (resultCode != ConnectionResult.SUCCESS) {
            return false;
        }
        return true;
    }

}

0 个答案:

没有答案