SecurityException:不允许启动服务Intent {act = com.google.android.c2dm.intent.REGISTER pkg = com.google.android.gms(has extras)}

 DOOD眷顾 发布于 2023-01-20 15:54

我尝试从Google获取GCM registrationId.

我的代码:

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

/**
 * Registers the application with GCM servers asynchronously.
 * 

* Stores the registration ID and the app versionCode in the application's * shared preferences. */ private void registerInBackground() { new AsyncTask() { @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

这是我的清单:




    

    
    
    
    
    
    
    
    

    

    
        


         

        

     

        
        
            
                
                
                
            
        
        

         
            
                
                
            
        

           
        
            
            
        
        
        
        

            
        
        

            
        
        
            
            
        
        
            
            
        
        
            
            
        
        
            
            
        
        
            
            
        
        
            
            
        
        

            
        
        

        

        

        
            
                
                
            
        

        
    


Anirudha Aga.. 8

更改


 
    

您正在获取异常,因为您尚未定义所需的权限

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有