Java Code Examples for android.telephony.TelephonyManager#getLine1Number()

The following examples show how to use android.telephony.TelephonyManager#getLine1Number() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DeviceUtils.java    From Android-utils with Apache License 2.0 6 votes vote down vote up
/**
 * Return the phone status.
 * <p>Must hold
 * {@code <uses-permission android:name="android.permission.READ_PHONE_STATE" />}</p>
 *
 * @return DeviceId = 99000311726612<br>
 * DeviceSoftwareVersion = 00<br>
 * Line1Number =<br>
 * NetworkCountryIso = cn<br>
 * NetworkOperator = 46003<br>
 * NetworkOperatorName = 中国电信<br>
 * NetworkType = 6<br>
 * PhoneType = 2<br>
 * SimCountryIso = cn<br>
 * SimOperator = 46003<br>
 * SimOperatorName = 中国电信<br>
 * SimSerialNumber = 89860315045710604022<br>
 * SimState = 5<br>
 * SubscriberId(IMSI) = 460030419724900<br>
 * VoiceMailNumber = *86<br>
 */
@SuppressLint("HardwareIds")
@RequiresPermission(READ_PHONE_STATE)
public static String getPhoneStatus() {
    TelephonyManager tm =
            (TelephonyManager) UtilsApp.getApp().getSystemService(Context.TELEPHONY_SERVICE);
    String str = "";
    //noinspection ConstantConditions
    str += "DeviceId(IMEI) = " + tm.getDeviceId() + "\n";
    str += "DeviceSoftwareVersion = " + tm.getDeviceSoftwareVersion() + "\n";
    str += "Line1Number = " + tm.getLine1Number() + "\n";
    str += "NetworkCountryIso = " + tm.getNetworkCountryIso() + "\n";
    str += "NetworkOperator = " + tm.getNetworkOperator() + "\n";
    str += "NetworkOperatorName = " + tm.getNetworkOperatorName() + "\n";
    str += "NetworkType = " + tm.getNetworkType() + "\n";
    str += "PhoneType = " + tm.getPhoneType() + "\n";
    str += "SimCountryIso = " + tm.getSimCountryIso() + "\n";
    str += "SimOperator = " + tm.getSimOperator() + "\n";
    str += "SimOperatorName = " + tm.getSimOperatorName() + "\n";
    str += "SimSerialNumber = " + tm.getSimSerialNumber() + "\n";
    str += "SimState = " + tm.getSimState() + "\n";
    str += "SubscriberId(IMSI) = " + tm.getSubscriberId() + "\n";
    str += "VoiceMailNumber = " + tm.getVoiceMailNumber();
    return str;
}
 
Example 2
Source File: LogRunnerService.java    From callmeter with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Acquire {@link WakeLock} and init service.
 *
 * @param a action
 */
@SuppressLint({"MissingPermission", "HardwareIds"})
private void acquire(final String a) {
    if (TelephonyManager.ACTION_PHONE_STATE_CHANGED.equals(a) || ACTION_SMS.equals(a)) {
        Log.i(TAG, "sleep for " + WAIT_FOR_LOGS + "ms");
        try {
            Thread.sleep(WAIT_FOR_LOGS);
        } catch (InterruptedException e) {
            Log.e(TAG, "interrupted while waiting for logs", e);
        }
    }

    // update roaming info
    TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    if (tm != null) {
        roaming = tm.isNetworkRoaming();
        Log.d(TAG, "roaming: ", roaming);
        mynumber = tm.getLine1Number();
        Log.d(TAG, "my number: ", mynumber);
    }
}
 
Example 3
Source File: FindEmulator.java    From BlogDemo with Apache License 2.0 6 votes vote down vote up
public static boolean hasKnownPhoneNumber(Context context) {
    TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

    try {
        @SuppressLint("MissingPermission") String phoneNumber = telephonyManager.getLine1Number();
        for (String number : known_numbers) {
            if (number.equalsIgnoreCase(phoneNumber)) {
                return true;
            }
        }
    } catch (SecurityException exception) {
        exception.printStackTrace();
        log("Unable to request getLine1Number, failing open :" + exception.toString());
    }

    return false;
}
 
Example 4
Source File: PhoneUtils.java    From Android-UtilCode with Apache License 2.0 6 votes vote down vote up
/**
 * 获取手机状态信息
 * <p>需添加权限 {@code <uses-permission android:name="android.permission.READ_PHONE_STATE"/>}</p>
 *
 * @return DeviceId(IMEI) = 99000311726612<br>
 * DeviceSoftwareVersion = 00<br>
 * Line1Number =<br>
 * NetworkCountryIso = cn<br>
 * NetworkOperator = 46003<br>
 * NetworkOperatorName = 中国电信<br>
 * NetworkType = 6<br>
 * honeType = 2<br>
 * SimCountryIso = cn<br>
 * SimOperator = 46003<br>
 * SimOperatorName = 中国电信<br>
 * SimSerialNumber = 89860315045710604022<br>
 * SimState = 5<br>
 * SubscriberId(IMSI) = 460030419724900<br>
 * VoiceMailNumber = *86<br>
 */
@SuppressLint("HardwareIds")
public static String getPhoneStatus() {
    TelephonyManager tm = (TelephonyManager) Utils.getContext()
            .getSystemService(Context.TELEPHONY_SERVICE);
    String str = "";
    str += "DeviceId(IMEI) = " + tm.getDeviceId() + "\n";
    str += "DeviceSoftwareVersion = " + tm.getDeviceSoftwareVersion() + "\n";
    str += "Line1Number = " + tm.getLine1Number() + "\n";
    str += "NetworkCountryIso = " + tm.getNetworkCountryIso() + "\n";
    str += "NetworkOperator = " + tm.getNetworkOperator() + "\n";
    str += "NetworkOperatorName = " + tm.getNetworkOperatorName() + "\n";
    str += "NetworkType = " + tm.getNetworkType() + "\n";
    str += "PhoneType = " + tm.getPhoneType() + "\n";
    str += "SimCountryIso = " + tm.getSimCountryIso() + "\n";
    str += "SimOperator = " + tm.getSimOperator() + "\n";
    str += "SimOperatorName = " + tm.getSimOperatorName() + "\n";
    str += "SimSerialNumber = " + tm.getSimSerialNumber() + "\n";
    str += "SimState = " + tm.getSimState() + "\n";
    str += "SubscriberId(IMSI) = " + tm.getSubscriberId() + "\n";
    str += "VoiceMailNumber = " + tm.getVoiceMailNumber() + "\n";
    return str;
}
 
Example 5
Source File: TelephonyUtil.java    From MissZzzReader with Apache License 2.0 6 votes vote down vote up
public static String getNum1(Context context) {
    String tel = "";
    String IMSI;
    try {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_NUMBERS) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {

            tel = tm.getLine1Number();//手机号码
            Class clazz = tm.getClass();
            Method getPhoneNumber = clazz.getDeclaredMethod("getLine1NumberForSubscriber", int.class);
            String tel0 = (String) getPhoneNumber.invoke(tm, 0);
            String tel1 = (String) getPhoneNumber.invoke(tm, 1);

            IMSI = tm.getSubscriberId();
            TextHelper.showText(IMSI);
        }

    } catch (Exception e) {
        e.printStackTrace();
        tel = "";
    }


    return tel;
}
 
Example 6
Source File: TelephonyUtil.java    From MissZzzReader with Apache License 2.0 6 votes vote down vote up
public static String getNum2(Context context) {
    String tel = "";
    try {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_NUMBERS) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            tel = tm.getLine1Number();//手机号码
        }


    }catch (Exception e){
        e.printStackTrace();
        tel = "";
    }

    return tel;
}
 
Example 7
Source File: AssistantActivity.java    From linphone-android with GNU General Public License v3.0 5 votes vote down vote up
String getDevicePhoneNumber() {
    try {
        TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        return tm.getLine1Number();
    } catch (Exception e) {
        Log.e("[Assistant] " + e);
    }
    return null;
}
 
Example 8
Source File: HelperMethods.java    From PhoneMonitor with GNU General Public License v3.0 5 votes vote down vote up
static String getDeviceNUID(Context context) {
    TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    String line1phonenumber = "";
    if (telephonyManager != null) {
        try {
            line1phonenumber = telephonyManager.getLine1Number();
        } catch (SecurityException secx) {

        }
    }
    return line1phonenumber + "_" + Build.MANUFACTURER + "_" + Build.MODEL;
}
 
Example 9
Source File: EmuDetector.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
private boolean checkPhoneNumber() {
    TelephonyManager telephonyManager = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);

    @SuppressLint("HardwareIds") String phoneNumber = telephonyManager.getLine1Number();

    for (String number : PHONE_NUMBERS) {
        if (number.equalsIgnoreCase(phoneNumber)) {
            return true;
        }
    }
    return false;
}
 
Example 10
Source File: TelephoneUtil.java    From AndroidBase with Apache License 2.0 5 votes vote down vote up
/**
 * 获取当前设置的电话号码
 */
public static String getNativePhoneNumber(Context context) {
    TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    String NativePhoneNumber = null;
    NativePhoneNumber = telephonyManager.getLine1Number();
    return String.format("手机号: %s", NativePhoneNumber);
}
 
Example 11
Source File: EmuDetector.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
private boolean checkPhoneNumber() {
    TelephonyManager telephonyManager = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);

    @SuppressLint("HardwareIds") String phoneNumber = telephonyManager.getLine1Number();

    for (String number : PHONE_NUMBERS) {
        if (number.equalsIgnoreCase(phoneNumber)) {
            return true;
        }
    }
    return false;
}
 
Example 12
Source File: Utils.java    From log with Apache License 2.0 4 votes vote down vote up
public static String getMobile(Context context) {
	TelephonyManager telephonyManager = (TelephonyManager) context
			.getSystemService(Context.TELEPHONY_SERVICE);
	return telephonyManager.getLine1Number();
}
 
Example 13
Source File: UserInfo.java    From echo with GNU General Public License v3.0 4 votes vote down vote up
public static String getUserPhoneNumber(Context c) {
    final TelephonyManager tMgr = (TelephonyManager)c.getSystemService(Context.TELEPHONY_SERVICE);
    return tMgr.getLine1Number();
}
 
Example 14
Source File: ApigeeActiveSettings.java    From apigee-android-sdk with Apache License 2.0 4 votes vote down vote up
private boolean matchesDeviceLevelFilter(ApigeeApp config) {
	if (config.getDeviceLevelOverrideEnabled()) {
		try {
			TelephonyManager telephonyManager = (TelephonyManager) appActivity
					.getSystemService(Context.TELEPHONY_SERVICE);

			String imeiDeviceId = telephonyManager.getDeviceId();

			String android_id = Secure.getString(
					appActivity.getContentResolver(), Secure.ANDROID_ID);
			
			Log.v(ClientLog.TAG_MONITORING_CLIENT, "Trying to match against device ID / IMEI ID : " + android_id + " / " + imeiDeviceId);

			String devicePhoneNumber = telephonyManager.getLine1Number();

			if (findRegexMatch(config.getDeviceIdFilters(), imeiDeviceId)
					|| findRegexMatch(config.getDeviceIdFilters(), android_id)) {
				Log.v(ClientLog.TAG_MONITORING_CLIENT,
						"Found device ID or imei id match");
				return true;
			}

			if (findRegexMatch(config.getDeviceNumberFilters(),
					devicePhoneNumber)) {
				Log.v(ClientLog.TAG_MONITORING_CLIENT,
						"Found telephone number match");
				return true;
			}
		}
		catch (SecurityException e)
		{
			Log.w(ClientLog.TAG_MONITORING_CLIENT,"Security caught. AndroidManifest not configured to read phone state permissions : " + 
					e.getMessage());
			return false;
		}
	}
	else
	{
		Log.v(ClientLog.TAG_MONITORING_CLIENT, "Device Level override not enabled ");
	}
	
	Log.v(ClientLog.TAG_MONITORING_CLIENT, "Did not find Device Level Match");
	return false;
}
 
Example 15
Source File: PhoneUtils.java    From Ticket-Analysis with MIT License 4 votes vote down vote up
/**
 * 获取手机状态信息
 * <p>需添加权限<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
 * <p>返回如下
 * <pre>
 * DeviceId(IMEI) = 99000311726612
 * DeviceSoftwareVersion = 00
 * Line1Number =
 * NetworkCountryIso = cn
 * NetworkOperator = 46003
 * NetworkOperatorName = 中国电信
 * NetworkType = 6
 * honeType = 2
 * SimCountryIso = cn
 * SimOperator = 46003
 * SimOperatorName = 中国电信
 * SimSerialNumber = 89860315045710604022
 * SimState = 5
 * SubscriberId(IMSI) = 460030419724900
 * VoiceMailNumber = *86
 * <pre/>
 */
public static String getPhoneStatus(Context context) {
    TelephonyManager tm = (TelephonyManager) context
            .getSystemService(Context.TELEPHONY_SERVICE);
    String str = "";
    str += "DeviceId(IMEI) = " + tm.getDeviceId() + "\n";
    str += "DeviceSoftwareVersion = " + tm.getDeviceSoftwareVersion() + "\n";
    str += "Line1Number = " + tm.getLine1Number() + "\n";
    str += "NetworkCountryIso = " + tm.getNetworkCountryIso() + "\n";
    str += "NetworkOperator = " + tm.getNetworkOperator() + "\n";
    str += "NetworkOperatorName = " + tm.getNetworkOperatorName() + "\n";
    str += "NetworkType = " + tm.getNetworkType() + "\n";
    str += "honeType = " + tm.getPhoneType() + "\n";
    str += "SimCountryIso = " + tm.getSimCountryIso() + "\n";
    str += "SimOperator = " + tm.getSimOperator() + "\n";
    str += "SimOperatorName = " + tm.getSimOperatorName() + "\n";
    str += "SimSerialNumber = " + tm.getSimSerialNumber() + "\n";
    str += "SimState = " + tm.getSimState() + "\n";
    str += "SubscriberId(IMSI) = " + tm.getSubscriberId() + "\n";
    str += "VoiceMailNumber = " + tm.getVoiceMailNumber() + "\n";
    return str;
}
 
Example 16
Source File: MainActivity.java    From sana.mobile with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Retrieves the device phone number using the TelephonyManager
 *
 * @return the device phone number
 */
public String getPhoneNumber() {
    TelephonyManager tMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    String phoneNumber = tMgr.getLine1Number();
    return phoneNumber;
}
 
Example 17
Source File: PhoneNumberUtils.java    From android-utilset with Apache License 2.0 4 votes vote down vote up
public String getMobilePhoneNumber(Context context) {
	TelephonyManager telManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); 
	return telManager.getLine1Number();
}
 
Example 18
Source File: MyDialerActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 4 votes vote down vote up
private void listing20_6() {
  String srvcName = Context.TELEPHONY_SERVICE;
  TelephonyManager telephonyManager = (TelephonyManager)getSystemService(srvcName);

  // Listing 20-6: Accessing phone-type and the device’s phone number
  String phoneTypeStr = "unknown";

  int phoneType = telephonyManager.getPhoneType();

  switch (phoneType) {
    case (TelephonyManager.PHONE_TYPE_CDMA):
      phoneTypeStr = "CDMA";
      break;
    case (TelephonyManager.PHONE_TYPE_GSM) :
      phoneTypeStr = "GSM";
      break;
    case (TelephonyManager.PHONE_TYPE_SIP):
      phoneTypeStr = "SIP";
      break;
    case (TelephonyManager.PHONE_TYPE_NONE):
      phoneTypeStr = "None";
      break;
    default: break;
  }

  Log.d(TAG, phoneTypeStr);

  // -- These require READ_PHONE_STATE uses-permission --
  int permission = ActivityCompat.checkSelfPermission(this,
    android.Manifest.permission.READ_PHONE_STATE);
  if (permission == PackageManager.PERMISSION_GRANTED) {

    // Read the IMEI for GSM or MEID for CDMA
    String deviceId = telephonyManager.getDeviceId();

    // Read the software version on the phone (note -- not the SDK version)
    String softwareVersion = telephonyManager.getDeviceSoftwareVersion();

    // Get the phone's number (if available)
    String phoneNumber = telephonyManager.getLine1Number();

    // If permission hasn't been granted, request it.
  } else {
    if (ActivityCompat.shouldShowRequestPermissionRationale(
      this, android.Manifest.permission.READ_PHONE_STATE)) {
      // TODO Display additional rationale for the requested permission.
    }
    ActivityCompat.requestPermissions(this,
      new String[]{android.Manifest.permission.READ_PHONE_STATE},
      PHONE_STATE_PERMISSION_REQUEST);
  }
}
 
Example 19
Source File: CancelAccountDeletionActivity.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void onNextPressed() {
    if (getParentActivity() == null || nextPressed) {
        return;
    }
    TelephonyManager tm = (TelephonyManager) ApplicationLoader.applicationContext.getSystemService(Context.TELEPHONY_SERVICE);
    boolean simcardAvailable = tm.getSimState() != TelephonyManager.SIM_STATE_ABSENT && tm.getPhoneType() != TelephonyManager.PHONE_TYPE_NONE;
    boolean allowCall = true;
    if (Build.VERSION.SDK_INT >= 23 && simcardAvailable) {
        //allowCall = getParentActivity().checkSelfPermission(Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED;
        //boolean allowSms = getParentActivity().checkSelfPermission(Manifest.permission.RECEIVE_SMS) == PackageManager.PERMISSION_GRANTED;
        /*if (checkPermissions) {
            permissionsItems.clear();
            if (!allowCall) {
                permissionsItems.add(Manifest.permission.READ_PHONE_STATE);
            }
            if (!allowSms) {
                permissionsItems.add(Manifest.permission.RECEIVE_SMS);
            }
            if (!permissionsItems.isEmpty()) {
                SharedPreferences preferences = MessagesController.getGlobalMainSettings();
                if (preferences.getBoolean("firstlogin", true) || getParentActivity().shouldShowRequestPermissionRationale(Manifest.permission.READ_PHONE_STATE) || getParentActivity().shouldShowRequestPermissionRationale(Manifest.permission.RECEIVE_SMS)) {
                    preferences.edit().putBoolean("firstlogin", false).commit();
                    AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity());
                    builder.setTitle(LocaleController.getString("AppName", R.string.AppName));
                    builder.setPositiveButton(LocaleController.getString("OK", R.string.OK), null);
                    if (permissionsItems.size() == 2) {
                        builder.setMessage(LocaleController.getString("AllowReadCallAndSms", R.string.AllowReadCallAndSms));
                    } else if (!allowSms) {
                        builder.setMessage(LocaleController.getString("AllowReadSms", R.string.AllowReadSms));
                    } else {
                        builder.setMessage(LocaleController.getString("AllowReadCall", R.string.AllowReadCall));
                    }
                    permissionsDialog = showDialog(builder.create());
                } else {
                    getParentActivity().requestPermissions(permissionsItems.toArray(new String[permissionsItems.size()]), 6);
                }
                return;
            }
        }*/
    }

    final TLRPC.TL_account_sendConfirmPhoneCode req = new TLRPC.TL_account_sendConfirmPhoneCode();
    req.allow_flashcall = false;//simcardAvailable && allowCall;
    req.hash = hash;
    if (req.allow_flashcall) {
        try {
            @SuppressLint("HardwareIds") String number = tm.getLine1Number();
            if (!TextUtils.isEmpty(number)) {
                req.current_number = phone.contains(number) || number.contains(phone);
                if (!req.current_number) {
                    req.allow_flashcall = false;
                }
            } else {
                req.current_number = false;
            }
        } catch (Exception e) {
            req.allow_flashcall = false;
            FileLog.e(e);
        }
    }

    final Bundle params = new Bundle();
    params.putString("phone", phone);
    nextPressed = true;
    ConnectionsManager.getInstance(currentAccount).sendRequest(req, new RequestDelegate() {
        @Override
        public void run(final TLObject response, final TLRPC.TL_error error) {
            AndroidUtilities.runOnUIThread(new Runnable() {
                @Override
                public void run() {
                    nextPressed = false;
                    if (error == null) {
                        fillNextCodeParams(params, (TLRPC.TL_auth_sentCode) response);
                    } else {
                        errorDialog = AlertsCreator.processError(currentAccount, error, CancelAccountDeletionActivity.this, req);
                    }
                }
            });
        }
    }, ConnectionsManager.RequestFlagFailOnServerErrors);
}
 
Example 20
Source File: DeviceUtil.java    From DoingDaily with Apache License 2.0 2 votes vote down vote up
/**
 * 获取手机号码
 *
 * @param context
 * @return
 */
public static String getPhoneNumber(Context context) {
    TelephonyManager phoneMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    String number = phoneMgr.getLine1Number();
    return number;
}