LoRa Gateway Packet Forwarder JSON Daten
25.12.2024
Elektronik | Funk | Software
Der Technik-Blog
On this page we show you how to read information from the mobile network with Android.These features require access to the telephone status as well as access to the location services. If it is not possible, the user is informed about a toast message, because the app checks the permissions at the start.
Android Version | API Version |
---|---|
4.0.3 | 15 |
MainAcitvity.java: This code is located in the MainActivity
package com.aeqweb.cellinfo;
import ...
public class MainActivity extends AppCompatActivity {
TelephonyManager telm;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.READ_PHONE_STATE)
!= PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this, "Please allow phone state access",
Toast.LENGTH_LONG).show();
} else {
if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this, "Please allow location services",
Toast.LENGTH_LONG).show();
} else {
if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this, "Please allow location service",
Toast.LENGTH_LONG).show();
} else {
telm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
TextView cid = (TextView) findViewById(R.id.cell_id);
TextView lac = (TextView) findViewById(R.id.cell_lac);
TextView nty = (TextView) findViewById(R.id.cell_type);
TextView mcc = (TextView) findViewById(R.id.cell_mcc);
TextView mnc = (TextView) findViewById(R.id.cell_mnc);
TextView imei = (TextView) findViewById(R.id.IMEI);
TextView cname = (TextView) findViewById(R.id.network_name);
cid.setText("" + cell_id());
lac.setText("" + cell_lac());
nty.setText("" + networkType());
mcc.setText("" + cell_mcc());
mnc.setText("" + cell_mnc());
imei.setText("" + IMEI());
cname.setText("" + networkOperator());
}
}
}
}
private int cell_id() {
GsmCellLocation cellLocation = (GsmCellLocation) telm.getCellLocation();
return cellLocation.getCid() & 0xffff;
}
private int cell_lac() {
GsmCellLocation cellLocation = (GsmCellLocation) telm.getCellLocation();
return cellLocation.getLac() & 0xffff;
}
private int cell_mcc() {
String networkOperator = telm.getNetworkOperator();
if (!TextUtils.isEmpty(networkOperator)) {
return Integer.parseInt(networkOperator.substring(0, 3));
} else {
return 0;
}
}
private int cell_mnc() {
String networkOperator = telm.getNetworkOperator();
if (!TextUtils.isEmpty(networkOperator)) {
return Integer.parseInt(networkOperator.substring(3));
} else {
return 0;
}
}
private String networkOperator() {
return telm.getNetworkOperatorName();
}
private String networkType() {
int networktype = telm.getNetworkType();
switch (networktype) {
case TelephonyManager.NETWORK_TYPE_1xRTT:
return "1xRTT";
case TelephonyManager.NETWORK_TYPE_CDMA:
return "CDMA";
case TelephonyManager.NETWORK_TYPE_EDGE:
return "EDGE";
case TelephonyManager.NETWORK_TYPE_EHRPD:
return "eHRPD";
case TelephonyManager.NETWORK_TYPE_EVDO_0:
return "EVDO rev. 0";
case TelephonyManager.NETWORK_TYPE_EVDO_A:
return "EVDO rev. A";
case TelephonyManager.NETWORK_TYPE_EVDO_B:
return "EVDO rev. B";
case TelephonyManager.NETWORK_TYPE_GPRS:
return "GPRS";
case TelephonyManager.NETWORK_TYPE_HSDPA:
return "HSDPA";
case TelephonyManager.NETWORK_TYPE_HSPA:
return "HSPA";
case TelephonyManager.NETWORK_TYPE_HSPAP:
return "HSPA+";
case TelephonyManager.NETWORK_TYPE_HSUPA:
return "HSUPA";
case TelephonyManager.NETWORK_TYPE_IDEN:
return "iDen";
case TelephonyManager.NETWORK_TYPE_LTE:
return "LTE";
case TelephonyManager.NETWORK_TYPE_UMTS:
return "UMTS";
case TelephonyManager.NETWORK_TYPE_UNKNOWN:
return "Unknown";
}
throw new RuntimeException("Unknown");
}
public String IMEI() {
TelephonyManager mngr = (TelephonyManager) getApplicationContext().getSystemService(getApplicationContext().TELEPHONY_SERVICE);
String imei = mngr.getDeviceId();
return imei;
}
}
Manifest: These permissions must be defined
<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" />
On this page we show you how to get the status of the mobile network with android. The source code and the APK file are available here
read moreOn this page we show you how to create notifications with android. The notification is displayed in the status bar and can be opened by clicking
read moreAEQ-WEB © 2015-2024 All Right Reserved