我正在尝试按照developer.android.com/google/gcm选择GCM,但最终会出现以下错误:
11-05 08:12:53.147:E / AndroidRuntime(1873):致命异常:AsyncTask#1 11-05 08:12:53.147:E / AndroidRuntime(1873):进程:com.example.gcmtest00,PID:1873 11-05 08:12:53.147:E / AndroidRuntime(1873):java.lang.RuntimeException:执行doInBackground()时发生错误 11-05 08:12:53.147:E / AndroidRuntime(1873):在android.os.AsyncTask $ 3.done(AsyncTask.java:300) 11-05 08:12:53.147:E / AndroidRuntime(1873):at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355) 11-05 08:12:53.147:E / AndroidRuntime(1873):at java.util.concurrent.FutureTask.setException(FutureTask.java:222) 11-05 08:12:53.147:E / AndroidRuntime(1873):at java.util.concurrent.FutureTask.run(FutureTask.java:242) 11-05 08:12:53.147:E / AndroidRuntime(1873):在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231) 11-05 08:12:53.147:E / AndroidRuntime(1873):at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 11-05 08:12:53.147:E / AndroidRuntime(1873):at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:587) 11-05 08:12:53.147:E / AndroidRuntime(1873):at java.lang.Thread.run(Thread.java:841) 11-05 08:12:53.147:E / AndroidRuntime(1873):引起:java.lang.SecurityException:不允许启动服务Intent {act = com.google.android.c2dm.intent.REGISTER pkg = com.google .android.gms(有附加内容)}未经许可com.google.android.c2dm.permission.RECEIVE 11-05 08:12:53.147:E / AndroidRuntime(1873):在android.app.ContextImpl.startServiceCommon(ContextImpl.java:1503) 11-05 08:12:53.147:E / AndroidRuntime(1873):在android.app.ContextImpl.startService(ContextImpl.java:1480) 11-05 08:12:53.147:E / AndroidRuntime(1873):在android.content.ContextWrapper.startService(ContextWrapper.java:494) 11-05 08:12:53.147:E / AndroidRuntime(1873):at com.google.android.gms.gcm.GoogleCloudMessaging.c(Unknown Source) 11-05 08:12:53.147:E / AndroidRuntime(1873):at com.google.android.gms.gcm.GoogleCloudMessaging.register(Unknown Source) 11-05 08:12:53.147:E / AndroidRuntime(1873):at com.example.gcmtest00.MainActivity $ getRegId.doInBackground(MainActivity.java:53) 11-05 08:12:53.147:E / AndroidRuntime(1873):at com.example.gcmtest00.MainActivity $ getRegId.doInBackground(MainActivity.java:1) 11-05 08:12:53.147:E / AndroidRuntime(1873):在android.os.AsyncTask $ 2.call(AsyncTask.java:288) 11-05 08:12:53.147:E / AndroidRuntime(1873):at java.util.concurrent.FutureTask.run(FutureTask.java:237) 11-05 08:12:53.147:E / AndroidRuntime(1873):... 4更多
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gcmtest00"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="com.example.gcmtest00.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.gcmtest00.permission.C2D_MESSAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>
<activity
android:name="com.example.gcmtest00.MainActivity"
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="gcm.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.example.gcmtest00" />
</intent-filter>
</receiver>
<service android:name=".GcmMessageHandler" />
</application>
MainActivity.java
package com.example.gcmtest00;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.gcm.GoogleCloudMessaging;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity{
public static final String EXTRA_MESSAGE = "message";
public static final String PROPERTY_REG_ID = "registration_id";
private static final String PROPERTY_APP_VERSION = "appVersion";
private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
Button btnRegId;
TextView etRegId;
GoogleCloudMessaging gcm=null;
String regid;
String SENDER_ID = "186386566618"; //project number
Context context;
AtomicInteger msgId = new AtomicInteger();
SharedPreferences prefs;
static final String TAG = "GCMDemo";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = getApplicationContext();
btnRegId = (Button) findViewById(R.id.btnGetRegId);
etRegId = (TextView) findViewById(R.id.etRegId);
if (checkPlayServices()) {
gcm = GoogleCloudMessaging.getInstance(this);
regid = getRegistrationId(context);
if (regid.isEmpty()) {
registerInBackground();
}
}else {
Log.i(TAG, "No valid Google Play Services APK found.");
}
}
private String getRegistrationId(Context context) {
final SharedPreferences prefs = getGCMPreferences(context);
String registrationId = prefs.getString(PROPERTY_REG_ID, "");
if (registrationId.isEmpty()) {
Log.i(TAG, "Registration not found.");
return "";
}
int registeredVersion = prefs.getInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE);
int currentVersion = getAppVersion(context);
if (registeredVersion != currentVersion) {
Log.i(TAG, "App version changed.");
return "";
}
return registrationId;
}
private SharedPreferences getGCMPreferences(Context context) {
return getSharedPreferences(MainActivity.class.getSimpleName(),
Context.MODE_PRIVATE);
}
private static int getAppVersion(Context context) {
try {
PackageInfo packageInfo = context.getPackageManager()
.getPackageInfo(context.getPackageName(), 0);
return packageInfo.versionCode;
} catch (NameNotFoundException e) {
throw new RuntimeException("Could not get package name: " + e);
}
}
@Override
protected void onResume() {
super.onResume();
checkPlayServices();
}
private boolean checkPlayServices() {
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (resultCode != ConnectionResult.SUCCESS) {
if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
GooglePlayServicesUtil.getErrorDialog(resultCode, this,
PLAY_SERVICES_RESOLUTION_REQUEST).show();
} else {
Log.i(TAG, "This device is not supported.");
finish();
}
return false;
}
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(context);
}
regid = gcm.register(SENDER_ID);
msg = "Device registered, registration ID=" + regid;
sendRegistrationIdToBackend();
storeRegistrationId(context, regid);
} catch (IOException ex) {
msg = "Error :" + ex.getMessage();
}
return msg;
}
@Override
protected void onPostExecute(String msg) {
etRegId.append(msg + "\n");
}
}.execute();
}
private void sendRegistrationIdToBackend() {
}
private void storeRegistrationId(Context context, String regId) {
final SharedPreferences prefs = getGCMPreferences(context);
int appVersion = getAppVersion(context);
Log.i(TAG, "Saving regId on app version " + appVersion);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(PROPERTY_REG_ID, regId);
editor.putInt(PROPERTY_APP_VERSION, appVersion);
editor.commit();
}
}
答案 0 :(得分:1)
只需添加:
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
的intent-filter
标记内的 receiver