Search code examples
androidandroid-studiosmsmanager

cannot resolve symbol SmsManager.getDefault()


package com.soft.mash.contactmanager;

import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.widget.Toast;

public class SendSms extends Activity {

    SmsManager smsManager ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public boolean SendMessage(String number, String textMessage) {
        try {
            smsManager = (SmsManager) new SmsManager.getDefault();
            smsManager.sendTextMessage(number, null, textMessage, null, null);
            return true;
        }catch (Exception e)
        {
            Toast.makeText(getApplicationContext(),"SMS sending failed",Toast.LENGTH_SHORT).show();
            e.printStackTrace();
            return false;
        }
    }
}

Why

cannot resolve symbol 'getDefault()'

is occurring here?

In one similar StackOverflow question's answer advised to "Invalidate Cache and restart". But not solving my problem here.


Solution

  • What I was doing wrong here is I was trying to create new Instance of SmsManager by using new. But the things here needed is to only get the default Instance of SmsManager.