Merhaba, Android 6.0 Marshmallow sonrasında bildiğiniz üzere kullanıcıdan alınan izinler sadece manifest dosyasından talep etmek yeterli bulunmadığı için istenen izinleri ihtiyaç duyulan yerde bir defaya mahsus tekrar kullanıcının gözüne sokmak gerekiyor, buna da runtime permission deniliyor. Bana cihazın IMEI numarası lazım olduğu için bir ufak bi çalışma yaptım bunu sizinle paylaşmak istiyorum.
Neyse lafı uzatmadan kodu fırlatayım.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
package com.damrakoc.waservice.utils.device; import android.Manifest; import android.annotation.TargetApi; import android.app.Activity; import android.content.Context; import android.content.DialogInterface; import android.content.pm.PackageManager; import android.os.Build; import android.support.annotation.NonNull; import android.support.v4.app.ActivityCompat; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.telephony.TelephonyManager; import android.util.Log; import com.damrakoc.waservice.MainActivity; import com.damrakoc.waservice.R; /** * Created by damra on 15/04/17. */ public class Permissions extends AppCompatActivity { final private static int PERMISSIONS_REQUEST_READ_PHONE_STATE = 0; final private Activity activity; public Permissions(Activity activity) { this.activity = activity; } /** * Called when the 'loadIMEI' function is triggered. */ @TargetApi(Build.VERSION_CODES.M) public void loadIMEI() { // Check if the READ_PHONE_STATE permission is already available. if (ActivityCompat.checkSelfPermission(activity.getApplicationContext(), 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. doPermissionGrantedStuffs(); } } /** * Requests the READ_PHONE_STATE permission. * If the permission has been denied previously, a dialog will prompt the user to grant the * permission, otherwise it is requested directly. */ private void requestReadPhoneStatePermission() { if (ActivityCompat.shouldShowRequestPermissionRationale(activity, 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. alertPerm(getString(R.string.permission_read_phone_state_rationale), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.READ_PHONE_STATE}, PERMISSIONS_REQUEST_READ_PHONE_STATE); doPermissionGrantedStuffs(); } }); } else { // READ_PHONE_STATE permission has not been granted yet. Request it directly. ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.READ_PHONE_STATE}, 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 == 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 doPermissionGrantedStuffs(); } else { alertPerm(getString(R.string.permissions_not_granted_read_phone_state), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { doPermissionGrantedStuffs(); } }); } } } private void alertPerm(String msg,DialogInterface.OnClickListener okListener) { new AlertDialog.Builder(activity) .setTitle("Permission Request") .setMessage(msg) .setCancelable(false) .setPositiveButton(android.R.string.yes, okListener) .setIcon(R.mipmap.ic_launcher_round) .show(); } public void doPermissionGrantedStuffs() { //Have an object of TelephonyManager TelephonyManager tm = (TelephonyManager) activity.getSystemService(Context.TELEPHONY_SERVICE); //Get IMEI Number of Phone String IMEINumber = tm.getDeviceId(); //Get Subscriber ID String subscriberID = tm.getSubscriberId(); //Get SIM Serial Number String SIMSerialNumber = tm.getSimSerialNumber(); //Get Line Number String lineNumber = tm.getLine1Number(); //Get Network Country ISO Code String networkCountryISO = tm.getNetworkCountryIso(); //Get SIM Country ISO Code String SIMCountryISO = tm.getSimCountryIso(); //Get the device software version String softwareVersion = tm.getDeviceSoftwareVersion(); //Get the Voice mail number String voiceMailNumber = tm.getVoiceMailNumber(); //Get the Phone Type CDMA/GSM/NONE int phoneType = tm.getPhoneType(); switch (phoneType) { case (TelephonyManager.PHONE_TYPE_CDMA): // your code break; case (TelephonyManager.PHONE_TYPE_GSM): // your code break; case (TelephonyManager.PHONE_TYPE_NONE): // your code break; } //Find whether the Phone is in Roaming, returns true if in roaming boolean isRoaming = tm.isNetworkRoaming(); //Get the SIM state int SIMState = tm.getSimState(); switch (SIMState) { case TelephonyManager.SIM_STATE_ABSENT: // your code break; case TelephonyManager.SIM_STATE_NETWORK_LOCKED: // your code break; case TelephonyManager.SIM_STATE_PIN_REQUIRED: // your code break; case TelephonyManager.SIM_STATE_PUK_REQUIRED: // your code break; case TelephonyManager.SIM_STATE_READY: // your code break; case TelephonyManager.SIM_STATE_UNKNOWN: // your code break; } Log.d(MainActivity.TAG, "doPermissionGrantedStuffs:IMEINumber: "+IMEINumber); Log.d(MainActivity.TAG, "doPermissionGrantedStuffs:subscriberID: "+subscriberID); Log.d(MainActivity.TAG, "doPermissionGrantedStuffs:SIMSerialNumber: "+SIMSerialNumber); Log.d(MainActivity.TAG, "doPermissionGrantedStuffs:lineNumber: "+lineNumber); Log.d(MainActivity.TAG, "doPermissionGrantedStuffs:networkCountryISO: "+networkCountryISO); Log.d(MainActivity.TAG, "doPermissionGrantedStuffs:SIMCountryISO: "+SIMCountryISO); Log.d(MainActivity.TAG, "doPermissionGrantedStuffs:softwareVersion: "+softwareVersion); Log.d(MainActivity.TAG, "doPermissionGrantedStuffs:voiceMailNumber: "+voiceMailNumber); Log.d(MainActivity.TAG, "doPermissionGrantedStuffs:isRoaming: "+isRoaming); Log.d(MainActivity.TAG, "doPermissionGrantedStuffs:SIMState: "+SIMState); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package com.damrakoc.waservice; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import android.widget.Toast; import com.damrakoc.waservice.utils.device.DeviceInfo; import com.damrakoc.waservice.utils.device.Permissions; import com.damrakoc.whatsappapi.WhatsappApi; public class MainActivity extends AppCompatActivity { public static final String TAG = "WAService"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Permissions perm = new Permissions(this); perm.loadIMEI(); } } |
sonuç: