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);
}
}