1

I'm trying to turn off the cell radio whenever the phone is idle. But the following error pops up on building the project.

Error:(41, 23) error: cannot find symbol method setRadioPower(boolean)

I have referred to a lot of resources and everywhere the way is followed its not working for me.

I am posting my Java files here:

MainActivity.java

public class MainActivity extends AppCompatActivity {
    private Phone ph = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final TelephonyManager telephonyManager =
                (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
        ph = com.example.vk9621.radiocall.PhoneFactory.getDefaultPhone();
        PhoneStateListener callStateListener = new PhoneStateListener() {
            public void onCallStateChanged(int state, String incomingNumber)
            {
                if(state==TelephonyManager.CALL_STATE_RINGING){
                    Toast.makeText(getApplicationContext(),"Phone Is Ringing",
                            Toast.LENGTH_LONG).show();
                }
                if(state==TelephonyManager.CALL_STATE_OFFHOOK){
                    Toast.makeText(getApplicationContext(),"Phone is Currently in A call",
                            Toast.LENGTH_LONG).show();
                }

                if(state==TelephonyManager.CALL_STATE_IDLE){
                    ph.setRadioPower(false);
                }
            }
        };
        telephonyManager.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);

    }

}

My PhoneFactory.java is:

import android.content.Context;
import android.os.Looper;
import android.provider.ContactsContract;

/**
 * Created by VK9621 on 1/18/2018.
 */

public class PhoneFactory {
    static final String LOG_TAG = "PhoneFactory";
    static final int SOCKET_OPEN_RETRY_MILLIS = 2 * 1000;
    static final int SOCKET_OPEN_MAX_RETRY = 3;
    //***** Class Variables
    static private ContactsContract.CommonDataKinds.Phone sProxyPhone = null;

    static private boolean sMadeDefaults = false;

    static private Looper sLooper;
    static private Context sContext;

    public static ContactsContract.CommonDataKinds.Phone getDefaultPhone() {
        if (sLooper != Looper.myLooper()) {
            throw new RuntimeException(
                    "PhoneFactory.getDefaultPhone must be called from Looper thread");
        }
        if (!sMadeDefaults) {
            throw new IllegalStateException("Default phones haven't been made yet!");
        }
        return sProxyPhone;
    }
}

Can someone tell me what is the problem with the code??

Dipali Shah
  • 3,742
  • 32
  • 47

2 Answers2

0

In your MainActivity your variable ph of type Phone is having Incorrect import

i.e. com.example.vk9621.radiocall.PhoneFactory.getDefaultPhone() it should be of

import com.android.internal.telephony.Phone;

And as TelePhony the ITelephony interface is internal, so you cannot get a standard reference to it. You could use reflection all the way, i.e.

 TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);

        Method m1 = null;
        try {
            m1 = tm.getClass().getDeclaredMethod("getITelephony");
            m1.setAccessible(true);
            Object iTelephony = m1.invoke(tm);

            Method m2 = iTelephony.getClass().getDeclaredMethod("setRadioPower");

            m2.invoke(iTelephony,false); //if you want to do setRadioPower(false)
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

Check Google Group's Discussion of Phone Imports

Dipali Shah
  • 3,742
  • 32
  • 47
0

If your phone is not rooted, or if you do not have system permission android.Manifest.permission.MODIFY_PHONE_STATE, you won't be able to access this API.

See my answer here: https://stackoverflow.com/a/63450422/3148856