i have an android app in which i get connection status, connection type(WIFI, MOBILE) and the signal strength(for each type of connection).
The java code:
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);
}}
And layout code:
<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" />
And the permission:
<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"/>
This works really well on the emulator(nexus 4 API 22/nexus 4 API 21) but when I test it on my Samsung Galaxy S4(Lollipop 5.0.1) it returns the error "Application has stopped".
If I test it without telephonyManager(and the relative instructions) it works.
What could be the problem?
Thank you!
The logCat here:
http://www.yourfilelink.com/get.php?fid=1165584