Android phone name java

How do I get Android Device ID or IMEI?

The following code snippets shows you how to get a Device ID of an Android phone. To be able to read the Device ID you need to update the AndroidManifest.xml file and add the READ_PHONE_STATE permission.

package org.kodejava.android; import android.Manifest; import android.content.Context; import android.content.pm.PackageManager; import android.os.Build; import android.provider.Settings; import android.telephony.TelephonyManager; import android.util.Log; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; public class MainActivity extends AppCompatActivity < private static final int PERMISSIONS_READ_PHONE_STATE = 1; @Override protected void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) < ActivityCompat.requestPermissions(this, new String[], PERMISSIONS_READ_PHONE_STATE); > String deviceId = null; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) < deviceId = Settings.Secure.getString(getApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID); >else < TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); if (telephonyManager.getImei() != null) < deviceId = telephonyManager.getImei(); >else if (telephonyManager.getMeid() != null) < deviceId = telephonyManager.getMeid(); >> Log.d("MyApp", "Device ID: " + deviceId); > > 

A programmer, recreational runner and diver, live in the island of Bali, Indonesia. Programming in Java, Spring, Hibernate / JPA. You can support me working on this project, buy me a cup of coffee ☕ every little bit helps, thank you 🙏

Источник

How to get programmatically android device name?

This example demonstrate about How to get programmatically android device name.

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Читайте также:  Тег BUTTON, атрибут disabled

Step 2 − Add the following code to res/layout/activity_main.xml.

In the above code, we have taken text view to show device name.

Step 3 − Add the following code to src/MainActivity.java

package com.example.myapplication; import android.Manifest; import android.app.ProgressDialog; import android.content.pm.PackageManager; import android.os.Build; import android.os.Bundle; import android.support.annotation.RequiresApi; import android.support.v4.app.ActivityCompat; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.webkit.CookieManager; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends AppCompatActivity < TextView textView; @RequiresApi(api = Build.VERSION_CODES.P) @Override protected void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = findViewById(R.id.text); if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) < ActivityCompat.requestPermissions(this, new String[], 101); > > @RequiresApi(api = Build.VERSION_CODES.O) @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) < switch (requestCode) < case 101: if (grantResults[0] = = PackageManager.PERMISSION_GRANTED) < if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) < return; >textView.setText(Build.DEVICE); > else < //not granted >break; default: super.onRequestPermissionsResult(requestCode, permissions, grantResults); > > @RequiresApi(api = Build.VERSION_CODES.O) @Override protected void onResume() < super.onResume(); if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) < return; >textView.setText(Build.DEVICE); > >

Step 4 − Add the following code to AndroidManifest.xml

Let’s try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project’s activity files and click Run icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen —

Click here to download the project code

Источник

Получить номер телефона в Android с помощью TelephonyManager

Следуйте за нами на нашей фан-странице, чтобы получать уведомления каждый раз, когда появляются новые статьи. Facebook

1- Android Phone number

Класс android.telephony.TelephonyManager предоставляет информацию телефонных услуг (telephony services) как ID, EMEI Number, SIM Serial Number, Phone Network Type. Помимо этого он предоставляет информацию статуса телефона.

В данной статье я покажу вам как использовать TelephonyManager, чтобы получить номер телефона устройства Android и другую полезную информацию.

Нужно разрешить приложению просматривать статус телефона, поэтому вам нужно добавить следующие разрешения (permission) в файл AndroidManifest.xml:

Примечание: От Android 6.0 (API Level 23), приложение хочет знать статус телефона, ему нужно спросить разрешение пользователя.

 package org.o7planning.getphonenumberexample; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import android.Manifest; import android.annotation.SuppressLint; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.os.Bundle; import android.telephony.SignalStrength; import android.telephony.TelephonyManager; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends AppCompatActivity < private static final int MY_PERMISSION_REQUEST_CODE_PHONE_STATE = 1; private static final String LOG_TAG = "AndroidExample"; private EditText editTextPhoneNumbers; private Button buttonGetPhones; @Override protected void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.editTextPhoneNumbers = (EditText) this.findViewById(R.id.editText_infos); this.buttonGetPhones = (Button) this.findViewById(R.id.button_getInfo); this.buttonGetPhones.setOnClickListener(new View.OnClickListener() < @Override public void onClick(View v) < askPermissionAndGetPhoneNumbers(); >>); > private void askPermissionAndGetPhoneNumbers() < // With Android Level >= 23, you have to ask the user // for permission to get Phone Number. if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) < // 23 // Check if we have READ_PHONE_STATE permission int readPhoneStatePermission = ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE); if ( readPhoneStatePermission != PackageManager.PERMISSION_GRANTED) < // If don't have permission so prompt the user. this.requestPermissions( new String[], MY_PERMISSION_REQUEST_CODE_PHONE_STATE ); return; > > this.getPhoneNumbers(); > // Need to ask user for permission: android.permission.READ_PHONE_STATE @SuppressLint("MissingPermission") private void getPhoneNumbers() < try < TelephonyManager manager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE); String phoneNumber1 = manager.getLine1Number(); this.editTextPhoneNumbers.setText(phoneNumber1); // Log.i( LOG_TAG,"Your Phone Number: " + phoneNumber1); Toast.makeText(this,"Your Phone Number: " + phoneNumber1, Toast.LENGTH_LONG).show(); // Other Informations: if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) < // API Level 26. String imei = manager.getImei(); int phoneCount = manager.getPhoneCount(); Log.i(LOG_TAG,"Phone Count: " + phoneCount); Log.i(LOG_TAG,"EMEI: " + imei); >if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) < // API Level 28. SignalStrength signalStrength = manager.getSignalStrength(); int level = signalStrength.getLevel(); Log.i(LOG_TAG,"Signal Strength Level: " + level); >> catch (Exception ex) < Log.e( LOG_TAG,"Error: ", ex); Toast.makeText(this,"Error: " + ex.getMessage(), Toast.LENGTH_LONG).show(); ex.printStackTrace(); >> // When you have the request results @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) < super.onRequestPermissionsResult(requestCode, permissions, grantResults); // switch (requestCode) < case MY_PERMISSION_REQUEST_CODE_PHONE_STATE: < // Note: If request is cancelled, the result arrays are empty. // Permissions granted (SEND_SMS). if (grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) < Log.i( LOG_TAG,"Permission granted!"); Toast.makeText(this, "Permission granted!", Toast.LENGTH_LONG).show(); this.getPhoneNumbers(); >// Cancelled or denied. else < Log.i( LOG_TAG,"Permission denied!"); Toast.makeText(this, "Permission denied!", Toast.LENGTH_LONG).show(); >break; > > > // When results returned @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) < super.onActivityResult(requestCode, resultCode, data); if (requestCode == MY_PERMISSION_REQUEST_CODE_PHONE_STATE) < if (resultCode == RESULT_OK) < // Do something with data (Result returned). Toast.makeText(this, "Action OK", Toast.LENGTH_LONG).show(); >else if (resultCode == RESULT_CANCELED) < Toast.makeText(this, "Action Cancelled", Toast.LENGTH_LONG).show(); >else < Toast.makeText(this, "Action Failed", Toast.LENGTH_LONG).show(); >> > > 

View more Tutorials:

Это онлайн курс вне вебсайта o7planning, который мы представляем, он включает бесплатные курсы или курсы со скидкой.

  • Android Beginners Guide To Create A Weather Forecast App
  • * * The Complete Android Oreo(8.1) , N ,M and Java Development
  • Absolute Java Basics for Android
  • Android App & Game Development :Build 6 Android Apps & Games
  • Android and iOS Apps for Your WordPress Blog
  • Unity 3d Game Development — iOS, Android, & Web — Beginners
  • Learn Android Development From Scratch
  • Ultimate Coding Course for Web App and Android Development
  • The Complete Android™ Material Design Course
  • Unity Android Game Development : Build 7 2D & 3D Games
  • Advance Android Programming — learning beyond basics
  • Publish Games on Android, iTunes, and Google Play with UE4
  • Learning Path:Android:Application Development with Android N
  • The Android Crash Course For Beginners to Advanced
  • Android development quick start for beginners
  • Android App Development and Design
  • Developing High Quality Android Applications
  • Become an iOS/Android Game Developer with Unity 2017
  • Learning Path: Android: App Development with Android N
  • The Complete Android & Java Developer Course — Build 21 Apps
  • The Complete Android App Development
  • Create Android & iOS Apps Without Coding
  • Develop Your First 2D Game With Unity3D for Android
  • Full Stack Mobile Developer course ( iOS 11, and Android O )
  • Develop Android Apps using Python: Kivy

Источник

Get Phone Number in Android using TelephonyManager

Follow us on our fanpages to receive notifications every time there are new articles. Facebook Twitter

1- Android Phone number

android.telephony.TelephonyManager class provides information regarding telephony services such as ID, EMEI Number, SIM Serial Number, Phone Network Type, etc. In addition, it also provides the information about the phone status.

In this article, I’m going to guide you to use TelephonyManager in order to get the phone number information of the Android device, and a few pieces of other common information.

It is required that the application is given permission to view phone status information, so you need to add the following permissions to the AndroidManifest.xml file :

Note: From Android 6.0 (API Level 23) onwards, an application wants to know the status of the phone it needs to ask to get user permission.

 package org.o7planning.getphonenumberexample; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import android.Manifest; import android.annotation.SuppressLint; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.os.Bundle; import android.telephony.SignalStrength; import android.telephony.TelephonyManager; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends AppCompatActivity < private static final int MY_PERMISSION_REQUEST_CODE_PHONE_STATE = 1; private static final String LOG_TAG = "AndroidExample"; private EditText editTextPhoneNumbers; private Button buttonGetPhones; @Override protected void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.editTextPhoneNumbers = (EditText) this.findViewById(R.id.editText_infos); this.buttonGetPhones = (Button) this.findViewById(R.id.button_getInfo); this.buttonGetPhones.setOnClickListener(new View.OnClickListener() < @Override public void onClick(View v) < askPermissionAndGetPhoneNumbers(); >>); > private void askPermissionAndGetPhoneNumbers() < // With Android Level >= 23, you have to ask the user // for permission to get Phone Number. if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) < // 23 // Check if we have READ_PHONE_STATE permission int readPhoneStatePermission = ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE); if ( readPhoneStatePermission != PackageManager.PERMISSION_GRANTED) < // If don't have permission so prompt the user. this.requestPermissions( new String[], MY_PERMISSION_REQUEST_CODE_PHONE_STATE ); return; > > this.getPhoneNumbers(); > // Need to ask user for permission: android.permission.READ_PHONE_STATE @SuppressLint("MissingPermission") private void getPhoneNumbers() < try < TelephonyManager manager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE); String phoneNumber1 = manager.getLine1Number(); this.editTextPhoneNumbers.setText(phoneNumber1); // Log.i( LOG_TAG,"Your Phone Number: " + phoneNumber1); Toast.makeText(this,"Your Phone Number: " + phoneNumber1, Toast.LENGTH_LONG).show(); // Other Informations: if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) < // API Level 26. String imei = manager.getImei(); int phoneCount = manager.getPhoneCount(); Log.i(LOG_TAG,"Phone Count: " + phoneCount); Log.i(LOG_TAG,"EMEI: " + imei); >if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) < // API Level 28. SignalStrength signalStrength = manager.getSignalStrength(); int level = signalStrength.getLevel(); Log.i(LOG_TAG,"Signal Strength Level: " + level); >> catch (Exception ex) < Log.e( LOG_TAG,"Error: ", ex); Toast.makeText(this,"Error: " + ex.getMessage(), Toast.LENGTH_LONG).show(); ex.printStackTrace(); >> // When you have the request results @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) < super.onRequestPermissionsResult(requestCode, permissions, grantResults); // switch (requestCode) < case MY_PERMISSION_REQUEST_CODE_PHONE_STATE: < // Note: If request is cancelled, the result arrays are empty. // Permissions granted (SEND_SMS). if (grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) < Log.i( LOG_TAG,"Permission granted!"); Toast.makeText(this, "Permission granted!", Toast.LENGTH_LONG).show(); this.getPhoneNumbers(); >// Cancelled or denied. else < Log.i( LOG_TAG,"Permission denied!"); Toast.makeText(this, "Permission denied!", Toast.LENGTH_LONG).show(); >break; > > > // When results returned @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) < super.onActivityResult(requestCode, resultCode, data); if (requestCode == MY_PERMISSION_REQUEST_CODE_PHONE_STATE) < if (resultCode == RESULT_OK) < // Do something with data (Result returned). Toast.makeText(this, "Action OK", Toast.LENGTH_LONG).show(); >else if (resultCode == RESULT_CANCELED) < Toast.makeText(this, "Action Cancelled", Toast.LENGTH_LONG).show(); >else < Toast.makeText(this, "Action Failed", Toast.LENGTH_LONG).show(); >> > > 

View more Tutorials:

These are online courses outside the o7planning website that we introduced, which may include free or discounted courses.

  • Android Beginners Guide To Create A Weather Forecast App
  • * * The Complete Android Oreo(8.1) , N ,M and Java Development
  • Android App & Game Development :Build 6 Android Apps & Games
  • Absolute Java Basics for Android
  • Android and iOS Apps for Your WordPress Blog
  • Unity 3d Game Development — iOS, Android, & Web — Beginners
  • Learn Android Development From Scratch
  • Ultimate Coding Course for Web App and Android Development
  • Unity Android Game Development : Build 7 2D & 3D Games
  • The Complete Android™ Material Design Course
  • Advance Android Programming — learning beyond basics
  • Publish Games on Android, iTunes, and Google Play with UE4
  • The Android Crash Course For Beginners to Advanced
  • Learning Path:Android:Application Development with Android N
  • Android development quick start for beginners
  • Developing High Quality Android Applications
  • Android App Development and Design
  • Become an iOS/Android Game Developer with Unity 2017
  • The Complete Android & Java Developer Course — Build 21 Apps
  • Learning Path: Android: App Development with Android N
  • The Complete Android App Development
  • Create Android & iOS Apps Without Coding
  • Full Stack Mobile Developer course ( iOS 11, and Android O )
  • Develop Your First 2D Game With Unity3D for Android
  • Android Internals and Working with the source

Источник

How do I get Android Device ID or IMEI?

The following code snippets shows you how to get a Device ID of an Android phone. To be able to read the Device ID you need to update the AndroidManifest.xml file and add the READ_PHONE_STATE permission.

package org.kodejava.android; import android.Manifest; import android.content.Context; import android.content.pm.PackageManager; import android.os.Build; import android.provider.Settings; import android.telephony.TelephonyManager; import android.util.Log; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; public class MainActivity extends AppCompatActivity < private static final int PERMISSIONS_READ_PHONE_STATE = 1; @Override protected void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) < ActivityCompat.requestPermissions(this, new String[], PERMISSIONS_READ_PHONE_STATE); > String deviceId = null; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) < deviceId = Settings.Secure.getString(getApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID); >else < TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); if (telephonyManager.getImei() != null) < deviceId = telephonyManager.getImei(); >else if (telephonyManager.getMeid() != null) < deviceId = telephonyManager.getMeid(); >> Log.d("MyApp", "Device ID: " + deviceId); > > 

A programmer, recreational runner and diver, live in the island of Bali, Indonesia. Programming in Java, Spring, Hibernate / JPA. You can support me working on this project, buy me a cup of coffee ☕ every little bit helps, thank you 🙏

Источник

Оцените статью