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

**URL:** https://discourse.processing.org/t/how-can-i-retrieve-sms-on-android-phone-not-work/41888
**Category:** Processing for Android
**Created:** [May 2, 2023, 4:10pm UTC](https://discourse.processing.org/t/how-can-i-retrieve-sms-on-android-phone-not-work/41888 "2023-05-02T16:10:09Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)
#### Post date: [May 2, 2023, 4:10pm UTC](https://discourse.processing.org/t/how-can-i-retrieve-sms-on-android-phone-not-work/41888/1 "2023-05-02T16:10:09Z")

</div>

**[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:

> **[Demo code to receive SMS in Android/Processing - Processing Forum](https://forum.processing.org/one/topic/demo-code-to-receive-sms-in-android-processing.html)**
>
> Processing Forum

> **[Is access to SMS available? - Processing Forum](https://forum.processing.org/one/topic/is-access-to-sms-available.html)**
>
> Processing Forum

> **[Processing 2.x and 3.x Forum](https://forum.processing.org/two/discussion/3148/receiving-sms-in-processing)**
>
> Processing is an electronic sketchbook, a language and a worldwide community. This is its forum.

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

```auto

//=================================================================//
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); 
    
  }}); 
  //-------------------------------------------------// 
  
}
//=================================================================//

```

---

<div class="post-metadata">

### Author: ![JoseMY](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josemy/32/2016_2.png) [@JoseMY](https://discourse.processing.org/u/JoseMY)
#### Post date: [May 16, 2023, 2:44pm UTC](https://discourse.processing.org/t/how-can-i-retrieve-sms-on-android-phone-not-work/41888/2 "2023-05-16T14:44:58Z")

</div>

Hello!

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

---

<div class="post-metadata">

### Author: ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)
#### Post date: [May 16, 2023, 6:42pm UTC](https://discourse.processing.org/t/how-can-i-retrieve-sms-on-android-phone-not-work/41888/3 "2023-05-16T18:42:39Z")

</div>

@JoseMY

- link : [Request runtime permissions &nbsp;|&nbsp; Android Developers](https://developer.android.com/training/permissions/requesting?hl=en)

I’ll refer you to it. thank you.

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [May 17, 2023, 12:31am UTC](https://discourse.processing.org/t/how-can-i-retrieve-sms-on-android-phone-not-work/41888/4 "2023-05-17T00:31:59Z")

</div>

@GWAK

Hi

Try this

> **[processing-androidExamples/Basic/SMS\_Receive at master ·...](https://github.com/tms-martins/processing-androidExamples/tree/master/Basic/SMS_Receive)**
>
> master/Basic/SMS\_Receive

> **[processing-androidExamples/Basic/SMS\_Send at master ·...](https://github.com/tms-martins/processing-androidExamples/tree/master/Basic/SMS_Send)**
>
> master/Basic/SMS\_Send

---

<div class="post-metadata">

### Author: ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)
#### Post date: [May 17, 2023, 5:25pm UTC](https://discourse.processing.org/t/how-can-i-retrieve-sms-on-android-phone-not-work/41888/5 "2023-05-17T17:25:18Z")

</div>

@jafal

Thank you. This is a really good resource.

thank you

---

<div class="post-metadata">

### Author: ![SantosCuccia](https://avatars.discourse-cdn.com/v4/letter/s/aeb1de/32.png) [@SantosCuccia](https://discourse.processing.org/u/SantosCuccia)
#### Post date: [July 3, 2023, 7:06am UTC](https://discourse.processing.org/t/how-can-i-retrieve-sms-on-android-phone-not-work/41888/6 "2023-07-03T07:06:16Z")

</div>

Do you have no permission to open it？

---

<div class="post-metadata">

### Author: ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)
#### Post date: [July 3, 2023, 7:31am UTC](https://discourse.processing.org/t/how-can-i-retrieve-sms-on-android-phone-not-work/41888/7 "2023-07-03T07:31:30Z")

</div>

@SantosCuccia

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