Skip to content Skip to sidebar Skip to footer

How Can I Call A Function In Activity From Another Activity In Android?

I have a problem about sending sms in android. I have two classes. one of them is SendMessage.java and other one is SendingSms.java and I want to send my messages by SendingSms.jav

Solution 1:

Make the method as public static which you are going to access from another activity. then call it as activity1.sendsms();

This is the way . if you post the code will advice you better way... hope this will help you .

Hi use the below code. the thing is you need to use Context.

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.widget.Toast;

public class SendingSms extends Activity {

    static Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        context=this;

    }

     public static void sendSMS(String telNo, String mesaj)
        {
            String SENT = "SMS_SENT";
            String DELIVERED = "SMS_DELIVERED";

            PendingIntent sentPI = PendingIntent.getBroadcast(context, 0, new Intent(SENT), 0);

            PendingIntent deliveredPI = PendingIntent.getBroadcast(context, 0, new Intent(DELIVERED), 0);

            context.registerReceiver(new BroadcastReceiver() {

                @Override
                public void onReceive(Context arg0, Intent arg1) {
                    // TODO Auto-generated method stub
                    switch(getResultCode())
                    {
                    case Activity.RESULT_OK:
                        Toast.makeText(context, "SMS Gönderildi",Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                         Toast.makeText(context, "Generic failure",Toast.LENGTH_SHORT).show();
                         break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                         Toast.makeText(context, "No service",Toast.LENGTH_SHORT).show();
                         break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        Toast.makeText(context, "Null PDU",Toast.LENGTH_SHORT).show();
                        break;                  
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        Toast.makeText(context, "Radio off",Toast.LENGTH_SHORT).show();
                        break;
                    }
                }
            }, new IntentFilter(SENT));


            context. registerReceiver(new BroadcastReceiver(){
                @Override
                public void onReceive(Context arg0, Intent arg1) {
                    switch (getResultCode())
                    {
                        case Activity.RESULT_OK:
                            Toast.makeText(context, "SMS iletildi",
                                    Toast.LENGTH_SHORT).show();
                            break;
                        case Activity.RESULT_CANCELED:
                            Toast.makeText(context, "SMS iletilemedi",
                                    Toast.LENGTH_SHORT).show();
                            break;
                    }
                }

            }, new IntentFilter(DELIVERED));       

            SmsManager sms = SmsManager.getDefault();
            sms.sendTextMessage(telNo, null, mesaj, sentPI, deliveredPI);

         Toast.makeText(context, telNo.toString() + " - " + mesaj.toString(), Toast.LENGTH_LONG).show();
        }

} 

Solution 2:

First intialize the class in your sendMessage.java

sendinsms sm=new sendinsms();

sm.sendsms(your parameter);


Solution 3:

First of all, you are missing one permission:

> Caused by: java.lang.SecurityException: Sending SMS message: User
> 10048 does not have android.permission.SEND_SMS.

Secondly, this method should not be static - you can have a memory leak.

If you SendMessage extends SendSms you should make SendSms.sendSms method public and it should work

You should not use: static Context context;

context should be a normal field. And why you need it :>? You can use activity context


Solution 4:

you can do in two ways :

1. pass the context of the activity in which the method is present into the other activity where you want to call the method.

2. second way is to make the method static and then call it from other activity using classname with . operator

Eg:

Class_Name.method()


Post a Comment for "How Can I Call A Function In Activity From Another Activity In Android?"