SmS Reading in Android Programmatically

An Android you can read Sms programmatically. When you able to read Sms Programmatically then you can do more advance task by this operation. Like I read sms and Change profile of the phone and read contact by name and the result of contact search are send back to my phone from where we search the contact. Let Enjoy it.
Create new project and  add class SmsRemoteController and extends it from BroadcastReceiver .
BroadcastReceiver class is a single method onReceive().Add the following code in onReceive() method.

String sendingNumber="";
String smsBody="";
Bundle bundle=intent.getExtras();
            SmsMessage[] msgs = null;
            String smsBody="";
              if (bundle!=null) {
                    Object[] pdus=(Object[])bundle.get("pdus");
                    msgs=new SmsMessage[pdus.length];
                    for (int i = 0; i < pdus.length; i++) {
                          msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                          sendingNumber += msgs[i].getOriginatingAddress();
                          smsBody=msgs[i].getMessageBody().toString();
                      }
        Toast.makeText(getApplicationContext(), smsBody,
                    Toast.LENGTH_SHORT).show();

Now add the SmsRemoteController receiver class to manifest file
 <receiver android:name=".SmsRemoteController" >
          <intent-filter android:priority="100">
              <action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
          </intent-filter> 
        </receiver>

Run your application . Its will Show you sms body on toast when any sms received on your device or
emulator.


Now we write the code of our project which we discuss in start to read contact and change porfile mode of mobile Remotely through on single sms.
First we add the layout file code
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     android:background="@drawable/background" >

    <LinearLayout
        android:id="@+id/buttonLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:orientation="vertical" >

        <ToggleButton
            android:id="@+id/enableDisable"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textOn="Sms Controller ON"
            android:textOff="Sms Controller OFF"
            android:width="160dp"
             />
    </LinearLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/buttonLayout"
        android:layout_centerHorizontal="true" >

         <TextView
             android:id="@+id/textView1"
             android:layout_width="wrap_content"
             android:layout_height="match_parent"
             android:gravity="bottom|center"
             android:textSize="25px"
             android:text="Enable And Disable Sms Controller"
             android:width="160dp" />

         <Button
             android:id="@+id/btnHelp"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_alignParentTop="true"
             android:layout_centerHorizontal="true"
             android:text="Help"
             android:width="160dp" />
     </RelativeLayout>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="Naqib 03469280195" />

</RelativeLayout>
 Now  I write the code of java activity for this layout file where we add single to button to enable and disable the SmsController Program.

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ToggleButton;

public class SmsController extends Activity implements OnClickListener, OnCheckedChangeListener{
 private SharedPreferences myPrefs;
 private SharedPreferences.Editor editor;
 private Button btnHelp;
 private ToggleButton btnToggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        // save State of Toggle Button
        myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
        editor=myPrefs.edit();
        btnToggle=(ToggleButton) findViewById(R.id.enableDisable);
        btnHelp=(Button) findViewById(R.id.btnHelp);
        btnHelp.setOnClickListener(this);
        btnToggle.setOnCheckedChangeListener(this);
         setDefaultButtonChecked();
    }
    private  void setDefaultButtonChecked()
    {
        String value=getSharedPreferences();
        if(value.equals("ON"))
        {
            btnToggle.setChecked(true);
        }
        else if(value.equals("OFF"))
        {
            btnToggle.setChecked(false);
        }
        else {
            btnToggle.setChecked(false);
        }
    }

    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.btnHelp:
             startActivity(new Intent(this,HelpActivity.class));
            break;

        default:
            break;
        }
    }


    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        // TODO Auto-generated method stub
        if(btnToggle.isChecked())
        {
             editor.putString("state", "ON");
             editor.commit();
        Toast.makeText(getApplicationContext(), "Sms Controller Activated", Toast.LENGTH_SHORT).show();   
        }
        else {
             editor.putString("state", "OFF");
             editor.commit();

        Toast.makeText(getApplicationContext(), "sms Controller Deactivated", Toast.LENGTH_SHORT).show();   
        }
    }
    private String getSharedPreferences() {
        // TODO Auto-generated method stub
         myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
         String value=myPrefs.getString("state", "not");
         return value;
    }
}

In last we add the complete code of SmsRemoteController class
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ToggleButton;

public class SmsController extends Activity implements OnClickListener, OnCheckedChangeListener{
 private SharedPreferences myPrefs;
 private SharedPreferences.Editor editor;
 private Button btnHelp;
 private ToggleButton btnToggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        // save State of Toggle Button
        myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
        editor=myPrefs.edit();
        btnToggle=(ToggleButton) findViewById(R.id.enableDisable);
        btnHelp=(Button) findViewById(R.id.btnHelp);
        btnHelp.setOnClickListener(this);
        btnToggle.setOnCheckedChangeListener(this);
         setDefaultButtonChecked();
    }
    private  void setDefaultButtonChecked()
    {
        String value=getSharedPreferences();
        if(value.equals("ON"))
        {
            btnToggle.setChecked(true);
        }
        else if(value.equals("OFF"))
        {
            btnToggle.setChecked(false);
        }
        else {
            btnToggle.setChecked(false);
        }
    }

    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.btnHelp:
             startActivity(new Intent(this,HelpActivity.class));
            break;

        default:
            break;
        }
    }


    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        // TODO Auto-generated method stub
        if(btnToggle.isChecked())
        {
             editor.putString("state", "ON");
             editor.commit();
        Toast.makeText(getApplicationContext(), "Sms Controller Activated", Toast.LENGTH_SHORT).show();   
        }
        else {
             editor.putString("state", "OFF");
             editor.commit();

        Toast.makeText(getApplicationContext(), "sms Controller Deactivated", Toast.LENGTH_SHORT).show();   
        }
    }
    private String getSharedPreferences() {
        // TODO Auto-generated method stub
         myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
         String value=myPrefs.getString("state", "not");
         return value;
    }
}
Below is the manifest file code
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.smsremotecontroller"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="15" />
       <uses-permission android:name="android.permission.RECEIVE_SMS"/>
      <uses-permission android:name="android.permission.SEND_SMS"/>
      <uses-permission android:name="android.permission.READ_CONTACTS"/>
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity android:name=".SmsController" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"></action>
               <category android:name="android.intent.category.LAUNCHER"></category>
            </intent-filter>
        </activity>
         <receiver android:name=".SmsRemoteController" >
          <intent-filter android:priority="100">
              <action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
          </intent-filter> 
        </receiver>
        <activity android:name=".HelpActivity"></activity>
    </application>

</manifest>
Now See the screen shot of Projects
Now you are able to understand my project partially . Following are the source code and apk file inside the soure code bin folder
Download_Here Source Code And Apk File

Post a Comment

6 Comments

  1. Hi, Naqib! This is really useful. I can use this in my app. :)

    ReplyDelete
  2. i need that code can you send me to my email aqilahliyana35@gmail.com. Thanks

    ReplyDelete
  3. GOOD JOB ☺
    Kust Students are really doing very well, keep it up

    ReplyDelete
  4. My vote Goes to your important information
    the information provided by you is really helpful
    we provide service WhatsApp API Integration With Tally

    ReplyDelete