How to detect when user didn't allow for open bluetooth?
if(!isBluetoothOpen()) {
val enableBluetoothIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
if (isPermissionGranted()) {
startActivityForResult(
enableBluetoothIntent,
REQUEST_ENABLE_BLUETOOTH
)
}
As per documentation https://developer.android.com/reference/android/bluetooth/BluetoothAdapter
Notification of the result of this activity is posted using the Activity.onActivityResult(int, int, Intent) callback. The resultCode will be Activity.RESULT_OK if Bluetooth has been turned on or Activity.RESULT_CANCELED if the user has rejected the request or an error has occurred.
Code:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == REQUEST_ENABLE_BLUETOOTH) {
when (resultCode) {
Activity.RESULT_OK -> { // User approved permission }
Activity.RESULT_CANCELED -> { // User rejected permission }
}
}
}
Related
I am using FirebaseUI Auth to authenticate users with gmail. I am signing to with my gmail. In onActivityResult I check result code and it is RESULT_OK. Although when I create IdpResponse from intent, I get null and I can't check whether it's a new user or not.
I have added google-services.json in the application, also added libraries and plugins in gradle.
here is my code :
fun promptUserSignIn() {
val providers = arrayListOf(AuthUI.IdpConfig.GoogleBuilder().build())
startActivityForResult(
AuthUI.getInstance().createSignInIntentBuilder().setAvailableProviders(
providers
).build(), SIGN_IN_REQUEST_CODE
)
}
#Override
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == SIGN_IN_REQUEST_CODE) {
val response = IdpResponse.fromResultIntent(intent)
if (resultCode == Activity.RESULT_OK) {
Log.e(TAG, "response: $response intent: $intent")
}
}
}
I have a Bluetooth application in Android 10 and before I can start to scan for the devices I need to request the location permissions. When I install the application for the first time I get the popup for the location, but after that, if I turn them off it won't ask again and I need to ask the user to turn them on every time if they are off.
Here's my code:
override fun onStart() {
super.onStart()
requestPermissions(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), PERMISSION_REQUEST_CODE)
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == PERMISSION_REQUEST_CODE && permissions.contains(Manifest.permission.ACCESS_FINE_LOCATION)) {
if (grantResults.contains(PackageManager.PERMISSION_GRANTED)) {
if (!BluetoothAdapter.getDefaultAdapter().isEnabled) {
startActivityForResult(Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE), ENABLE_BLT_REQUEST_CODE)
} else {
viewModel.discoverBLTDevices()
}
} else {
showToast("Location permission needed to scan for devices.")
}
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == ENABLE_BLT_REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
viewModel.discoverBLTDevices()
} else if (resultCode == Activity.RESULT_CANCELED) {
finish()
}
}
}
First time you ask for permission, if user denied, second time you should show custom dialog (or something else) to inform to user that he / she should allow location permission from setting, also navigate to settings.
Example:
private fun requestLocationPermissions() {
// shouldShowRequestPermissionRationale method checks permission is not granted.
// So Requesting permission
if (!shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION)
|| !shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_COARSE_LOCATION)) {
requestPermission(PERM_ID, arrayOf(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION))
} else {
showPermissionRequiredDialog()
}
}
fun showPermissionRequiredDialog() {
val alertDialog = MaterialAlertDialogBuilder(context, R.style.DarkDialogTheme)
alertDialog.setTitle("title")
alertDialog.setMessage("your message to user")
alertDialog.setPositiveButton(R.string.label_ok, DialogInterface.OnClickListener { dialog, which ->
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:" + BuildConfig.APPLICATION_ID))
context.startActivity(intent)
})
alertDialog.show()
}
}
Also Take look this
I am trying to implement the Firebase AuthUI for a dummy app following the course by google itself on udacity. I am not getting the same output as in the course itself and idk what to do exactly or what is going wrong. Below is the code i have written to start up the AuthUI:
private fun launchSignInFlow() {
val providers =
arrayListOf(
AuthUI.IdpConfig.EmailBuilder().build(),
AuthUI.IdpConfig.GoogleBuilder().build()
)
startActivityForResult(
AuthUI.getInstance().createSignInIntentBuilder().setAvailableProviders(
providers
).build(), SIGN_IN_RESULT_CODE
)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == SIGN_IN_RESULT_CODE) {
val response = IdpResponse.fromResultIntent(data)
if (resultCode == Activity.RESULT_OK) {
Log.i(
TAG,
"Successfully signed in user " +
"${FirebaseAuth.getInstance().currentUser?.displayName}!"
)
} else {
Log.i(TAG, "Sign in unsuccessful ${response?.error?.errorCode}")
}
}
}
this is the output screen/ui that i get from this code
And this is what the output should be like according to the course i am following
What's worse is that even when i try to enter an email to the output i get, the screen gets stuck and does nothing at all. This is the link for the video of google for this specific task
https://youtu.be/JKZpZvbm3Bk
I have this function to create a snackbar with an action:
private fun setupSnackbar(view: View) {
if (isConnected) {
showSnackbar(view, Strings.connected, TSnackbar.LENGTH_SHORT)
} else {
showSnackbar(view, Strings.notConnected,
TSnackbar.LENGTH_INDEFINITE).setActionTextColor(
ContextCompat.getColor(this, R.color.lightGreyBlue)).setAction("Connect") {
checkBluetoothState()
connectToProxy()
}.show()
}
}
private fun checkBluetoothState() {
if (!bluetoothAdapter.isEnabled()) {
val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT)
}
}
I am aiming to execute connectToProxy() only after checkBluetoothState() is completed (and BT enabled).
Right now they get called at the same time. I know I would need a callback function here but I wouldn't know how to write it.
Thank you!
You have to implement onActivityResult, in which you'll receive bluetooth action response, i have implemented a basic task for you, in which i'm asking for bluetooth onclick.
btnBluetooth.setOnClickListener {
val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT)
}
You'll receive result in onActivityResult.
i have assigned REQUEST_ENABLE_BT= 1, you can change value.
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if(requestCode==REQUEST_ENABLE_BT && resultCode == RESULT_OK)
{
callYourFunction()
}
}
private fun callYourFunction() {
Toast.makeText(applicationContext,"Done",Toast.LENGTH_SHORT).show()
}
As per documentation , for in app update API implementation I am using immediate mode. But there is close button on full screen UI. When user clicks on the close button, UI is dismissed. How to handle this immediate flow in this case as I don't want user to use my app?
Please try this.
override fun onResume() {
super.onResume()
if (listener != null) {
appUpdateManager.registerListener(listener)
}
appUpdateManager
.appUpdateInfo.addOnSuccessListener { appUpdateInfo ->
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
&& appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)
) {
appUpdateManager.startUpdateFlowForResult(
appUpdateInfo,
AppUpdateType.IMMEDIATE,
this,
100
)
} else if (appUpdateInfo.updateAvailability()
== UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS
) {
appUpdateManager.startUpdateFlowForResult(
appUpdateInfo,
IMMEDIATE,
this,
100
);
}
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
// super.onActivityResult(requestCode, resultCode, data)
if (requestCode == 100) {
when (resultCode) {
Activity.RESULT_OK -> {
Toast.makeText(this, "RESULT_OK", Toast.LENGTH_LONG).show()
}
Activity.RESULT_CANCELED -> {
Toast.makeText(this, "RESULT_CANCELED", Toast.LENGTH_LONG).show()
finish()
}
ActivityResult.RESULT_IN_APP_UPDATE_FAILED -> {
Toast.makeText(this, "RESULT_IN_APP_UPDATE_FAILED", Toast.LENGTH_LONG).show()
finish()
}
else -> {
Toast.makeText(this, "No result", Toast.LENGTH_LONG).show()
}
}
}
}
In case if user clicks on close button, you can get that callback in this condition resultCode == Activity.RESULT_CANCELED:-
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?): Boolean {
// React to activity result and if request code == ResultActivity.REQUEST_CODE
if (requestCode == UPDATE_REQUEST_CODE) {
//when user clicks update button
if (resultCode == Activity.RESULT_OK) {
Toast.makeText(mContext, "App download starts...", Toast.LENGTH_LONG).show()
} else if (resultCode == Activity.RESULT_CANCELED) {
//if user click on close button
Toast.makeText(mContext, "App download canceled.", Toast.LENGTH_LONG).show()
//Here you can do whatever you want (call finish() to close the app.)
} else if (resultCode == ActivityResult.RESULT_IN_APP_UPDATE_FAILED) {
Toast.makeText(mContext, "App download failed.", Toast.LENGTH_LONG).show()
}
return true
}
return false
}
I assume you want to disallow the users to use your app until they update the app?
To do that, you can listen to update status and if the user cancels, you can request to start the update again (maybe show a dialog/message to the user explaining why the update is necessary).
From the docs
Get a callback for update status
After starting an update, you can use an onActivityResult() callback to handle an update failure or cancellation, as shown below.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MY_REQUEST_CODE) {
if (resultCode != RESULT_OK) {
log("Update flow failed! Result code: " + resultCode);
// If the update is cancelled or fails,
// you can request to start the update again.
}
}
}