I use this code as a broadcast receiver : 

    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, 
ir.smspeik.sms.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 activity to receive sms and show it : 

    package ir.smspeik.sms;
    
    import android.R.string;
    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.Button;
    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 myint) {
       // TODO Auto-generated method stub
       Toast.makeText(context, "intetn.", Toast.LENGTH_SHORT).show();
      String[] s = myint.getExtras().getString("sms").split("-");
      for(String i : s)
      {
       Toast.makeText(context, i, Toast.LENGTH_SHORT).show(); 
      TextView ed = (TextView) findViewById(R.id.txtMessageNo);
      ed.setText(i);
      }
      
      //TextView tNo = (Button) findViewById(R.id.txtMessageNo);
      //tNo.setText("ehsan");
      }
         
     };
     
      @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();
        
         }
     
     }
 
and the activity is register in manifest in below order : 

     <activity
                android:name=".GetResponse"
                 android:launchMode="singleTask"
                           android:label="@string/title_activity_main" >
                <intent-filter>
                    <action android:name="ir.smspeik.sms.GetResponse" />
    
                    <category 
android:name="android.intent.category.DEFAULT" />
                </intent-filter>
            </activity>
                    <receiver android:name=".ReceiveSms"  >
                <intent-filter android:priority="999">
                    <action 
android:name="android.provider.Telephony.SMS_RECEIVED"></action>
                </intent-filter>
            </receiver>
       

But first time application run and a sms received the activity start but 
nothing is shown and while activity is run or is in background it shows 
sms. How can I fix this?

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

Reply via email to