Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi sir
I am new on Android and I am now being face a problem in sms sending and receiving Actually I have 5 Activity class in project name ZigbeeActivity,SettingActivity,OnOffActivity etc each and a seprate class SendSMS and SMSReceiver Class now I wish to send a sms which compose from SettingActivit and call a function to SMSsend of SendSMSclass but when sending the Fatal Exception is shown on Settingctiviy screen.I have tied the source code of SendSMS class below pl. help me.

Java
package com.sms;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import android.app.PendingIntent;
import android.telephony.SmsManager;
import android.widget.Toast;
import android.app.Activity;
public class SendSMS extends Activity{
	private Object obj;
	public SendSMS(Object object){
		this.obj = object;
		}
	  //---sends an SMS message to another device---
	@SuppressWarnings("unused")
	public void sendSMS(String phoneNumber, String message)
    { 
		try{
        String SENT = "SMS_SENT";
        String DELIVERED = "SMS_DELIVERED";
 
        PendingIntent sentPI = PendingIntent.getActivity(this, 0, new Intent(this,obj.getClass()), 0);
 
        PendingIntent deliveredPI = PendingIntent.getActivity(this, 0, new Intent(this,obj.getClass()), 0);
 
        //---when the SMS has been sent---
        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode())
                {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "SMS sent", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Toast.makeText(getBaseContext(), "Generic failure", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        Toast.makeText(getBaseContext(), "No service", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        Toast.makeText(getBaseContext(), "Null PDU", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        Toast.makeText(getBaseContext(), "Radio off", 
                                Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        }, new IntentFilter(SENT));
 
        //---when the SMS has been delivered---
        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode())
                {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "SMS delivered", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case Activity.RESULT_CANCELED:
                        Toast.makeText(getBaseContext(), "SMS not delivered", 
                                Toast.LENGTH_SHORT).show();
                        break;                        
                }
            }
        }, new IntentFilter(DELIVERED));        
 
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);        
  
    }catch(Exception e){
    	Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show();
    }
    }

}

thanks n regards
Om Parkash
Posted
Updated 16-Feb-12 19:22pm
v2

1 solution

You have an

@SuppressWarnings("unused")

in there - so I assume the function is never used and you do not see that in the IDE as the warning doesn't show up due to this annotation but when you would delete that you would find out that there is something wrong and then you would find out where the FatalException pops. *takeAdeepBreath();*
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900