Unregistered for listening sms
<p>Now I am going with a smsReceiver class. Eventhough the app works smoothly in emulator, it shows an error when comes to the real device. The app can be installed in both, but when comes under the real device, it shows a toasted message like 'SMS LISTENER IS ALREADY UN-REGISTERED'</p>

<p>This is my SMSReceiver class:</p>

<pre><code>  public void onReceive(Context context, Intent intent) {
this.mcontext = context;
if (intent.getAction().equals(VoiceofText.ACTION)) {
    Bundle bundle = intent.getExtras();
if (bundle != null) {
 readSMS(bundle);
}
 }
}

public void readSMS(Bundle bundle) {
SmsMessage[] msgs = null;

try {

 Object[] pdus = (Object[]) bundle.get("pdus");

 if (pdus != null) {

   msgs = new SmsMessage[pdus.length];

   String smsBodyStr = null;
   String phoneNoStr = null;

   for (int k = 0; k &lt; msgs.length; k++) {
    msgs[k] = SmsMessage.createFromPdu((byte[]) pdus[k]);

    smsBodyStr = msgs[k].getMessageBody().trim();

    phoneNoStr = msgs[k].getOriginatingAddress().trim();

    speakSMS(smsBodyStr, phoneNoStr);
      }
}
} catch (Exception exe) {
    exe.printStackTrace();
}
} 
</code></pre>

<p>This is the manifest file I used :</p>

<pre><code>&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="a.b.c"
android:versionCode="1"
android:versionName="1.0" &gt;

&lt;uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="8" /&gt;

 &lt;application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" &gt;

     &lt;receiver android:name=".SmsReceiver"&gt;  
         &lt;/receiver&gt;      

    &lt;activity
        android:label="@string/app_name"
        android:name=".SMSText" &gt;

                    &lt;intent-filter &gt;
            &lt;action android:name="android.intent.action.MAIN" /&gt;
            &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
        &lt;/intent-filter&gt;

    &lt;/activity&gt;


 &lt;/application&gt;

&lt;uses-permission android:name="android.permission.READ_SMS"/&gt;
&lt;uses-permission android:name="android.permission.RECEIVE_SMS"/&gt;
&lt;uses-permission android:name="android.permission.READ_CONTACTS"/&gt;

   &lt;/manifest&gt;
</code></pre>

<p>Here I am specifying the Toast messages in the next class named SMSText.java</p>

<pre><code>public void registerSMS() {
receiver = new SMSReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(ACTION);
registerReceiver(receiver, filter);

Toast.makeText(getApplicationContext(), "registered for incomming sms",
            Toast.LENGTH_LONG).show();
}


public void unregisterSMS() {
try {
unregisterReceiver(receiver);
Toast.makeText(getApplicationContext(),"unregistered for listening sms",  
                 Toast.LENGTH_LONG).show();
} 
catch (Exception exe) {Toast.makeText(getApplicationContext(),
                "sms listener is already unregistered",  
                        Toast.LENGTH_LONG).show();
 }
}
</code></pre>
