Can't get IMEI number on first install - android

I am trying to get the IMEI number of the android device.I am able to get it too, but the application doesn't return the IMEI in the first Install.
i need to restart the application.
Can anyone tell me where I am Wrong?
Thanks in advance
public class RootActivity extends AppCompatActivity {
private static final int REQUEST_PERMISSION_PHONE_STATE = 1;
TextView tv ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = findViewById(R.id.TV_imei);
tv.setText(showPhoneStatePermission());
}private String showPhoneStatePermission() {
String imei ="ERROR";
int permissionCheck = ContextCompat.checkSelfPermission(
this, Manifest.permission.READ_PHONE_STATE);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_PHONE_STATE}, REQUEST_PERMISSION_PHONE_STATE);
} else {
imei=getIMEI();
}
return imei;
}
private String getIMEI() {
TelephonyManager telephonyManager = (TelephonyManager) getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
#SuppressLint("MissingPermission") String deviceId = telephonyManager.getDeviceId().trim();
return deviceId;
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
showPhoneStatePermission();
}
}

tv.setText(showPhoneStatePermission()) will only get called once in OnCreate(). After permission granted in onRequestPermissionsResult you did not set text .
You need to cal setText() after onRequestPermissionsResult.
You can use
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
tv.setText(showPhoneStatePermission());
}
If this is just a sample then Okay . If not then you are missing Permission handling in your code . You need to handle Rational too . Look at android-m-check-runtime-permission

Related

How to detect and selecting user local country?

I'm using that library: https://github.com/AlmogBaku/IntlPhoneInput to get user phone number.
I want to detect and select automatically the local country of the user.
Reading that: https://github.com/AlmogBaku/IntlPhoneInput#public-methods,
I added that: android.permission.READ_PHONE_STATE in my AndroidManifest.xml but can't detect and select automatically the local country of the user.
Any help ?
If you are using API-level 23+, then android.permission.READ_PHONE_STATE in manifest won't be sufficient and you need to request it programmatically, and to do so
public class MainActivity extends AppCompatActivity {
private static final int PERMISSION_READ_STATE = 21;
private IntlPhoneInput mPhoneInputView;
#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) {
// We do not have this permission. Let's ask the user by showingg a dialog
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, PERMISSION_READ_STATE);
}
mPhoneInputView = findViewById(R.id.my_phone_input);
}
// Called when the user decides the dialog permission
#Override
public void onRequestPermissionsResult(int requestCode,
#NonNull String[] permissions, #NonNull int[] grantResults) {
if (requestCode == PERMISSION_READ_STATE) {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission granted!
mPhoneInputView.setDefault();
} else {
// permission denied
}
}
}
...
}

Ask for WRITE_EXTERNAL_STORAGE permission from two different functions in the same activity

I have two different functions for takeScreenshot and saveAsPDF, both of them requires WRITE_EXTERNAL_STORAGE permission. But how can I create a switch case in onRequestPermissionsResult for same permission in the same Activity because requestCode is same in both the cases.
you can create separate class like following to check and ask permission ->
public class PermissionCheck {
private static final String TAG = PermissionCheck.class.getSimpleName();
private Context context;
private static final int EXTERNAL_WRITE_PERMISSION_CODE = 620;
private static final int CAMERA_PERMISSION_CODE = 335;
public PermissionCheck(Context context) {
this.context = context;
}
// Check Required Permissions...
public boolean CheckRequestedPermission(Context context, String CheckRequiredPermission) {
return ContextCompat.checkSelfPermission(context, CheckRequiredPermission) == PackageManager.PERMISSION_GRANTED;
}
// To Request Permission...
private void requestPermission(final Context context, final String RequestedPermission, final int PermissionCode) {
if (!CheckRequestedPermission(context, RequestedPermission)) {
ActivityCompat.requestPermissions((Activity) context, new String[]{RequestedPermission}, PermissionCode);
}
}
// Just add Permission Code that you required to get permissions and call this method...
public void AskPermission(int RequestCode) {
switch (RequestCode) {
case EXTERNAL_WRITE_PERMISSION_CODE:
requestPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE, EXTERNAL_WRITE_PERMISSION_CODE);
break;
case CAMERA_PERMISSION_CODE:
requestPermission(context, Manifest.permission.CAMERA, CAMERA_PERMISSION_CODE);
break;
}
}
}
and ask whatever permission you want and implement OnRequestPermissionResult in Activity or fragment like following.
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case EXTERNAL_WRITE_PERMISSION_CODE:
Log.d(TAG, "onPermissionResult : Permission Granted? " + IsExternalWritePermissionGranted);
// changing value based on permission Deny/Accept
IsExternalWritePermissionGranted = permissionCheck.CheckRequestedPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
break;
case CAMERA_PERMISSION:
// changing value based on permission Deny/Accept
IsCameraPermissionGranted = permissionCheck.CheckRequestedPermission(this, Manifest.permission.CAMERA);
break;
}
}
Note that the permission code you pass from activity/fragment to PermissionCheck should be same, as in the PermissionCheck class.

IMEI Getting null for the first time when we install application Android

I have tried different links but none of them works:
TelephonyManager returns null for IMEI number: what can cause this?
How to get device's IMEI/ESN number with code programming But in android > 6
and many more links but none of them working.
This is my Utils Class:
public static String getIMEINumber(Context context){
TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
return telephonyManager.getDeviceId();
}
Calling below code from my Activity:
String deviceIMEI = Utils.getIMEINumber(LoginActivity.this);
But this is giving me null sometimes (when we install App for the first time particularly).Can you help me on this?
Thank you very much for your time and assistance in this matter.
See sample:
private int REQUEST_PERMISSION_PHONE_STATE = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showPhoneStatePermission();
}
private void showPhoneStatePermission() {
int permissionCheck = ContextCompat.checkSelfPermission(
this, Manifest.permission.READ_PHONE_STATE);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
requestPermission(Manifest.permission.READ_PHONE_STATE, REQUEST_PERMISSION_PHONE_STATE);
} else {
getIMEI();
}
}
private void getIMEI() {
String deviceId = IMEIUtil.getDeviceId(this);
toastMessage(deviceId);
}
private void requestPermission(String permissionName, int permissionRequestCode) {
ActivityCompat.requestPermissions(this,
new String[]{permissionName}, permissionRequestCode);
}
private void toastMessage(String msg) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_PERMISSION_PHONE_STATE) {
showPhoneStatePermission();
}
}
And class IMEIUtil:
public static String getDeviceId(Context context) {
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String deviceId = telephonyManager.getDeviceId().trim();
if (deviceId == null) {
String androidId = Settings.Secure.getString(context.getContentResolver(),
Settings.Secure.ANDROID_ID);
deviceId = android.os.Build.SERIAL + "#" + androidId;
}
return deviceId.trim();
}
Require add permission <uses-permission android:name="android.permission.READ_PHONE_STATE" /> before tag <application> on AndroidManifest. You can see full code: https://github.com/HoangDang/GetIMEI/tree/master/GetIMEI. I hope it can help you!

Storage permission not working for Nougat

I want to give storage permission for my app. My code working perfect till Marshmallow, only problem in Nougat
The below method always return false in nougat even permission granted manually from settings.
private boolean checkWriteExternalPermission() {
String permission = "android.permission.WRITE_EXTERNAL_STORAGE";
int res = getApplicationContext().checkCallingOrSelfPermission(
permission);
return (res == PackageManager.PERMISSION_GRANTED);
}
I used this for Nougat and allow permission but above method still returns false.
void storagePermission(){
StorageManager sm = (StorageManager)getSystemService(Context.STORAGE_SERVICE);
StorageVolume volume = sm.getPrimaryStorageVolume();
Intent intent = volume.createAccessIntent(Environment.DIRECTORY_PICTURES);
startActivityForResult(intent, 1);
}
Please help me to resolve this.
You should use libs: https://github.com/hotchemi/PermissionsDispatcher
#RuntimePermissions
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MainActivityPermissionsDispatcher.storagePermissionWithCheck(this);
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
// NOTE: delegate the permission handling to generated method
MainActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
}
#NeedsPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
void storagePermission(){
StorageManager sm =(StorageManager)getSystemService(Context.STORAGE_SERVICE);
StorageVolume volume = sm.getPrimaryStorageVolume();
Intent intent = volume.createAccessIntent(Environment.DIRECTORY_PICTURES);
startActivityForResult(intent, 1);
}
#OnPermissionDenied(Manifest.permission.WRITE_EXTERNAL_STORAGE)
void showDeniedForCamera() {
// don't allow code here
}
#OnNeverAskAgain(Manifest.permission.WRITE_EXTERNAL_STORAGE)
void showNeverAskForCamera() {
// neverAskAgain code here
}
}
You should add this code:
public void onActivityResult(final int requestCode, int resultCode, final Intent data){
if(requestCode==1) {
switch (resultCode) {
case Activity.RESULT_OK:
getContentResolver().takePersistableUriPermission(data.getData(),
Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
break;
}
}
}

How to request permission to open camera hardware

I trying to develop an App that uses the Camera hardware in Android. While coding the following line
this.mCameraManager.openCamera(cameraIds[0], new CameraDevice.StateCallback() {...}
I was asked to explicitly check if the permission is available. when i added the permission check, Android added the following lines
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
and I override the follwoing method
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
now, i do not know what to write inside the if-statement and the "onRequestPermissionsResult" mentioned above. your help is highly appreciated
requestPermissions(new String[]{Manifest.permission.CAMERA}, REQ_ACCESS_FINE_LOCATION);
You have to show android permission dialog to users, after that you can catch users motions in onRequestPermissionsResult.
I think that you should look this website : https://developer.android.com/training/permissions/requesting.html
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST_CODE_PERMISSIONS:
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED)
// Open Camera
break;
}
}
Try this:
// The permission required by the application to work properly
protected static final String[] requiredPermissions;
private static final int PERMISSION_REQUEST = 0;
static {
List<String> perms = new ArrayList<>(Arrays.asList(
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.CAMERA,
Manifest.permission.RECORD_AUDIO
));
requiredPermissions = perms.toArray(new String[perms.size()]);
}
Call verifyPermissions() method in onCreate():
private void verifyPermissions() {
if (!hasAllPermissions()) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(
this,
requiredPermissions,
PERMISSION_REQUEST
);
}
}
private boolean hasAllPermissions() {
// Check if we have all required permissions.
for (String perm : requiredPermissions) {
if (ActivityCompat.checkSelfPermission(this, perm) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
return true;
}
#Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST: {
// If request is cancelled, the result arrays are empty.
if (!hasAllPermissions()) {
finish();
}
return;
}
}
}

Categories

Resources