Android SMS Receiver not working

后端 未结 5 843
南旧
南旧 2020-12-11 13:23

I am new to android programming please help me in resolving a problem.

My code to receive sms is not working.

the manifest file is

<         


        
相关标签:
5条回答
  • 2020-12-11 13:59
    package com.google.android;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.telephony.SmsMessage;
    import android.widget.Toast;
    
    public class SMSReceiver extends BroadcastReceiver {    
        @Override /** This line is important, as you have not overriden the original method*/
        public void onReceive(Context context, Intent intent) {
            //---get the SMS message passed in---
            Bundle bundle = intent.getExtras();        
            SmsMessage[] msgs = null;
            String str = "";            
            if (bundle != null) {
                //---retrieve the SMS message received---
                Object[] pdus = (Object[]) bundle.get("pdus");
                msgs = new SmsMessage[pdus.length];            
                for (int i=0; i<msgs.length; i++) {
                    msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                
                    str += "SMS from " + msgs[i].getOriginatingAddress();                     
                    str += " :";
                    str += msgs[i].getMessageBody().toString();
                    str += "\n";        
                }
                //---display the new SMS message---
                Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
            }
        }
    }
    

    I hope this will help you.

    0 讨论(0)
  • 2020-12-11 14:00

    Here is what I have working for me at the moment. The code that I'm providing is used to block incoming text messages, but you can easily modify it to only include the area where it only alerts you of incoming messages and doesn't process them any further.


    SmsReceiver.java

    package com.android.SMS;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.telephony.SmsMessage;
    import android.widget.Toast;
    
    public class SmsReceiver extends BroadcastReceiver {
    
    public static int MSG_TPE=0;
    private String getAddress;
    public void onReceive(Context context, Intent intent) { 
        String MSG_TYPE=intent.getAction();
            if(MSG_TYPE.equals("android.provider.Telephony.SMS_RECEIVED")) {
                Toast received = Toast.makeText(context,"SMS Received: "+MSG_TYPE , Toast.LENGTH_LONG);
                received.show();
    
                    Bundle bundle = intent.getExtras();
                    Object messages[] = (Object[]) bundle.get("pdus");
                    SmsMessage smsMessage[] = new SmsMessage[messages.length];
                    for (int n = 0; n < messages.length; n++) {
                        smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
                    }
    
                        getAddress = smsMessage[0].getOriginatingAddress();
                        // Filter incoming messages
                        if(getAddress.equals("APPROVEDPHONENUMBER")) {
                            Toast approved = Toast.makeText(context,"Approved SMS from: " + smsMessage[0].getOriginatingAddress(), Toast.LENGTH_LONG);
                            approved.show();
                                // Message is approved and let through
                        } else {
                            Toast blocked = Toast.makeText(context,"Blocked SMS from: " + smsMessage[0].getOriginatingAddress(), Toast.LENGTH_LONG);
                            blocked.show();
                                // Message is blocked
                                abortBroadcast();
                        }
                        // End filter
                            for(int i=0;i<8;i++) {
                                System.out.println("Blocking SMS");
                            }
    
            }
    
    }
    
    }
    


    This is the code that detects an incoming message

    if(MSG_TYPE.equals("android.provider.Telephony.SMS_RECEIVED")) {
            Toast received = Toast.makeText(context,"SMS Received: "+MSG_TYPE , Toast.LENGTH_LONG);
            received.show();
    }
    



    AndroidManifest.xml

    PERMISSIONS:

    <uses-feature android:name="android.hardware.telephony" /> 
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.WRITE_SMS" />
    <uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    


    APPLICATION BLOCK:

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
    
    
        <service android:name=".MyService" android:enabled="true"/>
         <receiver android:name="SmsReceiver">
                <intent-filter android:priority="2147483647">
                    <action android:name="android.provider.Telephony.SMS_SENT"/>
                </intent-filter>
         </receiver>
        <service android:name=".MyServiceSentReceived" android:enabled="true"/>
             <receiver android:name="SmsReceiver">
                    <intent-filter android:priority="2147483645">
                        <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
                    </intent-filter>
             </receiver>
    
    </application>
    

    It's important to place the service and receiver blocks inside of your main "application" block as shown in the code above.

    0 讨论(0)
  • 2020-12-11 14:00

    delete this line. import android.telephony.gsm.SmsMessage;

    please be sure that SmsMessage is must class under "android.telephony".

    This may help you... :)

    0 讨论(0)
  • 2020-12-11 14:03

    You may need to add the BROADCAST_SMS permission to your receiver

    <receiver android:name=".SmsReceiver" android:enabled="true"
        android:permission="android.permission.BROADCAST_SMS">
        <intent-filter android:priority="2147483647">
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>
    
    0 讨论(0)
  • 2020-12-11 14:12

    Ensure your App has permission on the device/emulator:

    On the device/emulator go to... Settings -> Privacy -> Permission manager -> SMS

    Ensure your App is in the ALLOWED list. If it's in the denied list, click on it and select 'Allow'

    I went round in circles for an hour or two wondering why my code wasn't being called until I realised this.

    0 讨论(0)
提交回复
热议问题