无法获得GCM注册ID客户端android

时间:2014-05-06 14:57:45

标签: java android java-ee google-cloud-messaging

我无法获得所有设备的注册ID。 清单很好,我加上所有预先准备。 Sender_ID很好。 该设备已登录帐户。(Google帐户)。 下面的代码是处理GCM设备寄存器的活动。 请帮帮我!

public class ClientRegisterActivity extends Activity {
private static final String SENDER_ID = "*******";

private Client client = null;
  private GoogleCloudMessaging gcm;
 TextView tvHeader;
 EditText etEmail;
 EditText etPass;
 EditText etPassConf;
 EditText etFname;
 EditText etLname;
 EditText etPhone;
// Spinner spWorkArea;
Button bReg;

// Variables used for conversion into database
String fName;
String lName;
String email;
String password;
String phone;
// String workArea;
String regId = "";
String rg;
MyGCMBroadcastReceiver myReci;
// SQLLiteData entry = new SQLLiteData(RegisterActivity.this);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.client_activity_register);
    initialize();
}

private  void initialize() {
    tvHeader = (TextView) findViewById(R.id.ctvHeader);
    etEmail = (EditText) findViewById(R.id.cemail);
    etPass = (EditText) findViewById(R.id.cpassword);
    etPassConf = (EditText) findViewById(R.id.cpassword_confirm);
    etFname = (EditText) findViewById(R.id.cfirst_name);
    etLname = (EditText) findViewById(R.id.clast_name);
    etPhone = (EditText) findViewById(R.id.cetPhone);
    // spWorkArea = (Spinner) findViewById(R.id.cspWorkArea);
    bReg = (Button) findViewById(R.id.cbRegister);
    regId="";
    // Avoiding NullPointerException in checks
    etEmail.setText("");
    etPass.setText("");
    etPassConf.setText("");
    etFname.setText("");
    etLname.setText("");
    etPhone.setText("");
    myReci=new MyGCMBroadcastReceiver();
    registerReceiver(myReci,new IntentFilter("com.avshalom.ilani.easymovers.DISPLAY_MESSAGE_ACTION"));
    bReg.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            boolean didItWork;


            registerInBackground();


            if (everythingOK()) {
                // fName, lName, email, password have been updated in check
                // function
                // Write details in DB and continue to main screen / show
                // confirmation message
                if (!regId.equals(null))
                    didItWork = true;
                // not null ?

            }

            else
                setContentView(R.layout.client_activity_register); 

            Log.d("Text", "kobi: regId = " + regId);
            Gson gson = new Gson();
            client = new Client();
            SendHttpRequest snd = new SendHttpRequest((etFname.getText()
                    .toString()), etLname.getText().toString(), etEmail
                    .getText().toString(), etPass.getText().toString(),
                    etPhone.getText().toString(), regId.toString());
            snd.start();
            // HttpResponse
            // response=SendHttpRequest.SendLogin(etFname.getText().toString(),etLname.getText().toString(),etEmail.getText().toString(),etPass.getText().toString(),etPhone.getText().toString(),
            // regId.toString());
            Log.d("Text", "kobi: after httpresponse");

            // client = gson.fromJson(reader, Client.class);
            Toast.makeText(ClientRegisterActivity.this,
                    client.getFirstName(), Toast.LENGTH_SHORT).show();

        }

    });


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    // getMenuInflater().inflate(R.menu.register, menu);
    return true;
}


 private void registerInBackground() {
        new AsyncTask<Void, Void, String>() {


            @Override
            protected String doInBackground(Void... params) {
                String msg = "";
                try {
                    if (gcm == null) {
                        gcm = GoogleCloudMessaging.getInstance(getApplicationContext());
                    }
                 //  while(regId=="")
                    rg = gcm.register(SENDER_ID);
                    regId=rg;

                    msg = "Device registered, registration ID=" + regId;
                    System.out.println(msg);
                    Log.d("Text", msg);
                    Log.d("Text", "kobi: " + regId);

                } catch (IOException ex) {
                    msg = "Error :" + ex.getMessage();

                }
                return msg;
            }

            @Override
            protected void onPostExecute(String msg) {

            }
        }.execute(null, null, null);
    }

在onpreexecute之后注册活动:

bReg.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //boolean didItWork;


            registerInBackground();

            /*
            if (everythingOK()) {
                // fName, lName, email, password have been updated in check
                // function
                // Write details in DB and continue to main screen / show
                // confirmation message
                if (!regId.equals(null))
                    didItWork = true;
                // not null ?

            }

            else
                setContentView(R.layout.client_activity_register); */

            Log.d("Text", "kobi: regId = " + regId);
            Gson gson = new Gson();
            client = new Client();
            SendHttpRequest snd = new SendHttpRequest((etFname.getText()
                    .toString()), etLname.getText().toString(), etEmail
                    .getText().toString(), etPass.getText().toString(),
                    etPhone.getText().toString(), regId.toString());
            snd.start();
            // HttpResponse
            // response=SendHttpRequest.SendLogin(etFname.getText().toString(),etLname.getText().toString(),etEmail.getText().toString(),etPass.getText().toString(),etPhone.getText().toString(),
            // regId.toString());
            Log.d("Text", "kobi: after httpresponse");

            // client = gson.fromJson(reader, Client.class);
            Toast.makeText(ClientRegisterActivity.this,
                    client.getFirstName(), Toast.LENGTH_SHORT).show();

        }

    });


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    // getMenuInflater().inflate(R.menu.register, menu);
    return true;
}


 private void registerInBackground() {
        new AsyncTask<Void, Void, String>() {


            @Override
            protected String doInBackground(Void... params) {
                String msg = "";
                try {
                    if (gcm == null) {
                        gcm = GoogleCloudMessaging.getInstance(getApplicationContext());
                    }
                 //  while(regId=="")
                    rg = gcm.register(SENDER_ID);
                    regId=rg;

                    msg = "Device registered, registration ID=" + regId;
                    System.out.println(msg);
                    Log.d("Text", msg);
                    Log.d("Text", "kobi: " + regId);

                } catch (IOException ex) {
                    msg = "Error :" + ex.getMessage();

                }
                return msg;
            }

            @Override
            protected void onPostExecute(String msg) {
                boolean didItWork;
                if (everythingOK()) {
                    // fName, lName, email, password have been updated in check
                    // function
                    // Write details in DB and continue to main screen / show
                    // confirmation message
                    if (!regId.equals(null))
                        didItWork = true;
                    // not null ?
                    else
                        setContentView(R.layout.client_activity_register);

                }
            }
        }.execute(null, null, null);
    }

这是清单文件:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.avshalom.ilani.easymovers"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="17" />


    <!-- For receiving messaged from the GCM -->
    <permission
        android:name="com.avshalom.ilani.easymovers.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.avshalom.ilani.easymovers.permission.C2D_MESSAGE" />
    <!-- For receiving messaged from the GCM -->


    <!-- For receiving the GCM messages -->
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <!-- For accessing the internet -->
    <uses-permission android:name="android.permission.INTERNET" />
    <!-- For google accounts -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />

    <!-- When receiving a message, to keep the device awake -->
    <uses-permission android:name="android.permission.WAKE_LOCK" />


    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.avshalom.ilani.easymovers.Login"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
         <activity
            android:name="com.avshalom.ilani.easymovers.Choose"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

         <activity
            android:name="com.avshalom.ilani.easymovers.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

         <activity
            android:name="com.avshalom.ilani.easymovers.ClientRegisterActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.avshalom.ilani.easymovers.offers.tabs"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
         <activity
            android:name="com.avshalom.ilani.easymovers.MoverRegisterActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

         <receiver
            android:name="com.avshalom.ilani.easymovers.MyGCMBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

                <category android:name="com.avshalom.ilani.easymovers" />
            </intent-filter>
        </receiver>

         <service android:name="com.avshalom.ilani.easymovers.GCMIntentService" />


    </application>

</manifest>

0 个答案:

没有答案
相关问题