简体   繁体   English

当通过Intent手动调用广播接收器时,应用程序崩溃

[英]Application crashes when broadcast receiver is being called manually via Intent

My AndroidManifest.xml File 我的AndroidManifest.xml文件

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


<receiver
            android:name=".MyReceiver"
            android:enabled="true"
            android:exported="true"
            android:permission="android.permission.RECEIVE_SMS">
            <intent-filter>
                <action
                    android:name = "android.provider.Telephony.SMS_RECEIVED" >
                </action>
            </intent-filter>

</receiver>



<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.RECEIVE_SMS"> </uses-permission>

My SignIn Class 我的SignIn类

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sign_in);

        next = findViewById(R.id.button);
        next.setOnClickListener(this);

        if(ContextCompat.checkSelfPermission(this, Manifest.permission.RECEIVE_SMS)!= PackageManager.PERMISSION_GRANTED){

                        if(ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.RECEIVE_SMS)){

                        }else{
                            ActivityCompat.requestPermissions(this,new String[] {Manifest.permission.RECEIVE_SMS},MY_PERMISSION_REQUEST_RECEIVE_SMS);
                        }

                    }

}

@Override
    public void onRequestPermissionsResult(int requestCode,String permissions[],int[] grantResults){
        switch (requestCode){
            case MY_PERMISSION_REQUEST_RECEIVE_SMS:{
                if(grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
                    Toast.makeText(getApplicationContext(),"Thanks for permitting",Toast.LENGTH_LONG).show();
                }else {
                    Toast.makeText(getApplicationContext(),"No permission",Toast.LENGTH_LONG).show();
                }
            }
        }
    }

    @Override
        public void onClick(View v) {

                 Intent intent = new Intent();                          
                 intent.setAction("android.provider.Telephony.SMS_RECEIVED");
                 sendBroadcast(intent);
    }

My MyReceiver Class 我的MyReceiver类

package com.example.gofresh;
//all imports
public class MyReceiver extends BroadcastReceiver {

         private static final String TAG = "SmsBroadcastReceiver";

        @Override
            public void onReceive(Context context, Intent intent) {

                    Log.i(TAG,"Intent Received: " +intent.getAction());
        }
    }

My Log 我的日志

019-06-10 11:19:39.063 14497-14497/com.example.gofresh D/AndroidRuntime: Shutting down VM
2019-06-10 11:19:39.067 14497-14497/com.example.gofresh E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.gofresh, PID: 14497
    java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.provider.Telephony.SMS_RECEIVED from pid=14497, uid=10184
        at android.os.Parcel.createException(Parcel.java:1950)
        at android.os.Parcel.readException(Parcel.java:1918)
        at android.os.Parcel.readException(Parcel.java:1868)
        at android.app.IActivityManager$Stub$Proxy.broadcastIntent(IActivityManager.java:3842)
        at android.app.ContextImpl.sendBroadcast(ContextImpl.java:1012)
        at android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:444)
        at com.example.gofresh.SignIn.onClick(SignIn.java:122)
        at android.view.View.performClick(View.java:6669)
        at android.view.View.performClickInternal(View.java:6638)
        at android.view.View.access$3100(View.java:789)
        at android.view.View$PerformClick.run(View.java:26145)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6863)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: android.os.RemoteException: Remote stack trace:
        at com.android.server.am.ActivityManagerService.broadcastIntentLocked(ActivityManagerService.java:23397)
        at com.android.server.am.ActivityManagerService.broadcastIntent(ActivityManagerService.java:24071)
        at android.app.IActivityManager$Stub.onTransact$broadcastIntent$(IActivityManager.java:10250)
        at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:167)
        at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:3820)
2019-06-10 11:19:39.088 14497-14569/com.example.gofresh D/OSTracker: OS Event: crash
2019-06-10 11:19:39.104 14497-14497/com.example.gofresh I/Process: Sending signal. PID: 14497 SIG: 9

On opening the application, i am asked for the permission and it is granted and the Toast Mesasage also pops up. 在打开应用程序时,我被要求获得许可,并且它被授予并且Toast Mesasage也会弹出。 However when i click on my button, the application crashes. 但是当我点击我的按钮时,应用程序崩溃了。 I have cut only those part of code where i am calling the Broadcast Receiver. 我只删除了我调用广播接收器的那部分代码。 Even if I comment out the manual intent part in the onClick method in SignIn class, it still shows in the MyReceiver class that i have called the onReceive method, even though i have called it nowhere else in my code. 即使我在SignIn类的onClick方法中注释掉手动意图部分,它仍然在MyReceiver类中显示我已经调用onReceive方法,即使我在我的代码中没有其他地方调用它。 I feel that wherever i am going wrong is strongly connected to my misunderstanding of this part. 我觉得,无论我哪里出错,都与我对这部分的误解密切相关。

you must add permission Sms add following permission to manifest tag in manifest file in app level 您必须添加权限Sms将以下权限添加到应用程序级别的清单文件中的清单标记

 <uses-permission android:name="android.permission.RECEIVE_SMS"/>

if you Add this permission in your manifest now notice to targetSDKVersion in gradle file in app level if it is larger than 22 you must give permission runtime 如果您在清单中添加此权限,请注意应用程序级别的gradle文件中的targetSDKVersion,如果它大于22,则必须提供权限运行时

When you send a broadcast It will be received by every application on the phone. 发送广播时,手机上的每个应用程序都会收到广播。 except In case of LocalBroadcast. 除了LocalBroadcast。 Now you are sending the system broadcast "android.provider.Telephony.SMS_RECEIVED" that can be register by many other application. 现在您发送的系统广播“android.provider.Telephony.SMS_RECEIVED”可以被许多其他应用程序注册。 As a security this broadcast is only sent by SYSTEM. 作为安全,此广播仅由SYSTEM发送。 I hope you will understand the reason of the crash. 我希望你能理解坠机的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM