Search...

Showing posts with label Broadcast Receiver. Show all posts
Showing posts with label Broadcast Receiver. Show all posts

Tuesday, March 20, 2012

How to Listen For Incoming SMS Messages in Android?

Step1



<uses-permission id="android.permission.RECEIVE_SMS" />

    <application>

        <receiver class=".TestSMSApp">
            <intent-filter>

                <action android:value="android.provider.Telephony.SMS_RECEIVED" />

            </intent-filter>
        </receiver>

    </application>

Step2:



package org.apache.sms;

import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.IntentReceiver;
import android.os.Bundle;
import android.provider.Telephony;
import android.util.Log;
import android.telephony.gsm.SmsMessage;

public class TestSMSApp extends IntentReceiver {
    private static final String LOG_TAG = "TestSMSApp";

    /* package */ static final String ACTION =
            "android.provider.Telephony.SMS_RECEIVED";

    public void onReceiveIntent(Context context, Intent intent) {
        if (intent.getAction().equals(ACTION)) {

           Bundle bundle = intent.getExtras();      
        SmsMessage[] msgs = null;
        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]);              
                str += "SMS from " + msgs[i].getOriginatingAddress();                    
                str += " :";
                str += msgs[i].getMessageBody().toString();
                str += "\n";      
            }
            //---display the new SMS message---
            Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
         
            }
         
          }
        }
   }

   




How to Retrieve incoming call’s phone number in Android?

Step1: Add Below code in your manifest file


 <receiver android:name=".CallBroadcastReceiver">
        <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />    
        </intent-filter>
</receiver>

</application>
<uses-sdk android:minSdkVersion="5" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />


Step2: Create class to receive Broadcast message




public class CallBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
   
 String action = intent.getAction();

           if(action.equalsIgnoreCase("android.intent.action.PHONE_STATE")){
              if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
                                  TelephonyManager.EXTRA_STATE_RINGING)) {
                  //Incoming call
               doSomething(intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER));
              }
}