How to read and forward certain SMSes programmatically in your Android App? - complete source code

Programmer World
Programmer World
18.3 هزار بار بازدید - 4 سال پیش - This video shows the steps
This video shows the steps to read the SMSes and forward certain SMS based on a particular condition. In this App, it forwards the SMS to a pre-defined number mentioned in the App's code. However, the number can be taken as an input from the layout also.

The SMS send activity is triggered by a button in the layout. However, it can be automated in the App's code and the forward can be done based on certain condition or time interval.


I hope this video is useful to you. For any questions, suggestions or appreciation please contact us at: https://programmerworld.co/contact/ or email at: [email protected]


The complete source code and details of this video is shared in the below link:
https://programmerworld.co/android/ho...


The main Java code is copied below also for reference:

package com.programmerworld.smsforwardapp;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
   private TextView textView;
   private String stringNumber = "0000";

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       ActivityCompat.requestPermissions(this,
               new String[]{Manifest.permission.READ_SMS,
                       Manifest.permission.SEND_SMS},
               PackageManager.PERMISSION_GRANTED);
       textView = findViewById(R.id.textView);
   }

   public void buttonForward(View view){
       Cursor cursor = getContentResolver().query(Uri.parse("content://sms"), null,null,null,null);
       cursor.moveToFirst();
       while (cursor!=null){
           String stringMessage = cursor.getString(12);
           if (stringMessage.contains("programmer")){
               SmsManager smsManagerSend = SmsManager.getDefault();
               smsManagerSend.sendTextMessage(stringNumber, null, stringMessage, null, null);
               textView.setText("Message Sent");
               break;
           }
           textView.setText("Message NOT found");
           cursor.moveToNext();
       }
   }
}
4 سال پیش در تاریخ 1399/09/30 منتشر شده است.
18,374 بـار بازدید شده
... بیشتر