安全异常:不允许启动服务意图act=com.google.android.c2dm.intent.REGISTER。

20

我遇到了错误SecurityException:Not allowed to start service Intent,但在搜索了很多主题后似乎没有好的解决方案。

请帮帮我,

谢谢,

p/s :

我确保在Google API控制台中定义了正确的项目号码和正确的包名

错误:

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.startServiceAsUser(ContextImpl.java:1785)
            at android.app.ContextImpl.startService(ContextImpl.java:1757)
            at android.content.ContextWrapper.startService(ContextWrapper.java:480)
            at com.google.android.gms.gcm.GoogleCloudMessaging.e(Unknown Source)
            at com.google.android.gms.gcm.GoogleCloudMessaging.register(Unknown Source)
            at controller.RegisterGCMClientAsync.doInBackground(RegisterGCMClientAsync.java:62)
            at controller.RegisterGCMClientAsync.doInBackground(RegisterGCMClientAsync.java:18)
            at android.os.AsyncTask$2.call(AsyncTask.java:287)
            at java.util.concurrent.FutureTask.run(FutureTask.java:234)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
            at java.lang.Thread.run(Thread.java:841)

Manifest.xml文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="app.cloudstringers" >

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="app.cloudstringers.Cloudstringers"
        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="ui.fragment.RegisterWithAccountsFragment" />

    <meta-data android:name="com.google.android.gms.version"
               android:value="@integer/google_play_services_version" />

    <permission
        android:name="app.cloudstringers.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <!-- Receiver GCM -->
    <receiver
        android:name="app.cloudstringers.GcmBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="app.cloudstringers" />
        </intent-filter>
    </receiver>

    <!-- Service GCM -->
    <service
        android:exported="true"
        android:name="app.cloudstringers.GcmIntentService" />

    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="app.cloudstringers.permission.C2D_MESSAGE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />

</application>

解决方案 我把代码放错位置了。应该将<use-permission>标签放在<application>标签之外。

2个回答

18

改为这个:

    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="app.cloudstringers.permission.C2D_MESSAGE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
      <permission
        android:name="app.cloudstringers.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="app.cloudstringers.Cloudstringers"
        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="ui.fragment.RegisterWithAccountsFragment" />

    <meta-data android:name="com.google.android.gms.version"
               android:value="@integer/google_play_services_version" />



    <!-- Receiver GCM -->
    <receiver
        android:name="app.cloudstringers.GcmBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="app.cloudstringers" />
        </intent-filter>
    </receiver>

    <!-- Service GCM -->
    <service
        android:exported="true"
        android:name="app.cloudstringers.GcmIntentService" />



</application>

您应该将<use-permission>标签放在<application>标签外面。


6

您应该检查Play商店是否能够工作。 如果不能工作,就无法启动Google服务。


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