I'm having some trouble with Kotlin syntax, it requires a semicolon inside a function call (?!).
I was simply attempting to convert the code from AltBeacon website, the one used to get coarse location permissions. Here's what I've come up with on a rush:
class MainActivity : AppCompatActivity() {
val TAG = "MAIN_ACTIVITY"
val PERMISSION_REQUEST_COARSE_LOCATION = 1
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
if (checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){
val builder = AlertDialog.Builder(this)
builder.setTitle("This app needs location access")
builder.setMessage("Please grant location access so this app can detect beacons.")
builder.setPositiveButton(android.R.string.ok, null)
builder.setOnDismissListener {
val permissions = arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION)
requestPermissions(permissions, PERMISSION_REQUEST_COARSE_LOCATION)
}
builder.show()
}
}
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
when (requestCode) {
PERMISSION_REQUEST_COARSE_LOCATION-> {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "coarse location permission granted")
} else {
val builder = AlertDialog.Builder(this)
builder.setTitle("Functionality limited")
builder.setMessage("Since location access has not been granted, this app will not be able to discover beacons when in the background.");
builder.setPositiveButton(android.R.string.ok, null)
builder.setOnDismissListener { }
builder.show()
}
}
}
}
}
Compiler has issues with this line:
requestPermissions(permissions, PERMISSION_REQUEST_COARSE_LOCATION)
Unexpected tokens (use ';' to separate expressions on the same line)
Everything is ok with kotlin syntax here.
The problem is that you have a strange invisible symbol at the end of that line. I pasted that code in notepad++ and saw this:
So just delete it and compilation will succeed.
Related
Im trying to implement permission handling for healthconnect. This is done in the funciton checkPermissionAndRun (strongly inspired by the documentation), however I'm not sure what the difference between the two else{...} code segments is.Both check if the permission is given already, but why do we need 2 of those? Furthermore, is it alright if I call the checkpermissionandrun function once in the oncreate, or should i do this also when interacting with HealthConnect?
class MainActivity : AppCompatActivity() {
// build a set of permissions for required data types
val PERMISSIONS =
setOf(
HealthPermission.createReadPermission(HeartRateRecord::class),
HealthPermission.createWritePermission(HeartRateRecord::class),
HealthPermission.createReadPermission(StepsRecord::class),
HealthPermission.createWritePermission(StepsRecord::class)
)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val hcManager = HealthConnectManager(this)
checkPermissionsAndRun(hcManager.healthConnectClient, PERMISSIONS)
}
private fun checkPermissionsAndRun(client: HealthConnectClient, PERMISSIONS: Set<HealthPermission>) {
val requestPermissionActivityContract = PermissionController.createRequestPermissionResultContract()
val requestPermissions =
registerForActivityResult(requestPermissionActivityContract) { granted ->
if (granted.containsAll(PERMISSIONS)) {
//Permission granted text 1
Toast.makeText(applicationContext, "permissions granted 1", Toast.LENGTH_SHORT).show()
} else {
// Lack of required permissions, But what is different to the coroutine else below?
}
}
lifecycleScope.launch {
val granted = client.permissionController.getGrantedPermissions(PERMISSIONS)
if (granted.containsAll(PERMISSIONS)) {
//Permission Granted text 2
Toast.makeText(applicationContext, "permissions granted 2", Toast.LENGTH_SHORT).show()
} else {
//lack of required permissions
requestPermissions.launch(PERMISSIONS)
}
}
}
}
Thank you in advance
I need to access the IMEI number of android device. I'm not going to publish this android app on playstore. It's for personal use in the organization. Is there any workaround to fetch the IMEI number? Right now I'm geting security exception while fetching IMEI. I have made device admin app. Though I'm not able to access IMEI number.
class MainActivity : AppCompatActivity() {
val REQUEST_CODE = 101
var imei: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// in the below line, we are initializing our variables.
// in the below line, we are initializing our variables.
val telephonyManager = getSystemService(TELEPHONY_SERVICE) as TelephonyManager
val imeiTextView = findViewById<TextView>(R.id.idTVIMEI)
// in the below line, we are checking for permissions
// in the below line, we are checking for permissions
if (ActivityCompat.checkSelfPermission(
this,
Manifest.permission.READ_PHONE_STATE
) != PackageManager.PERMISSION_GRANTED
) {
// if permissions are not provided we are requesting for permissions.
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.READ_PHONE_STATE),
REQUEST_CODE
)
}
// in the below line, we are setting our imei to our text view.
// in the below line, we are setting our imei to our text view.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
imei = telephonyManager.imei
}
imeiTextView.setText(imei)
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<String?>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == REQUEST_CODE) {
// in the below line, we are checking if permission is granted.
if (grantResults.size != 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// if permissions are granted we are displaying below toast message.
Toast.makeText(this, "Permission granted.", Toast.LENGTH_SHORT).show()
} else {
// in the below line, we are displaying toast message
// if permissions are not granted.
Toast.makeText(this, "Permission denied.", Toast.LENGTH_SHORT).show()
}
}
}
}
I followed the simple steps to ask for multiple permissions at once, here is my code for the permission request:
class MainActivity : AppCompatActivity() {
private val permissionCode = 100
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
getPermissions()
}
fun getPermissions() {
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.NFC, Manifest.permission.INTERNET),
permissionCode
)
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
when (requestCode) {
permissionCode -> {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission granted
Toast.makeText(this, "Permissions granted", Toast.LENGTH_SHORT).show()
} else {
// Permission denied
Toast.makeText(this, "Permission denied", Toast.LENGTH_SHORT).show()
}
}
}
}
When I am starting the app I dont get any dialog to accept or deny the permissions and just get the toast "Permissions granted" but if I check the permissions in the app info I dont see any permissions granted. What I am doing wrong? Can someone help me?
Neither INTERNET nor NFC are permissions that need to be requested at runtime. Just having them in the manifest (via <uses-permission> elements) is sufficient.
Only permissions with a protection level of dangerous need to be requested at runtime — this table lists those. INTERNET and NFC are normal permissions, not dangerous.
I'm using google maps so I need to ask some permissions.
I'm requesting the user during the onStart() of the Fragment
I'm calling requestPermissions because I'm in a fragment and I also have called super.onRequestPermissionsResult as recommended in this answer and as you can see in the code below.
I have put some breakpoint and Log to be 100% sure that onRequestPermissionsResult is not called.
class FindLessonFragment : BaseFragment(), OnMapReadyCallback, OnClusterItemClickListener<ClusterLesson>, OnClusterClickListener<ClusterLesson> {
override fun onStart() {
super.onStart()
checkPermissions()
}
private fun checkPermissions() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (activity!!.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
|| activity!!.checkSelfPermission(permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
|| activity!!.checkSelfPermission(permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
val builder = AlertDialog.Builder(activity!!)
builder.setTitle("This app needs location access")
builder.setMessage("Please grant location access so this app can work normally.")
builder.setPositiveButton(android.R.string.ok, null)
builder.setOnDismissListener {
requestPermissions(
arrayOf(permission.ACCESS_COARSE_LOCATION, permission.READ_EXTERNAL_STORAGE, permission.ACCESS_FINE_LOCATION),
PERMISSION_REQUEST_LOCATION)
}
builder.show()
}
}
}
override fun onRequestPermissionsResult(requestCode: Int, #NonNull permissions: Array<String>, #NonNull grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
Log.e("PERMISSIONRESULT", "PASS PERMISSION RESULT")
when (requestCode) {
BaseActivity.PERMISSION_REQUEST_LOCATION -> {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.d("BaseActivity", "coarse location permission granted")
onMapReady(mMap)
} else {
val builder = AlertDialog.Builder(context!!)
builder.setTitle("Functionality limited")
builder.setMessage(
"Since location access has not been granted, this app will not be able to work correctly.")
builder.setPositiveButton(android.R.string.ok, null)
builder.setOnDismissListener { dialog -> dialog.dismiss() }
builder.show()
}
}
}
}
companion object {
fun create() = FindLessonFragment()
internal const val PERMISSION_REQUEST_LOCATION = 1
}
}
minSdkVersion 23
Any kind of help is welcome
The onRequestPermissionsResult has to be catch in the activty.
For the past months I have been developing an app which a feature of it implements BLE connection from the app to a device. Suddenly today, after a few scans, the app returns everytime 0 device despite obtaining before like 5 or 6.
I have been thinking that it was a permission problem related with coarse location, however, the changes I made it was so little that it should not be affected, but that does not matter
As I followed the bluetooth guide that Google offers amongst other guides, this is my code:
onCreate method:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_guide)
handler = Handler()
devicesResult = ArrayList()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
// Android M Permission check
if (this.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
{
val builder = AlertDialog.Builder(this)
builder.setTitle("This app needs location access")
builder.setMessage("Please grant location access so this app can detect beacons.")
builder.setPositiveButton(android.R.string.ok, null)
builder.setOnDismissListener { requestPermissions(arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION), PERMISSION_REQUEST_COARSE_LOCATION) }
builder.show()
}else{
if (areLocationServicesEnabled(this))
{
if (!packageManager.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE))
{
Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show()
finish()
}
bluetoothAdapter?.takeIf { it.isDisabled }?.apply {
val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
startActivityForResult(enableBtIntent, 1)
}
//scanLeDevice(true)
}
}
}
}
onRequestPermissionsResult method:
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<String>, grantResults: IntArray
) {
when (requestCode) {
PERMISSION_REQUEST_COARSE_LOCATION -> {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//Log.d(FragmentActivity.TAG, "coarse location permission granted")
} else {
val builder = AlertDialog.Builder(this)
builder.setTitle("Functionality limited")
builder.setMessage("Since location access has not been granted, this app will not be able to discover beacons when in the background.")
builder.setPositiveButton(android.R.string.ok, null)
builder.setOnDismissListener { }
builder.show()
}
return
}
}
}
areLocationServicesEnabled method:
private fun areLocationServicesEnabled(context:Context):Boolean {
val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
return try {
locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
} catch (e:Exception) {
e.printStackTrace()
false
}
}
onResume method:
override fun onResume() {
super.onResume()
if (bluetoothAdapter != null && !bluetoothAdapter!!.isEnabled)
{
val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
startActivityForResult(enableBtIntent, 1)
}
mLEScanner = bluetoothAdapter!!.bluetoothLeScanner
scanLeDevice(true)
}
scanLeDevice method:
private fun scanLeDevice(enable:Boolean) {
if (enable)
{
handler!!.postDelayed({
mLEScanner!!.stopScan(mScanCallback)
//selectDevice()
}, SCAN_PERIOD)
mLEScanner!!.startScan(mScanCallback)
}
else
{
mLEScanner!!.stopScan(mScanCallback)
}
}
mScanCallback attribute:
private val mScanCallback = object : ScanCallback() {
override fun onScanResult(callbackType: Int, result: ScanResult) {
Log.i("callbackType", callbackType.toString())
Log.i("result", result.toString())
devicesResult!!.add(result)
}
override fun onBatchScanResults(results: List<ScanResult>) {
for (sr in results) {
Log.i("ScanResult - Results", sr.toString())
}
}
override fun onScanFailed(errorCode: Int) {
Log.e("Scan Failed", "Error Code: $errorCode")
}
}
As I said before, I expected to show and save on an Array called deviceResult every one of them, however, suddenly, no devices seems to appear after each new scan.
First of all, sorry for the big delay to answer muy question.
After trying not to use the scan more than 6 times between 30 seconds as #Emil pointed out on the last comment fo my question, I finally could make my project work, thanks #Emil.