我编写了一个应用程序,显示有关电话运营商、网络类型等的信息。按下按钮后,它崩溃了。下面是程序代码和日志。
public class MainActivity extends AppCompatActivity {
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.info);
}
public void onSubmit(View v) {
TelephonyManager tm=(TelephonyManager)getSystemService(Context.TELECOM_SERVICE);
int callstate=tm.getCallState();
String callstat="";
switch(callstate){
case TelephonyManager.CALL_STATE_IDLE:
callstat="Call State: Phone is idle\n";
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
callstat="Call State: Phone is in use\n";
break;
case TelephonyManager.CALL_STATE_RINGING:
callstat="Call State: Phone is ringing\n";
break;
}
String opname="\n Operator ID:"+""+tm.getNetworkOperator();
opname=opname+"\n Operator Name:"+""+tm.getNetworkOperatorName();
int phoneType=tm.getPhoneType();
String ptype="";
switch (phoneType){
case TelephonyManager.PHONE_TYPE_CDMA:
ptype="\nPhone Type: CDMA";
break;
case TelephonyManager.PHONE_TYPE_GSM:
ptype="\nPhone Type: GSM";
break;
case TelephonyManager.PHONE_TYPE_SIP:
ptype="\nPhone Type: SIP";
break;
case TelephonyManager.PHONE_TYPE_NONE:
ptype="\nPhone Type: NONE";
break;
}
boolean isRoaming=tm.isNetworkRoaming();
String pDetails="";
if (isRoaming){
pDetails="Roaming : Yes\n";
}
else {
pDetails="Roaming : No\n";
}
tv.setText(callstat+opname+ptype+pDetails);
}
}
日志猫:
5527) 在 java.lang.reflect.Method.invoke(Native Method) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739) 在 com.android.internal.os.ZygoteInit.main( ZygoteInit.java:629) 11-12 14:44:42.531 25260-25260/com.galaxy.operator E/MQSEventManagerDelegate: 获取 MQSService 失败。11-12 14:44:42.547 25260-25260/? I/Process:发送信号。PID:25260 SIG:9 739) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629) 11-12 14:44:42.531 25260-25260/com.galaxy.operator E/MQSEventManagerDelegate: 未能获得 MQSService。11-12 14:44:42.547 25260-25260/? I/Process:发送信号。PID:25260 SIG:9 739) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629) 11-12 14:44:42.531 25260-25260/com.galaxy.operator E/MQSEventManagerDelegate: 未能获得 MQSService。11-12 14:44:42.547 25260-25260/? I/Process:发送信号。PID:25260 SIG:9
我在清单中提供了以下权限:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
代替它
对此: