我正在尝试实施 GCM。
我按照谷歌的规定实现了所有。当我注册设备时,接收器中的回调没有效果。GCMIntentService 类中甚至没有 onError() 。
GCMIntentService 也从未构造过。
这是我的代码....
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.roconmachine.gcm"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<permission android:name="com.roconmachine.gcm.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.roconmachine.gcm.permission.C2D_MESSAGE" />
<!-- App receives GCM messages. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- GCM connects to Google Services. -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".NewGCMActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.google.android.gcm.GCMBroadcastReceiver" 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" />
</intent-filter>
</receiver>
<service android:name=".services.GCMIntentService" android:enabled="true"/>
</application>
</manifest>
这里是
/*
* Copyright 2012 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.roconmachine.gcm.services;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
import com.google.android.gcm.GCMBaseIntentService;
/**
* {@link IntentService} responsible for handling GCM messages.
*/
public class GCMIntentService extends GCMBaseIntentService {
public GCMIntentService() {
super("873601972999");
}
@Override
protected void onRegistered(Context context, String registrationId) {
Toast.makeText(this, "onRegistered", Toast.LENGTH_LONG).show();
}
@Override
protected void onUnregistered(Context context, String registrationId) {
Toast.makeText(this, "onUnregistered", Toast.LENGTH_LONG).show();
}
@Override
protected void onMessage(Context context, Intent intent) {
Toast.makeText(this, "onMessage", Toast.LENGTH_LONG).show();
}
@Override
protected void onDeletedMessages(Context context, int total) {
Toast.makeText(this, "onDeletedMessages", Toast.LENGTH_LONG).show();
}
@Override
public void onError(Context context, String errorId) {
Toast.makeText(this, "onError", Toast.LENGTH_LONG).show();
}
@Override
protected boolean onRecoverableError(Context context, String errorId) {
Toast.makeText(this, "onRecoverableError", Toast.LENGTH_LONG).show();
return super.onRecoverableError(context, errorId);
}
}
package com.roconmachine.gcm;
import com.google.android.gcm.GCMRegistrar;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class NewGCMActivity extends Activity {
private Button btnGetRegister;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnGetRegister = (Button)findViewById(R.id.button1);
btnGetRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
register();
}
});
}
private void register()
{
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
if (GCMRegistrar.getRegistrationId(this).equals("")) {
GCMRegistrar.register(this, "873601972999");
} else
{
Toast.makeText(this, "already register", Toast.LENGTH_LONG).show();
}
}
}