使用Google Play服务时出现ConcurrentModificationException

普拉巴卡

我是android编程的新手。我正在尝试开发简单的gcm测试应用程序以在gcm服务器上注册该应用程序。该代码是

public class MainActivity extends ActionBarActivity implements OnClickListener {

Button btnRegId;
Context context;
EditText etRegId;
GoogleCloudMessaging gcm;
AtomicInteger msgId = new AtomicInteger();
String regid;
public static final String EXTRA_MESSAGE = "message";
public static final String PROPERTY_REG_ID = "registration_id";
private static final String PROPERTY_APP_VERSION = "appVersion";


String SENDER_ID = "507707796382";
private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
static final String TAG = "GCM";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    context = getApplicationContext();

    btnRegId = (Button) findViewById(R.id.btnGetRegId);
    etRegId = (EditText) findViewById(R.id.etRegId);
    if (checkPlayServices()) {
        btnRegId.setOnClickListener(this);
        gcm = GoogleCloudMessaging.getInstance(this);
        regid = getRegistrationId(context);

        if (regid.isEmpty()) {
            registerInBackground();
        }
    } else {
        Log.i(TAG, "No valid Google Play Services APK found.");
        Toast.makeText(getApplicationContext(), "No valid Google Play Services APK found.", Toast.LENGTH_LONG).show();
    }
}




/**
 * Gets the current registration ID for application on GCM service.
 *
 * If result is empty, the app needs to register.
 *
 * @return registration ID, or empty string if there is no existing
 *         registration ID.
 */
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 "";
    }
    // Check if app was updated; if so, it must clear the registration ID
    // since the existing registration ID is not guaranteed to work with
    // the new app version.
    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;
}

/**
 * @return Application's {@code SharedPreferences}.
 */
private SharedPreferences getGCMPreferences(Context context) {
    // This sample app persists the registration ID in shared preferences, but
    // how you store the registration ID in your app is up to you.
    return getSharedPreferences(MainActivity.class.getSimpleName(),
            Context.MODE_PRIVATE);
}

/**
 * @return Application's version code from the {@code PackageManager}.
 */
private static int getAppVersion(Context context) {
    try {
        PackageInfo packageInfo = context.getPackageManager()
                .getPackageInfo(context.getPackageName(), 0);
        return packageInfo.versionCode;
    } catch (PackageManager.NameNotFoundException e) {
        // should never happen
        throw new RuntimeException("Could not get package name: " + e);
    }
}

@Override
public void onClick(View view) {

}

@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("GCM", "This device is not supported.");
            finish();
        }
        return false;
    }

    return true;
}

/**
 * Registers the application with GCM servers asynchronously.
 *
 * Stores the registration ID and app versionCode in the application's
 * shared preferences.
 */
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;

                // send the registration ID to server over HTTP,
                // so it can use GCM/HTTP or CCS to send messages to app.
                // The request to server should be authenticated if  app
                // is using accounts.
                sendRegistrationIdToBackend();

                // For this demo: we don't need to send it because the device
                // will send upstream messages to a server that echo back the
                // message using the 'from' address in the message.

                // Persist the registration ID - no need to register again.
                storeRegistrationId(context, regid);
            } catch (IOException ex) {
                msg = "Error :" + ex.getMessage();
                // If there is an error, don't just keep trying to register.
                // Require the user to click a button again, or perform
                // exponential back-off.
            }
            return msg;
        }
        @Override
        protected void onPostExecute(String msg) {
            etRegId.setText(msg + "\n");

        }

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

/**
 * Sends the registration ID to your server over HTTP, so it can use GCM/HTTP
 * or CCS to send messages to your app. Not needed for this demo since the
 * device sends upstream messages to a server that echoes back the message
 * using the 'from' address in the message.
 */
private void sendRegistrationIdToBackend() {
    //  implementation .
}

/**
 * Stores the registration ID and app versionCode in the application's
 * {@code SharedPreferences}.
 *
 * @param context application's context.
 * @param regId registration ID
 */
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();
}

}

执行此操作时出现错误。我的logcat窗口显示:

03-15 09:48:58.052    2686-2702/com.example.bhaskar.mygcm E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
Process: com.example.bhaskar.mygcm, PID: 2686
java.lang.RuntimeException: An error occured while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:300)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
        at java.util.concurrent.FutureTask.run(FutureTask.java:242)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
        at java.lang.Thread.run(Thread.java:818)
 Caused by: java.lang.SecurityException: Not allowed to start service Intent { act=com.google.android.c2dm.intent.REGISTER pkg=com.google.android.gms (has extras) } without permission com.google.android.c2dm.permission.RECEIVE
        at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1710)
        at android.app.ContextImpl.startService(ContextImpl.java:1687)
        at android.content.ContextWrapper.startService(ContextWrapper.java:515)
        at com.google.android.gms.gcm.GoogleCloudMessaging.e(Unknown Source)
        at com.google.android.gms.gcm.GoogleCloudMessaging.register(Unknown Source)
        at com.example.bhaskar.mygcm.MainActivity$1.doInBackground(MainActivity.java:158)
        at com.example.bhaskar.mygcm.MainActivity$1.doInBackground(MainActivity.java:150)
        at android.os.AsyncTask$2.call(AsyncTask.java:288)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)    
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)   
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
        at java.lang.Thread.run(Thread.java:818)

在158号我有;

regid = gcm.register(SENDER_ID);

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bhaskar.mygcm" >

<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.C2D_RECEIVE"/>

<permission android:name="com.google.android.c2dm.permission.C2D_MESSAGE" android:protectionLevel="signature"/>
<uses-permission android:name="com.google.android.c2dm.permission.C2D_MESSAGE"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <receiver android:name=".GcmBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.C2D_SEND">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE"/>
            <category android:name="com.example.bhaskar.mygcm"/>
        </intent-filter>

            </receiver>
    <service android:name=".GcmMessageHandler"/>



    <activity
        android:name=".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>
    <meta-data android:name="com.google.android.gms.version"
               android:value="@integer/google_play_services_version"/>
</application>
</manifest>

任何人都可以帮助我找出错误的地方,任何帮助将不胜感激。

n1ckname152

您是否添加了

<uses-permission android:name=" com.google.android.c2dm.permission.RECEIVE " /> 
 <uses-permission android:name="com.google.android.c2dm.permission.C2D_MESSAGE">

在你的清单上?日志猫说不。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用Google Play服务库时出现IllegalStateException

来自分类Dev

连接到Google Play服务时出现内部错误

来自分类Dev

从Google Play服务加载MapFragment时出现致命信号11

来自分类Dev

NoSuchMethodError:使用Froyo版本的Google Play服务时出现java.io.IOException。<init>异常

来自分类Dev

使用Google Play服务时出错

来自分类Dev

使用Google Play服务时出错

来自分类Dev

在RemotePlaybackClient上调用play()时,Google Play服务中出现NullPointerException

来自分类Dev

Google Play服务LocationClient的java.util.ConcurrentModificationException

来自分类Dev

更新到Google Play服务9.0.1 Android后,每次启动时都会出现ANR

来自分类Dev

登录/重试失败时出现Google Play游戏服务怪异现象(活动生命周期)

来自分类Dev

添加Google Play服务时出现了意外的最高水平例外情况

来自分类Dev

尝试访问Google Play服务Android时出现PLACES_API_INVALID_APP错误

来自分类Dev

更新到Google Play服务9.0.1 Android后,每次启动时都会出现ANR

来自分类Dev

Google Play服务9.2.0(Admob)出现NoClassDefFoundError

来自分类Dev

尝试使用Google+登录时无法使用Google Play服务

来自分类Dev

更新Google Play服务时出错

来自分类Dev

使用Google Play服务进行访问时Google Drive API的配额是多少?

来自分类Dev

使用Google移动广告时是否需要检查和验证Google Play服务?

来自分类Dev

Google Play 服务未出现在 Unity 中

来自分类Dev

使用广告-Google Play服务与Android SDK

来自分类Dev

使用 Web 服务时出现 UnKnowHost 异常

来自分类Dev

使用“ Google Play游戏服务”进行游戏时突然断开连接

来自分类Dev

使用Google Play服务构建Xamarin.Android项目时出错

来自分类Dev

使用Google Play服务时如何避免65k方法限制

来自分类Dev

将Google Play服务添加为依赖项时,无法使用gradle构建

来自分类Dev

使用Google Play服务库在我的应用中实施广告时出错

来自分类Dev

将Google Play服务添加为依赖项时,无法使用gradle构建

来自分类Dev

在Google Play服务9.6.1中使用最新的Smooch版本(4.0.5)时,应用崩溃

来自分类Dev

在Google Drive API中使用服务帐户下载文件时出现未经授权的响应

Related 相关文章

  1. 1

    使用Google Play服务库时出现IllegalStateException

  2. 2

    连接到Google Play服务时出现内部错误

  3. 3

    从Google Play服务加载MapFragment时出现致命信号11

  4. 4

    NoSuchMethodError:使用Froyo版本的Google Play服务时出现java.io.IOException。<init>异常

  5. 5

    使用Google Play服务时出错

  6. 6

    使用Google Play服务时出错

  7. 7

    在RemotePlaybackClient上调用play()时,Google Play服务中出现NullPointerException

  8. 8

    Google Play服务LocationClient的java.util.ConcurrentModificationException

  9. 9

    更新到Google Play服务9.0.1 Android后,每次启动时都会出现ANR

  10. 10

    登录/重试失败时出现Google Play游戏服务怪异现象(活动生命周期)

  11. 11

    添加Google Play服务时出现了意外的最高水平例外情况

  12. 12

    尝试访问Google Play服务Android时出现PLACES_API_INVALID_APP错误

  13. 13

    更新到Google Play服务9.0.1 Android后,每次启动时都会出现ANR

  14. 14

    Google Play服务9.2.0(Admob)出现NoClassDefFoundError

  15. 15

    尝试使用Google+登录时无法使用Google Play服务

  16. 16

    更新Google Play服务时出错

  17. 17

    使用Google Play服务进行访问时Google Drive API的配额是多少?

  18. 18

    使用Google移动广告时是否需要检查和验证Google Play服务?

  19. 19

    Google Play 服务未出现在 Unity 中

  20. 20

    使用广告-Google Play服务与Android SDK

  21. 21

    使用 Web 服务时出现 UnKnowHost 异常

  22. 22

    使用“ Google Play游戏服务”进行游戏时突然断开连接

  23. 23

    使用Google Play服务构建Xamarin.Android项目时出错

  24. 24

    使用Google Play服务时如何避免65k方法限制

  25. 25

    将Google Play服务添加为依赖项时,无法使用gradle构建

  26. 26

    使用Google Play服务库在我的应用中实施广告时出错

  27. 27

    将Google Play服务添加为依赖项时,无法使用gradle构建

  28. 28

    在Google Play服务9.6.1中使用最新的Smooch版本(4.0.5)时,应用崩溃

  29. 29

    在Google Drive API中使用服务帐户下载文件时出现未经授权的响应

热门标签

归档