有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java找不到符号方法setRadioPower()

每当手机闲置时,我都会关掉手机收音机。但在构建项目时会出现以下错误

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

我提到了很多资源,无论在哪里,人们都会遵循这种方式,但它对我来说不起作用

我在这里发布我的Java文件:

主要活动。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);

    }

}

我的电话工厂。java是:

import 安卓.content.Context;
import 安卓.os.Looper;
import 安卓.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;
    }
}

有人能告诉我代码有什么问题吗


共 (2) 个答案

  1. # 1 楼答案

    在main活动中,Phone类型的变量ph的导入不正确

    也就是说,它应该是

    import com.android.internal.telephony.Phone;
    

    由于TelePhony接口是内部接口,因此无法获得对它的标准引用。你可以一直使用反射,即

     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