安全异常:不允许启动服务意图{act=com.google.android.c2dm.intent.REGISTER pkg=com.google.android.gms(有额外内容)}。

10

我试图从 Google 获取我的 GCM 注册 ID。

我的代码:

String SENDER_ID = "722******53";

/**
 * Registers the application with GCM servers asynchronously.
 * <p>
 * Stores the registration ID and the 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;

                // You should send the registration ID to your server over
                // HTTP, so it
                // can use GCM/HTTP or CCS to send messages to your app.
                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 regID - 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) {
            mDisplay.append(msg + "\n");
        }
    }.execute(null, null, null);
}

我遇到一个错误:
03-01 19:15:36.261: E/AndroidRuntime(3467): FATAL EXCEPTION: AsyncTask #1
03-01 19:15:36.261: E/AndroidRuntime(3467): java.lang.RuntimeException: An error occured while executing doInBackground()
03-01 19:15:36.261: E/AndroidRuntime(3467):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
03-01 19:15:36.261: E/AndroidRuntime(3467):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
03-01 19:15:36.261: E/AndroidRuntime(3467):     at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
03-01 19:15:36.261: E/AndroidRuntime(3467):     at java.util.concurrent.FutureTask.run(FutureTask.java:239)
03-01 19:15:36.261: E/AndroidRuntime(3467):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
03-01 19:15:36.261: E/AndroidRuntime(3467):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
03-01 19:15:36.261: E/AndroidRuntime(3467):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
03-01 19:15:36.261: E/AndroidRuntime(3467):     at java.lang.Thread.run(Thread.java:841)
03-01 19:15:36.261: E/AndroidRuntime(3467): 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
03-01 19:15:36.261: E/AndroidRuntime(3467):     at android.app.ContextImpl.startServiceAsUser(ContextImpl.java:1800)
03-01 19:15:36.261: E/AndroidRuntime(3467):     at android.app.ContextImpl.startService(ContextImpl.java:1772)
03-01 19:15:36.261: E/AndroidRuntime(3467):     at android.content.ContextWrapper.startService(ContextWrapper.java:480)
03-01 19:15:36.261: E/AndroidRuntime(3467):     at com.google.android.gms.gcm.GoogleCloudMessaging.b(Unknown Source)
03-01 19:15:36.261: E/AndroidRuntime(3467):     at com.google.android.gms.gcm.GoogleCloudMessaging.register(Unknown Source)
03-01 19:15:36.261: E/AndroidRuntime(3467):     at com.example.gcm.DemoActivity$1.doInBackground(DemoActivity.java:177)
03-01 19:15:36.261: E/AndroidRuntime(3467):     at com.example.gcm.DemoActivity$1.doInBackground(DemoActivity.java:1)
03-01 19:15:36.261: E/AndroidRuntime(3467):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
03-01 19:15:36.261: E/AndroidRuntime(3467):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
03-01 19:15:36.261: E/AndroidRuntime(3467):     ... 4 more

这是我的清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.manyexampleapp"
    android:installLocation="preferExternal"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="18" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="com.example.manyexampleapp.c2dm.permission.RECEIVE" />
    <uses-permission android:name="com.example.manyexampleapp.gcm.permission.C2D_MESSAGE" />

    <permission
        android:name="com.example.manyexampleapp.gcm.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <application
        android:name="com.zoomer.ifs.BaseApplication"
        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.zoomer.ifs.MainActivity"
            android:label="@string/app_name"
            android:configChanges="orientation|keyboardHidden|screenSize"
                android:launchMode="singleTop">
<!--             <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter> -->
        </activity>

     <!--     PUSH -->

        <!--
          WakefulBroadcastReceiver that will receive intents from GCM
          services and hand them to the custom IntentService.

          The com.google.android.c2dm.permission.SEND permission is necessary
          so only GCM services can send data messages for the app.
        -->
        <receiver
            android:name="com.example.gcm.GcmBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <!-- Receives the actual messages. -->
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="com.example.manyexampleapp" />
            </intent-filter>
        </receiver>
        <service android:name="com.example.gcm.GcmIntentService" />

         <activity
            android:name="com.example.gcm.DemoActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

           <!--     DB -->
        <activity
            android:name="com.example.db.DbActivity"
            android:label="@string/app_name" >
            <intent-filter>
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.http.RestGetActivity"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
        </activity>
        <activity
            android:name="com.example.fb.FacebookLoginActivity"
            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.example.http.SendFeedbackActivity"
            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.zoomer.general.SearchNearbyOffersActivity"
            android:label="@string/app_name" >
            <intent-filter>
            </intent-filter>
        </activity>
        <activity
            android:name="com.facebook.LoginActivity"
            android:label="@string/app_name" >
            <intent-filter>
            </intent-filter>
        </activity>
        <activity android:name="com.example.manyexampleapp.StoresListActivity" >
            <intent-filter>
            </intent-filter>
        </activity>
        <activity android:name="com.example.fb.ShareActivity" >
            <intent-filter>
            </intent-filter>
        </activity>
        <activity android:name="com.example.notifications.NotificationsActivity" >
            <intent-filter>
            </intent-filter>
        </activity>
        <activity android:name="com.example.fb2.no_use.MainActivity" >
            <intent-filter>
            </intent-filter>
        </activity>
        <activity android:name="com.zoomer.offers.OffersListActivity" >

            <!--
                 <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            -->
        </activity>
        <activity android:name="com.example.http.SearchNearbyOffersActivity" >
<!--             <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter> -->
        </activity>

        <service
            android:name="com.example.geo.LocationService"
            android:enabled="true" />

        <receiver android:name="com.example.manyexampleapp.BootReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="com.example.manyexampleapp.LocationService.LOCATION_BROAD_MSG" />
            </intent-filter>
        </receiver>

        <meta-data
            android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/app_id" />
    </application>

</manifest>

请发布您正在使用的代码以获取注册ID。 - Anirudha Agashe
已添加。谢谢。你能看一下吗? - Elad Benda
2个回答

10

更改

<uses-permission android:name="com.example.manyexampleapp.c2dm.permission.RECEIVE" />

to

 <!-- This app has permission to register and receive data message. -->
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

您之所以出现异常,是因为您没有定义所需的权限。


5
我已经在我的应用程序和库项目中都添加了这个权限,但仍然收到此错误提示。 - IgorGanapolsky
@IgorGanapolsky:抱歉回复晚了。希望你现在已经解决了问题。如果没有,请发布你遇到的错误。此外,请尝试使用此处找到的项目https://developer.android.com/google/gcm/client.html,其中包括更详细的说明,以便进行GCM实现所需的操作。 - Anirudha Agashe
12
在一些安卓系统中,如果没有安装Google Play服务,就会遇到这个问题,与员工权限无关。 - Chine Gary
2
如果您正在使用Play服务插件,则uses-permission条目将自动添加到清单中。要检查这一点,您可以在Android Studio中使用“文件>配置文件或调试APK...”并检查清单。无需手动添加uses-permission条目。 - Mark

2
如果应用程序开发后安装了播放服务,那么可能会发生com.google.android.c2dm.permission.RECEIVE权限已被授予但Android仍然报错的情况。
在这种情况下,您必须完全重新安装开发的应用程序,以使此权限起作用。

你确定这只适用于 Marshmallow 之前的设备吗? - Mark

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接