gpt4 book ai didi

java - 找不到符号方法 setRadioPower()

转载 作者:太空宇宙 更新时间:2023-11-04 10:46:52 25 4
gpt4 key购买 nike

每当手机空闲时,我都会尝试关闭手机 radio 。但是构建项目时会弹出以下错误。

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

我引用了很多资源,但到处遵循的方法都不适合我。

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

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);

}

}

我的PhoneFactory.java是:

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;
}
}

有人可以告诉我代码有什么问题吗?

最佳答案

在您的 MainActivity 中,Phone 类型的变量 ph 导入不正确

com.example.vk9621.radiocall.PhoneFactory.getDefaultPhone() 它应该是

import com.android.internal.telephony.Phone;

由于 TelePhonyITelephony 接口(interface)是内部的,因此您无法获得对其的标准引用。您可以一直使用反射,即

 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

关于java - 找不到符号方法 setRadioPower(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48314284/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com