Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- myBtn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if(ContextCompat.checkSelfPermission(MainActivity.this,
- Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED)
- {} else {
- requestReadPhonePermission();
- }
- };// move the things you do with the telephony in the callback;
- }
- @Override
- public void onRequestPermissionsResult(int requestCode,
- String permissions[], int[] grantResults) {
- switch (requestCode) {
- case Phone_State_Permission_Code: {
- // 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.
- TelephonyManager tm=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
- String IMEInum;
- IMEInum=tm.getDeviceId();
- IMEI.setText("IMEI: "+IMEInum);
- String lineNum = tm.getLine1Number();
- Linenum.setText("Line number: "+lineNum);
- int phoneType=tm.getPhoneType();
- switch (phoneType)
- {
- case (TelephonyManager.PHONE_TYPE_CDMA):
- Phonetype.setText("Phone type: CDMA");
- break;
- case (TelephonyManager.PHONE_TYPE_GSM):
- Phonetype.setText("Phone type: GSM");
- break;
- case (TelephonyManager.PHONE_TYPE_NONE):
- Phonetype.setText("Phone type: NONE");
- break;
- }
- int CallState = tm.getCallState();
- switch (CallState) {
- case TelephonyManager.CALL_STATE_IDLE:
- Call.setText("CALL STATE : IDLE");
- break;
- case TelephonyManager.CALL_STATE_OFFHOOK:
- Call.setText("CALL STATE : OFFHOOK");
- break;
- case TelephonyManager.CALL_STATE_RINGING:
- Call.setText("CALL STATE : RINGING");
- break;
- }
- int SIMState=tm.getSimState();
- switch(SIMState)
- {
- case TelephonyManager.SIM_STATE_ABSENT :
- Sim.setText("SIM state: ABSENT");
- break;
- case TelephonyManager.SIM_STATE_NETWORK_LOCKED :
- Sim.setText("SIM state: NETWORK LOCKED");
- break;
- case TelephonyManager.SIM_STATE_PIN_REQUIRED :
- Sim.setText("SIM state: PIN REQUIRED");
- break;
- case TelephonyManager.SIM_STATE_PUK_REQUIRED :
- Sim.setText("SIM state: PUK REQUIRED");
- break;
- case TelephonyManager.SIM_STATE_READY :
- Sim.setText("SIM state: READY");
- break;
- case TelephonyManager.SIM_STATE_UNKNOWN :
- Sim.setText("SIM state: UNKNOWN");
- break;
- }
- } else {
- // permission denied, boo! Disable the
- // functionality that depends on this permission.
- //handle the situation if the permission was denied;
- }
- return;
- }
- // other 'case' lines to check for other
- // permissions this app might request
- }
- }
Add Comment
Please, Sign In to add comment