0

My AndroidManifest.xml File

<?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

@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

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. 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. I feel that wherever i am going wrong is strongly connected to my misunderstanding of this part.

Zoe
  • 27,060
  • 21
  • 118
  • 148
CS Learner
  • 231
  • 2
  • 11

2 Answers2

0

you must add permission Sms add following permission to manifest tag in manifest file in app level

 <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

shahram_javadi
  • 257
  • 1
  • 12
0

When you send a broadcast It will be received by every application on the phone. except In case of LocalBroadcast. Now you are sending the system broadcast "android.provider.Telephony.SMS_RECEIVED" that can be register by many other application. As a security this broadcast is only sent by SYSTEM. I hope you will understand the reason of the crash.

Nitin Jain
  • 1,314
  • 11
  • 15