How can I retrieve SMS on Android phone? - not work

[How can I retrieve SMS on Android phone?]

hello. Nice to meet you.

I want to receive text messages on Android.

Here is the reference link:

I used the source code below, but the app closes automatically. what could be wrong?


//=================================================================//
import android.app.Activity;
import android.widget.FrameLayout;     
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.View.OnClickListener;
import android.R;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.AdapterView;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.app.AlertDialog;
import android.widget.EditText;
import android.os.Looper;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;
//=================================================================//

//=================================================================//
  Context ctx;
  Activity act;
  
  String message = ""; 
  String number = ""; 
  SmsReceiver mySMSReceiver = new SmsReceiver();
//=================================================================//

//=================================================================//
void setup(){ 

  //-------------------------------------------------// 
  fullScreen();
  orientation(PORTRAIT);
  //-------------------------------------------------// 
  //-------------------------------------------------// 
  act = this.getActivity();
  ctx = this.getActivity().getApplicationContext();
  Looper.prepare();
  //-------------------------------------------------// 
  
}
//=================================================================//

//=================================================================//
void draw(){

  
  //-------------------------------------------------// 
  if(message!=""){ 
    fill(0,0,0);rect(0,0,400,600);
    fill(255,0,0);
    text("New message !",10,40);
    text(message,10,90);
    text("From : "+number,10,130);
    message="";      
  }
  //-------------------------------------------------// 


}
//=================================================================//


//=================================================================//
public class SmsReceiver extends BroadcastReceiver{

  //-------------------------------------------------// 
  public void onReceive(Context context, Intent intent) 
  {
      //---get the SMS message passed in---
      Bundle bundle = intent.getExtras();        
      SmsMessage[] msgs = null;
      String caller="";
      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]);                
              caller += msgs[i].getOriginatingAddress();
              str += msgs[i].getMessageBody().toString();
          }
                  
      }   
      message=str;
      number=caller;           
  }
  //-------------------------------------------------// 
}
//=================================================================//


//=================================================================//
public void onCreate(Bundle savedInstanceState) {
  
  super.onCreate(savedInstanceState);  
  //-------------------------------------------------// 
  act.runOnUiThread(new Runnable() {public void run() {
    
    IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");  
    act.registerReceiver(mySMSReceiver, filter); 
    
  }}); 
  //-------------------------------------------------// 
  
}
//=================================================================//

Hello!

You must request permissions at runtime. I don’t remember well how is it done, but you can google for it.

1 Like

@JoseMY

I’ll refer you to it. thank you.

@GWAK

Hi

Try this

2 Likes

@jafal

Thank you. This is a really good resource.

thank you

1 Like

Do you have no permission to open it?

1 Like

@SantosCuccia

Thank you for the reply.
I added it with ‘permission’ and proceeded.