在我的应用程序中,我想获取用户的SIM号码(如果有的话),我尝试了以下代码,但它返回""
const format = 'YYYY-MM-DD hh:mm:ss a';
所需权限
TelephonyManager tMgr =
(TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();
我到处寻找一种方法来获取用户的SIM卡号,但对我来说没什么用。有些人说可能是用户的SIM卡提供商对此功能有限制,但是我使用imo应用程序获取我的SIM号并在应用程序中显示,所以我在同一个手机上尝试了不同的方法,但没有任何作用,如果有人知道工作方法,请告诉我
答案 0 :(得分:1)
第1步:在AndroidManifest.xml中添加必需的权限
<uses-permission android:name="android.permission.READ_PHONE_STATE">
</uses-permission>
注意:对于6.0(棉花糖)及以上设备使用Run Rime Permissions
第2步:
TelephonyManager telephonyManager = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
String sim1= telephonyManager.getLine1Number();
String sim2= telephonyManager.getLine2Number();
检查this以获取更多信息
答案 1 :(得分:1)
首先,我建议你应该使用共享偏好,只需存储它并在你的应用程序中随时随地使用它。这是我猜的最简单的方法。
或强>
尝试登记
电话 - &GT;设置 - &gt;关于 - &gt;
电话身份,如果你可以在那里查看号码,那么你可以获得号码。但如果您无法在设置中查看电话号码,那么您将无法通过此代码获取。
答案 2 :(得分:1)
在Manifest中启用权限,获取您的SMI号码和Imei号码
Manifest.Java
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
MainActivity.Java
public class MainActivity extends AppCompatActivity {
Activity mActivity;
String mStatus;
private TextView mSmiTxt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mActivity = MainActivity.this;
mSmiTxt = (TextView) this.findViewById(R.id.smitxt);
mStatus=loadSMI1();
mSmiTxt.setText(mStatus);
/** Fading Transition Effect */
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
}
/**
* Called when the 'loadIMEI' function is triggered.
*/
public void loadIMEI() {
// Check if the READ_PHONE_STATE permission is already available.
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE)
!= PackageManager.PERMISSION_GRANTED) {
// READ_PHONE_STATE permission has not been granted.
requestReadPhoneStatePermission();
} else {
// READ_PHONE_STATE permission is already been granted.
loadImei();
}
}
public String getDeviceUniqueID() {
String device_unique_id = Settings.Secure.getString(getContentResolver(),
Settings.Secure.ANDROID_ID);
return device_unique_id;
}
private void requestReadPhoneStatePermission() {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.READ_PHONE_STATE)) {
// Provide an additional rationale to the user if the permission was not granted
// and the user would benefit from additional context for the use of the permission.
// For example if the user has previously denied the permission.
new AlertDialog.Builder(MainActivity.this)
.setTitle("Permission Request")
.setMessage(getString(R.string.permission_read_phone_state_rationale))
.setCancelable(false)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//re-request
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.READ_PHONE_STATE},
MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);
}
})
.setIcon(R.drawable.error)
.show();
} else {
// READ_PHONE_STATE permission has not been granted yet. Request it directly.
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE},
MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);
}
}
/**
* Callback received when a permissions request has been completed.
*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
if (requestCode == MY_PERMISSIONS_REQUEST_READ_PHONE_STATE) {
// Received permission result for READ_PHONE_STATE permission.est.");
// Check if the only required permission has been granted
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// READ_PHONE_STATE permission has been granted, proceed with displaying IMEI Number
//alertAlert(getString(R.string.permision_available_read_phone_state));
loadImei();
} else {
alertAlert(getString(R.string.permissions_not_granted_read_phone_state));
}
}
}
public String loadImei() {
//Have an object of TelephonyManager
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
//Get IMEI Number of Phone //////////////// for this example i only need the IMEI
String IMEINumber = tm.getDeviceId();
return IMEINumber;
}
private void alertAlert(String msg) {
new AlertDialog.Builder(MainActivity.this)
.setTitle("Permission Request")
.setMessage(msg)
.setCancelable(false)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do somthing here
}
})
.setIcon(R.drawable.error)
.show();
}
public String loadSMI1() {
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String sim1 = tm.getLine1Number();
return sim1;
}
}
如果SMI编号返回null,请从whatsapp
获取<uses-permission android:name="android.permission.GET_ACCOUNTS" />
AccountManager am = AccountManager.get(this);
Account[] accounts = am.getAccounts();
ArrayList<String> googleAccounts = new ArrayList<String>();
for (Account ac : accounts) {
String acname = ac.name;
String actype = ac.type;
// Take your time to look at all available accounts
System.out.println("Accounts : " + acname + ", " + actype);
}
if(actype.equals("com.whatsapp")){
String phoneNumber = ac.name;
}