0

Currently, I am trying to build a QR code scanner that scans the QR code and at the same time get the device id of the cell which then eventually post to the server

I'm constantly getting the error"call requires permission which may be rejected by the user"

In th manifest.xml ,i have already included <uses-permission android:name="android.permission.READ_PHONE_STATE" /> I also tries to downgrade the android SDK 23 back to 22.

Plus, none of the answers here seem to solve my issues. Any ideas?

public class MainActivity extends AppCompatActivity {
private static final String LOG_TAG = MainActivity.class.getSimpleName();
private static final int BARCODE_READER_REQUEST_CODE = 1;
public TelephonyManager tel;
public TextView imei;

private TextView mResultTextView;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mResultTextView = (TextView) findViewById(R.id.result_textview);
    Button scanBarcodeButton = (Button) 
    findViewById(R.id.scan_barcode_button);
    scanBarcodeButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getApplicationContext(), 
     BarcodeCaptureActivity.class);
            startActivityForResult(intent, BARCODE_READER_REQUEST_CODE);
        }
    });

}

private class OkHttpAync extends AsyncTask<Object, Void, Object> {

    private String TAG = MainActivity.OkHttpAync.class.getSimpleName();
    private Context contx;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Object doInBackground(Object... params) {
        contx = (Context) params[0];
        String requestType = (String) params[1];
        String requestParam = (String) params[2];

        Log.e(TAG, "processing http request in async task");

        if ("post".equals(requestType)) {
            Log.e(TAG, "processing post http request using OkHttp");
            postHttpResponse(requestParam);
        }
        return null;
    }

    @Override
    protected void onPostExecute(Object result) {
        super.onPostExecute(result);
    /*
        if (result != null) {
            Log.e(TAG, "populate UI after response from service using OkHttp 
   client");
            respone.setText((String) result);
        }

       */
    }
}
public void postHttpResponse(String requestParam) {
    OkHttpClient httpClient = new OkHttpClient();
    String url = "https://requestb.in/1k1hm5y1";
    RequestBody formBody = new FormBody.Builder()
           // .add("employeeid",userData )
            //.add("imie",userData2)
            .add("qrcode",requestParam)
            .build();
    Request request = new Request.Builder()
            .url(url)
            .post(formBody)
            .build();
    Response response = null;
    try{
        response = httpClient.newCall(request).execute();
    }
    catch (IOException e) {

    }
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{

// GET DEVICE ID
    TelephonyManager tManager = (TelephonyManager) getBaseContext()
            .getSystemService(Context.TELEPHONY_SERVICE);
    String deviceIMEI = tManager.getDeviceId();


    if (requestCode == BARCODE_READER_REQUEST_CODE) {
        if (resultCode == CommonStatusCodes.SUCCESS) {
            if (data != null) {
                Barcode barcode = 
     data.getParcelableExtra(BarcodeCaptureActivity.BarcodeObject);
                Point[] p = barcode.cornerPoints;
                //display scan bar code value
                mResultTextView.setText(barcode.displayValue);

                new MainActivity.OkHttpAync().execute(this, 
      "post",barcode.displayValue + 33 );

            } else mResultTextView.setText(R.string.no_barcode_captured);

        } else Log.e(LOG_TAG, 
   String.format(getString(R.string.barcode_error_format),
                CommonStatusCodes.getStatusCodeString(resultCode)));
    } else super.onActivityResult(requestCode, resultCode, data);
    }
     }
Abhishek Bhardwaj
  • 1,164
  • 3
  • 14
  • 39
epiphany
  • 756
  • 1
  • 11
  • 29
  • 1
    Possible duplicate of [Call requires permissions that may be rejected by user](https://stackoverflow.com/questions/33327984/call-requires-permissions-that-may-be-rejected-by-user) – AskNilesh Dec 21 '17 at 04:17

2 Answers2

0

the READ_PHONE_STATE permission is dangerous and you need to request it at runtime. You should take a look at this Google Guide.

I suggest using this PermissionsDispatcher Library for easy to request permission:

@NeedsPermission(Manifest.permission.READ_PHONE_STATE)
void readPhoneState() {
    // do any thing here
}


@OnPermissionDenied(Manifest.permission.READ_PHONE_STATE)
    void showDeniedForPhoneState() {

    }
Kingfisher Phuoc
  • 8,052
  • 9
  • 46
  • 86
0

Nearly tried every answer here, and this work by changing the sdk version to 22. Hope it helps anyone who stumble this issue as well

In the AndroidManifest.xml

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion '22.0.2'

    defaultConfig {
        applicationId "com.varvet.barcodereadersample"
        minSdkVersion 19
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:22.1.0'
    compile 'com.google.android.gms:play-services-vision:11.6.0'
    compile 'com.squareup.okhttp3:okhttp:3.3.1'
}
epiphany
  • 756
  • 1
  • 11
  • 29