[英]Android telephony manager Crashes
I'm an absolute beginner to android , I tried to create a small application that can display GSM Cellular tower details signal strength etc.. , As I mentioned above , I'm a novice to these applications ,I tried the code below using stack-overflow search results, however application crashes without starting. 我绝对是android的初学者,我试图创建一个可以显示GSM蜂窝塔详细信号强度等的小应用程序。如上所述,我是这些应用程序的新手,我尝试使用堆栈下面的代码-溢出搜索结果,但是应用程序崩溃而没有启动。
i would be grateful if someone could help me ! 如果有人可以帮助我,我将不胜感激! Thanks.
谢谢。
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.telephony.PhoneStateListener;
import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
// TelephonyManager Tel;
MyPhoneStateListener MyListener;
private TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
private GsmCellLocation cellLocation = (GsmCellLocation)telephonyManager.getCellLocation();
private Button btnRefresh,btnClose,btnReset;
private TextView CellLocation,CellID,GSMCode,signalPow;
int cellid= cellLocation.getCid();
int celllac = cellLocation.getLac();
int signalST ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyListener = new MyPhoneStateListener();
telephonyManager = ( TelephonyManager )getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(MyListener ,PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
btnRefresh = (Button)findViewById(R.id.btnRefresh);
btnClose = (Button)findViewById(R.id.btnClose);
btnReset = (Button)findViewById(R.id.btnReset);
CellLocation = (TextView)findViewById(R.id.txtCellLocation);
CellID = (TextView)findViewById(R.id.txtBaseStation);
GSMCode = (TextView)findViewById(R.id.txtGSMLocation);
signalPow = (TextView)findViewById(R.id.txtSignalStr);
btnRefresh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
signalPow.setText(Integer.toString(signalST));
CellLocation.setText(cellLocation.toString());
CellID.setText(Integer.toString(cellid));
// GSMCode.setText(Integer.toString(celllac));
}
});
}
@Override
protected void onPause()
{
super.onPause();
telephonyManager.listen(MyListener, PhoneStateListener.LISTEN_NONE);
}
@Override
protected void onResume()
{
super.onResume();
telephonyManager.listen(MyListener,PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
}
public class MyPhoneStateListener extends PhoneStateListener
{
/* Get the Signal strength from the provider, each tiome there is an update */
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength)
{
super.onSignalStrengthsChanged(signalStrength);
// Toast.makeText(getApplicationContext(), "Go to Firstdroid!!! GSM Cinr = "
// + String.valueOf(signalStrength.getGsmSignalStrength()), Toast.LENGTH_SHORT).show();
signalST = signalStrength.getGsmSignalStrength();
//I have tried to assign signal strength to an integer and display onClik of a button
}
}
}
Links https://stackoverflow.com/a/33936721 链接https://stackoverflow.com/a/33936721
LOGCAT LOGCAT
10-28 04:22:34.594 1781-1781/com.example.gih.test1 I/art: Late-enabling -Xcheck:jni
10-28 04:22:34.615 1781-1786/com.example.gih.test1 I/art: Debugger is no longer active
10-28 04:22:34.628 1781-1781/com.example.gih.test1 W/System: ClassLoader referenced unknown path: /data/app/com.example.gih.test1-1/lib/x86
10-28 04:22:36.584 1781-1781/com.example.gih.test1 W/System: ClassLoader referenced unknown path: /data/app/com.example.gih.test1-1/lib/x86
10-28 04:22:36.685 1781-1791/com.example.gih.test1 W/art: Suspending all threads took: 8.202ms
10-28 04:22:36.723 1781-1781/com.example.gih.test1 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.gih.test1, PID: 1781
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.gih.test1/com.example.gih.test1.MainActivity}: java.lang.IllegalStateException: System services not available to Activities before onCreate()
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
at android.app.Activity.getSystemService(Activity.java:5253)
at com.example.gih.test1.MainActivity.<init>(MainActivity.java:18)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
10-28 04:22:41.885 1781-1781/com.example.gih.test1 I/Process: Sending signal. PID: 1781 SIG: 9
Manifest.xml Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gih.test1">
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
LOGCAT - AFTER MOVING initialization to On-create LOGCAT-将移动初始化为创建后
10-28 04:39:28.339 2343-2343/com.example.gih.test1 I/Process: Sending signal. PID: 2343 SIG: 9
10-28 04:41:46.462 2505-2505/com.example.gih.test1 W/System: ClassLoader referenced unknown path: /data/app/com.example.gih.test1-2/lib/x86
10-28 04:41:46.634 2505-2515/com.example.gih.test1 I/art: Background sticky concurrent mark sweep GC freed 10950(703KB) AllocSpace objects, 5(116KB) LOS objects, 63% free, 828KB/2MB, paused 441us total 117.451ms
10-28 04:41:47.113 2505-2505/com.example.gih.test1 W/System: ClassLoader referenced unknown path: /data/app/com.example.gih.test1-2/lib/x86
10-28 04:41:47.353 2505-2505/com.example.gih.test1 W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
10-28 04:41:47.601 2505-2505/com.example.gih.test1 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.gih.test1, PID: 2505
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gih.test1/com.example.gih.test1.MainActivity}: java.lang.SecurityException: getCellLocation: Neither user 10062 nor current process has android.permission.ACCESS_COARSE_LOCATION.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.SecurityException: getCellLocation: Neither user 10062 nor current process has android.permission.ACCESS_COARSE_LOCATION.
at android.os.Parcel.readException(Parcel.java:1599)
at android.os.Parcel.readException(Parcel.java:1552)
at com.android.internal.telephony.ITelephony$Stub$Proxy.getCellLocation(ITelephony.java:2292)
at android.telephony.TelephonyManager.getCellLocation(TelephonyManager.java:826)
at com.example.gih.test1.MainActivity.onCreate(MainActivity.java:29)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
replace this line 替换这条线
private TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
private GsmCellLocation cellLocation = (GsmCellLocation)telephonyManager.getCellLocation();
int cellid= cellLocation.getCid();
int celllac = cellLocation.getLac();
with 与
private TelephonyManager telephonyManager;
private GsmCellLocation cellLocation ;
int cellid;
int celllac;
write this to check location permission in api >=23 写这个来检查api> = 23中的位置许可
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
Manifest.permission.ACCESS_FINE_LOCATION)) {
} else {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
111);
}
}
return;
}
and override this method to check permission results 并重写此方法以检查权限结果
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case 111: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
then inside onCreate method write these as 然后在onCreate方法中将它们写为
telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
cellLocation = (GsmCellLocation)telephonyManager.getCellLocation();
cellid= cellLocation.getCid();
celllac = cellLocation.getLac();
Move variable initialization ( telephonyManager
, cellLocation
, cellid
, celllac
) into onCreate
, as logcat error says. 正如logcat错误指出的
cellLocation
,将变量初始化( telephonyManager
, cellLocation
, cellid
, celllac
)移动到onCreate
。
Important lines in logcat: Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
(what happened) and at com.example.gih.test1.MainActivity.<init>(MainActivity.java:18)
(where happened) so you can see exact line where the problem lies (18). logcat中的重要行:
Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
(发生的情况)和at com.example.gih.test1.MainActivity.<init>(MainActivity.java:18)
Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
(发生的位置),以便您可以看到问题所在的确切位置(18)。 After correcting that line, you will get similar error for next line ( cellLocation
). 更正该行之后,您将为下一行(
cellLocation
)得到类似的错误。 Then, if you try to start your app, you will get NullPointerException
for initialization of cellid
and and next time for celllac
. 然后,如果您尝试启动您的应用程序,则将获得
NullPointerException
来初始化cellid
,并下次获取celllac
。 After moving everything inside your onCreate
, your app should work. 在将所有内容移到
onCreate
内部之后,您的应用程序应该可以运行了。
Log with combination of debug tells you everything (in your case, not always :) ). 结合debug日志可以告诉您一切(在您的情况下,并不总是:))。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.