LoRa Packet Forwarder JSON Daten
25.12.2024
Elektronik | Funk | Software
Der Technik-Blog
Auf dieser Seite zeigen wir, wie mit Android Informationen vom Mobilfunk-Netz ausgelesen werden können. Diese Funktionen benötigen einen Zugriff auf den Telefonstatus sowie einen Zugriff auf die Ortungsdienste. Sollte dies nicht möglich sein, so wird der Nutzer über eine Toast-Message informiert, da die App die Berechtigungen im Vorhinein überprüft.
Android Version | API Version |
---|---|
4.0.3 | 15 |
MainAcitvity.java: Dieser Code befindet sich in der 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: Diese Rechte müssen in der Manifest definiert werden
<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" />
Auf dieser Seite zeigen wir, wie mit Android die GPS-Position, Geschwindigkeit & Seehöhe sowie der Name der Ortschaft ermittelt werden kann.
WeiterlesenAuf dieser Seite zeigen wir, wie unter Android ein asynchroner HTTP/HTTPS Request gestartet wird und mittels Task verarbeitet wird.
WeiterlesenAEQ-WEB © 2015-2024 All Right Reserved