TelephonyManager.getDeviceId only returns 14 digit - android

TelephonyManager.getDeiceId() only return 14 digits of IMEI number.
But under Setting->Phone->Status show 15 digits.
I want to get 15 digit as appear under phone settings.

The IMEI (14 digits) is complemented by a check digit. The check digit is not
part of the digits transmitted at IMEI check occasions. The Check Digit shall
avoid manual transmission errors, e.g. when customers register stolen mobiles
at the operator's customer care desk.
http://www.tele-servizi.com/Janus/texts/imei.txt
http://en.wikipedia.org/wiki/International_Mobile_Equipment_Identity

TelephonyManager mTelephonyMgr;
mTelephonyMgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
mTelephonyMgr.getDeviceId()
This is what i have done and i getting all the 15 digits may this can help u ...

Or you can manually calculate the check-sum of the 14-digit IMEI number. e.g.
private int GetCheckSumDigit(String id) {
int digit = -1;
try{
if(id.length() == 14)
{
String str = "";
char[] digits = new char[id.length()];
id.getChars(0, id.length(), digits, 0);
for(int i=0; i<digits.length; i++)
{
String ch = digits[i]+"";
if((i+1)%2==0)
{
int x = Integer.parseInt(digits[i]+"");
x *= 2;
ch = x+"";
}
str += ch;
}
digits = new char[str.length()];
str.getChars(0, str.length(), digits, 0);
int total = 0;
for(int i=0; i<str.length(); i++)
total += Integer.parseInt(digits[i]+"");
//
int count = 0;
while((total+count)%10 != 0)
count++;
digit = count;
}
}catch(Exception exx)
{
exx.printStackTrace();
}
return digit;
}
Good Luck.

Some devices don't add last digit, we need to calculate last digit instead using Luhn algorithm:
private int getImeiCheckDigit(String imei14digits) {
if (imei14digits == null || imei14digits.length() != 14) {
throw new IllegalArgumentException("IMEI should be 14 digits");
}
int[] imeiArray = new int[imei14digits.length()];
final int DIVIDER = 10;
char[] chars = imei14digits.toCharArray();
for (int i = 0; i < chars.length; i++) {
imeiArray[i] = Character.getNumericValue(chars[i]);
}
int sum = 0;
for (int i = 0; i < imeiArray.length; i++) {
if (i % 2 == 0) {
sum += imeiArray[i];
} else {
int multi = imeiArray[i] * 2;
if (multi >= DIVIDER) {
sum += multi % DIVIDER;
sum += multi / DIVIDER;
} else {
sum += multi;
}
}
}
return (DIVIDER - sum % DIVIDER) % DIVIDER;
}

Related

Android - How to get edittext as in this xx-xxx-xxx-xxx-x format?

I want to show the number in this xx-xxx-xxx-xxx-x format on EditText.
Eg (01-140-176-515-4)
I tried modifying the below code which displays the number in credit card number format
(xxxx-xxxx-xxxx-xxxx)
et_cardnumber.addTextChangedListener(new TextWatcher() {
private static final int TOTAL_SYMBOLS = 19; // size of pattern 0000-0000-0000-0000
private static final int TOTAL_DIGITS = 16; // max numbers of digits in pattern: 0000 x 4
private static final int DIVIDER_MODULO = 5; // means divider position is every 5th symbol beginning with 1
private static final int DIVIDER_POSITION = DIVIDER_MODULO - 1; // means divider position is every 4th symbol beginning with 0
private static final char DIVIDER = '-';
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// noop
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
iv_cardtype.setImageResource(getCreditCardTypeForImageView(et_cardnumber.getText().toString()));
}
#Override
public void afterTextChanged(Editable s) {
if (!isInputCorrect(s, TOTAL_SYMBOLS, DIVIDER_MODULO, DIVIDER)) {
s.replace(0, s.length(), buildCorrecntString(getDigitArray(s, TOTAL_DIGITS), DIVIDER_POSITION, DIVIDER));
}
}
private boolean isInputCorrect(Editable s, int totalSymbols, int dividerModulo, char divider) {
boolean isCorrect = s.length() <= totalSymbols; // check size of entered string
for (int i = 0; i < s.length(); i++) { // chech that every element is right
if (i > 0 && (i + 1) % dividerModulo == 0) {
isCorrect &= divider == s.charAt(i);
} else {
isCorrect &= Character.isDigit(s.charAt(i));
}
}
return isCorrect;
}
private String buildCorrecntString(char[] digits, int dividerPosition, char divider) {
final StringBuilder formatted = new StringBuilder();
for (int i = 0; i < digits.length; i++) {
if (digits[i] != 0) {
formatted.append(digits[i]);
if ((i > 0) && (i < (digits.length - 1)) && (((i + 1) % dividerPosition) == 0)) {
formatted.append(divider);
}
}
}
return formatted.toString();
}
private char[] getDigitArray(final Editable s, final int size) {
char[] digits = new char[size];
int index = 0;
for (int i = 0; i < s.length() && index < size; i++) {
char current = s.charAt(i);
if (Character.isDigit(current)) {
digits[index] = current;
index++;
}
}
return digits;
}
});
I couldn't get it right when i make changes to get the format which i want.
Can anyone help me to get the number in xx-xxx-xxx-xxx-x format?
i dont know much in Android , but try to build something like this .
str Yourstring = "";
for (int i = 0; i < digits.length; i++) {
if (i == 2 || i == 5 || i == 8 || i == 11) {
Yourstring = Yourstring + "-" +digits[i];
}
else
{
Yourstring = Yourstring +digits[i];
}
}
I answered this already in this link , please change the logic according to your format
Use this library.
it will allow according yo your formate
EditText Pattern lib

Android hide numbers like password

I want to know how to hide some numbers in TextView and some are shown just like that
(****-****-1234)
Thank you
Try This Method...
public static String StrRpl(String str) {
char[] chars = str.toCharArray();
for (int i = 0, j = 0; i < chars.length && j < 5; i++) {
char ch = chars[i];
if (!Character.isWhitespace(ch)) {
chars[i] = '*';
j++;
}
}
str = new String(chars);
return str;
}
Output : *****234
pass a string to this method and it will return string with '*' character till first 5 characters (You can change your number of count.. current is 5)

decimal to ascii android

I am attempting to implement a conversion from a decimal number to ascii. Right now, i've hard coded it to 50 but I will use userinput later on. When I run the application and call the method, what is outputed is random (it changes everytime I press it) eg "[C#4263f600"
I followed the process in the link: decimal to binary. Why am I getting such a weird output?
//method
if (valid) {
String str = ascii();
mTextOutput.setText(str);
...
public String ascii(){
char[] binary_reverse = new char[9];
char[] binary = new char[9];
int ascii = 50;
int y = 0;
while (ascii !=1) {
if (ascii % 2 ==0)
{
binary_reverse[y]='0';
}
else if (ascii % 2 == 1)
{
binary_reverse[y]='1';
}
ascii /= 2;
y++;
}
if (ascii ==1)
{
binary_reverse[y] = '1';
}
if (y<8) {
for(; y < 8; y++) {
binary_reverse[y] = '0';
}
}
for (int z = 0; z < 8; z++) {
binary[z] = binary_reverse[7-1];
}
String str = binary.toString();
return str;
}
//You can try this :
binary[z] = binary_reverse[7-1]; - So you want to set every element of binary to the value in binary_reverse[6]
//and also
String str = binary.toString(); to String str = new String(binary);
reference : #Tim

Generate random numbers less than 9

I want to generate random number in two field between range 1-8 and also field sum should be less than 9.
what I've done done till now
for (int i; i <= 6; i++) {
fieldOne = rand.nextInt(9 - 5) + 5;
fieldTwo = rand.nextInt(9 - 5) + 5;
fieldSum = fieldOne + fieldTwo;
System.out.print(fieldSum); // should be < 9 and not repetition
}
but fieldSum become greater then 9 so How it is control this condition?
And Sequence should be random should not repeat 2 or more time.
rand.nextInt(9-5) won't calculate a random number between 5 and 8, it will make the operation 9 minus 5 and then will calculate rand.nextInt(4).
To calculate a random number between 5 and 8 you have to do something like this:
Random r = new Random();
for (int i = 0; i < 10; i++) {
int rand1 = r.nextInt(4) + 5;
int rand2 = r.nextInt(4) + 5;
int result = rand1 + rand2;
Log.v("", "" + result);
}
The problem is that result can't be < than 9 because you are summing two numbers that are => 5 so the lower result you can get is 10.
Edit for a solution with no repetition:
private List<Integer> rdmNumList = new ArrayList<Integer>();
private final int leftLimitRange = 1;
private final int rightLimitRange = 10;
private final int numOfIteration = 10;
public void generateNumList() {
for (int i = leftLimitRange; i <= rightLimitRange; i++) {
this.rdmNumList.add(num);
}
}
public int getRandomNumer() {
Random r = new Random();
int rNum = r.nextInt(rdmNumList.size());
int result = this.rdmNumList.get(rNum);
this.rdmNumList.remove(rNum);
return result;
}
public void test() {
this.generateNumList();
if(this.numOfIteration <= ((this.rightLimitRange - this.leftLimitRange + 1))) {
for (int i = 0; i < this.numOfIteration; i++) {
Log.v("Debug Output", String.valueOf(this.getRandomNumer()));
}
}
}
Note: this is efficient only with small range of number
This code works but it's far from being good. You should find some other solution by yourself. This answer will help you find your way
Maybe try this --> random.nextInt()%9; another way Get random nuber with random.nextInt(9).
Try,
fieldSum=(fieldOne+fieldTwo)%9;
This will def

How add "-" before each capital letter in a string

How can i add "-" before each capital letter of my string apart from first capital letter of my string.
I have a string like this "HelloWorldMyNameIsCarl" and i am using this
"HelloWorldMyNameIsCarl".replaceAll("(.)(\\p{Lu})", "$1-$2")
it's working fine.
solution is
"Hello_World_My_Name_Is_Carl"
but for "THisForNEWTest" it's not working and solution is
"T-His-For-NEw-Test"
But i want
"T-His-For-N-Ew-Test"
please suggest me what i do for this problem.
thanks.
if there is a too complex problem for regular expressions, you can always use normal programming. it might even be a little bit more efficient :
public static String doIt(String input)
{
int size=input.length();
if(size==0)
return "";
StringBuilder sb=new StringBuilder(size);
sb.append(input.charAt(0));
for(int i=1;i<size;++i)
{
char c=input.charAt(i);
if(Character.isUpperCase(c))
sb.append('-');
sb.append(c);
}
return sb.toString();
}
in any case, for regular expressions tests, you can check out this website.
so, for regular expressions, the solution can be:
return input.charAt(0)+input.substring(1).replaceAll("(\\p{Lu})","-$1");
Why aren't you just doing this:
replaceAll("(\\p{Lu})", "-$1").replaceAll("^-", "")
Try Below Code:
String test = "THisForNEwTest";
int size = test.length();
StringBuffer sb = new StringBuffer();
if(size!=0)
sb.append(test.charAt(0));
for (int i = 1; i < size; i++) {
if(Character.isUpperCase(test.charAt(i))){
sb.append("-"+test.charAt(i));
}else{
sb.append(test.charAt(i));
}
}
System.out.println("result is::::"+sb.toString());
Try this:
public static String function(String str) {
StringBuilder result = new StringBuilder();
List<Integer> capital = new ArrayList<Integer>();
for (int i = 0; i < str.length(); i++)
if (Character.isUpperCase(str.charAt(i)))
capital.add(i);
int capIndex = 0;
int x = 0;
for (int y = 0; y < str.length(); y++) {
if(x < capital.size())
capIndex = capital.get(x);
if (y == 0) {
result.append(str.charAt(y));
x++;
} else if(y == capIndex){
result.append("-" + str.charAt(y));
x++;
} else {
if(str.charAt(y) != ' ')
result.append(str.charAt(y));
}
}
return result.toString();
}

Categories

Resources