Android 使用 telephonyManager 的问题
Android problems using telephonyManager
我有一个 android 应用程序,我可以在其中获取连接状态、连接类型(WIFI、移动)和信号强度(对于每种连接类型)。
java代码:
public class MainActivity extends Activity {
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button start = (Button) findViewById(R.id.button);
final TextView status = (TextView) findViewById(R.id.status);
final TextView type = (TextView) findViewById(R.id.type);
final TextView speed = (TextView) findViewById(R.id.speed);
final ConnectivityManager cm = (ConnectivityManager) getSystemService(this.CONNECTIVITY_SERVICE);
final WifiManager wm = (WifiManager) getSystemService(this.WIFI_SERVICE);
final TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NetworkInfo netInfo = cm.getActiveNetworkInfo();
WifiInfo wifiInfo = wm.getConnectionInfo();
CellInfoGsm cellInfoGsm = (CellInfoGsm) tm.getAllCellInfo().get(0);
CellSignalStrengthGsm cellSignal = cellInfoGsm.getCellSignalStrength();
if (netInfo != null && netInfo.isConnected()) {
status.setText("Status: connected");
type.setText("Type: " + netInfo.getTypeName());
if (type.getText().equals("Type: WIFI")) {
speed.setText("Signal: " + wifiInfo.getLinkSpeed()+ "dBm");
}else{
speed.setText("Signal: " + cellSignal.getDbm() + "dBm");
}
}else{
status.setText("Status: not connected");
type.setText("Type: /");
speed.setText("Signal: /");
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}}
和布局代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:id="@+id/layout">
<TextView android:text="Status: /" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/status"
android:textColor="#ff000000" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:id="@+id/button"
android:layout_below="@+id/status"
android:layout_centerHorizontal="true"
android:layout_marginTop="105dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Signal: /"
android:id="@+id/speed"
android:textColor="#ff000000"
android:layout_below="@+id/type"
android:layout_alignParentStart="true"
android:layout_marginTop="21dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Type: /"
android:id="@+id/type"
android:layout_marginTop="20dp"
android:layout_below="@+id/status"
android:layout_alignParentStart="true"
android:textColor="#ff000000" />
以及权限:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
这在模拟器(nexus 4 API 22/nexus 4 API 21)上运行得非常好,但是当我在我的三星 Galaxy S4(Lollipop 5.0.1)上测试它时 returns 错误 "Application has stopped"。
如果我在没有 telephonyManager(和相关说明)的情况下测试它,它就可以工作。
可能是什么问题?
谢谢!
logCat 这里:
http://www.yourfilelink.com/get.php?fid=1165584
可能有很多原因。没有日志就不能准确地说。例如-
cm.getActiveNetworkInfo();
这可能 return null
在实际设备中进行测试时可能会导致目前可能没有活动网络。即使设备已连接,也很容易成为竞争条件。
已更新
你可能正面临ClassCastException
您如何确定 tm.getAllCellInfo().get(0)
是 CellInfoGsm
?我不知道你住在哪里,但可能会有所不同,例如 CellInfoLte
再次可以肯定地说没有适当的日志
我有一个 android 应用程序,我可以在其中获取连接状态、连接类型(WIFI、移动)和信号强度(对于每种连接类型)。
java代码:
public class MainActivity extends Activity {
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button start = (Button) findViewById(R.id.button);
final TextView status = (TextView) findViewById(R.id.status);
final TextView type = (TextView) findViewById(R.id.type);
final TextView speed = (TextView) findViewById(R.id.speed);
final ConnectivityManager cm = (ConnectivityManager) getSystemService(this.CONNECTIVITY_SERVICE);
final WifiManager wm = (WifiManager) getSystemService(this.WIFI_SERVICE);
final TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NetworkInfo netInfo = cm.getActiveNetworkInfo();
WifiInfo wifiInfo = wm.getConnectionInfo();
CellInfoGsm cellInfoGsm = (CellInfoGsm) tm.getAllCellInfo().get(0);
CellSignalStrengthGsm cellSignal = cellInfoGsm.getCellSignalStrength();
if (netInfo != null && netInfo.isConnected()) {
status.setText("Status: connected");
type.setText("Type: " + netInfo.getTypeName());
if (type.getText().equals("Type: WIFI")) {
speed.setText("Signal: " + wifiInfo.getLinkSpeed()+ "dBm");
}else{
speed.setText("Signal: " + cellSignal.getDbm() + "dBm");
}
}else{
status.setText("Status: not connected");
type.setText("Type: /");
speed.setText("Signal: /");
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}}
和布局代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:id="@+id/layout">
<TextView android:text="Status: /" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/status"
android:textColor="#ff000000" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:id="@+id/button"
android:layout_below="@+id/status"
android:layout_centerHorizontal="true"
android:layout_marginTop="105dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Signal: /"
android:id="@+id/speed"
android:textColor="#ff000000"
android:layout_below="@+id/type"
android:layout_alignParentStart="true"
android:layout_marginTop="21dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Type: /"
android:id="@+id/type"
android:layout_marginTop="20dp"
android:layout_below="@+id/status"
android:layout_alignParentStart="true"
android:textColor="#ff000000" />
以及权限:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
这在模拟器(nexus 4 API 22/nexus 4 API 21)上运行得非常好,但是当我在我的三星 Galaxy S4(Lollipop 5.0.1)上测试它时 returns 错误 "Application has stopped"。
如果我在没有 telephonyManager(和相关说明)的情况下测试它,它就可以工作。
可能是什么问题?
谢谢!
logCat 这里:
http://www.yourfilelink.com/get.php?fid=1165584
可能有很多原因。没有日志就不能准确地说。例如-
cm.getActiveNetworkInfo();
这可能 return null
在实际设备中进行测试时可能会导致目前可能没有活动网络。即使设备已连接,也很容易成为竞争条件。
已更新
你可能正面临ClassCastException
您如何确定 tm.getAllCellInfo().get(0)
是 CellInfoGsm
?我不知道你住在哪里,但可能会有所不同,例如 CellInfoLte
再次可以肯定地说没有适当的日志