Android SMS Receiver not working

后端 未结 5 845
南旧
南旧 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 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:

     
    
    
    
    
    


    APPLICATION BLOCK:

    
    
    
        
         
                
                    
                
         
        
             
                    
                        
                    
             
    
    
    

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

提交回复
热议问题