Monday, September 3, 2012

[android-developers] Receive Sms

I write this broadcast receiver to receive sms from certains number :

    package ir.smspeik.sms;
   
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.telephony.SmsMessage;
    import android.widget.Toast;
   
    public class ReceiveSms extends BroadcastReceiver{
     @Override
     public void onReceive(Context context, Intent intent)
     {
     //---get the SMS message passed in---
      //---get the SMS message passed in---
      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();
      //---launch the MainActivity---
      Intent mainActivityIntent = new Intent(context, GetResponse.class);
     mainActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      context.startActivity(mainActivityIntent);
      //---send a broadcast to update the SMS received in the activity---
      Intent broadcastIntent = new Intent();
      broadcastIntent.setAction("SMS_RECEIVED_ACTION");
      //intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK"
      broadcastIntent.putExtra("sms", str);
      context.sendBroadcast(broadcastIntent);
     this.abortBroadcast();
     // this.clearAbortBroadcast();
      }
      }
     }

 
and this Intent for receiving sms :

    package ir.smspeik.sms;
   
    import android.app.Activity;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.os.Bundle;
    import android.widget.EditText;
    import android.widget.TextView;
    import android.widget.Toast;
   
    public class GetResponse extends Activity{
     IntentFilter intentFilter; 
     String[] sms;
     private BroadcastReceiver intentReceiver = new BroadcastReceiver() {
         @Override
         public void onReceive(Context context, Intent intent) {
             //---display the SMS received in the TextView---
          //TextView SMSes = (TextView) findViewById(R.id.textView1);
          Toast.makeText(context, intent.getExtras().getString("sms") + "Ehsan",
          Toast.LENGTH_SHORT).show();
           sms = intent.getExtras().getString("sms").split("-");
           EditText smsNo = (EditText) findViewById(R.id.txtMessageNo);
           smsNo.setText("Ehsan");
          // Toast.makeText(,sms[0],
         //     Toast.LENGTH_SHORT).show();
           TextView smsBody = (TextView) findViewById(R.id.lblMessageNo);
           smsBody.setText(sms[1]);
           //Toast.makeText(context,sms[1],Toast.LENGTH_SHORT).show();
           }
     };
     
      @Override
         public void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.getresponse);
           intentFilter = new IntentFilter();
             intentFilter.addAction("SMS_RECEIVED_ACTION");
   
             //---register the receiver---
             registerReceiver(intentReceiver, intentFilter);
            
          }
     
         public void onStart()
         {
         super.onStart();
       
         }
         public void onRestart()
         {
          //registerReceiver(intentReceiver, intentFilter);
         super.onRestart();
       
         }
         public void onResume()
         {
          registerReceiver(intentReceiver, intentFilter);
         super.onResume();
        
         }
         public void onPause()
         {
          unregisterReceiver(intentReceiver);
         super.onPause();
       
         }
         public void onStop()
         {
         super.onStop();
        
         }
         public void onDestroy()
         {
         // unregisterReceiver(intentReceiver);
         super.onDestroy();
       
         }
     }

SMS is received and Intent started but nothing shown. When It was working the intent must run or in background to show the sms but when intent was killed or first one that it is runned, nothing shown. How can I fix it?

 

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment