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;
}
}
有人能告诉我代码有什么问题吗
# 1 楼答案
在main活动中,Phone类型的变量
ph
的导入不正确也就是说,它应该是
由于
TelePhony
接口是内部接口,因此无法获得对它的标准引用。你可以一直使用反射,即Check Google Group's Discussion of Phone Imports
# 2 楼答案
如果您的手机没有根目录,或者您没有系统权限
android.Manifest.permission.MODIFY_PHONE_STATE
,您将无法访问此API请看我的答案: https://stackoverflow.com/a/63450422/3148856