BluetoothAdapter using ACTION_REQUEST_DISCOVERABLE - android

I am trying to use BluetoothAdapter with ACTION_REQUEST_DISCOVERABLE to discover my phone to other users according to this:
testIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivity(testIntent);
I get the dialog box of Bluetooth Permission Request, but for some reason I can't see it on my activity. I can see it only when I exit this program. I want to override the menu in that dialog.
Can I do it , if so , how can I change it ? And why can't I see it on the activity I am in it ?

Related

Android : How can I close the file browser/file picker?

In my application, I have a connection to a server and I want to transfer a file on it. So first, I have to select a file. I open a file browser with this code :
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
intent.setType("*/*");
startActivityForResult(intent, REQUEST_BROWSE_FILES);
The problem arrive if I have a disconnection during time I browse to find a file. If the connection is lost, I want to popup a message box saying : "Connection lost" then I want to close the file picker. How can I do something similar to that ?
In fact, when the disconnection occur, I don't know which activity is on top to show a message. Did I miss something, but it is annoying to need an activity to popup a dialog.
Thank you for your help.
you cant directly close another activity from yours that you started from a file picker intent. I think you just need to wait for them to choose something and once you get the response back in OnActivityResult check if you disconnected and if you are just show a popup that you are disconnected

Custom Bluetooth message mBluetoothAdapter

With the following code you can pop up a window with bluetooth acces request:
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
The output of the code is the following image:
Is it possible to change the message (so not "An app wants to turn on Bluetooth", but something like "Hey can you please turn on Bluetooth?"
You should create your own dialog with your own message and then call
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mBluetoothAdapter.enable();
And do not forget to declare the BLUETOOTH_ADMIN permission in your manifest.
Anyway, this operation is discouraged... The enable() method is provided only for applications that include a user interface for changing system settings, such as a "power manager" app.
No, this is build-in Android functionality, so you would have to make a custom dialog which asks the question the way you want.
Then you would enable or disable Bluetooth programmatically based on what the users choice was.

How do I use Espresso with System dialogs

I have buttons in my app that launch Android Chooser intents (ACTION_VIEW, etc.) And i was wondering how to use an Espresso idling resource. to wait and detect display of the dialog and how to cancel it and return after.
I am not asking for code per se .. just wondering how to do it?
e.g.
onView(withId(R.id.ad_email_button)).perform(click());
this button opens the intent
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
So how to cancel the dialog so the test doesn't hang and verify the appropriate system dialog was shown?
As far as I know using Espresso we are limited to actions and verifications inside specific Activity. But I found out that we can access UiAutomator as well and perform BACK action to dismiss alert dialog:
InstrumentationRegistry.getInstrumentation().getUiAutomation()
.performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);
Looks like the answer is to use UIAutomator for these tests:
https://developer.android.com/tools/testing-support-library/index.html#features

handle device's default screen lock behaviour

I am using device default lock screen in my app for activating and deactivating screen lock. In my application I used a check box which shows that screen lock is activated or not.
Intent intent = new Intent(DevicePolicyManager.ACTION_SET_NEW_PASSWORD);
startActivity(intent);
I am receiving it callback in my DeviceAdminReceiver class.
Methods:onPasswordChanged onPasswordFailed onPasswordSucceeded
Now if user selects none or press back none of these methods get called. I am not able to identify is screen is locked or not ? I used OnActivityResult for handling callback in my Activity, it works fine for back pressed(resultcode 0) but gives same results for all other options.
I found this link which tells that it can't be handled externally.
Summary: I wants to handle screen lock options directly from my application.
if we set password quality on that case user can not able to select none password option
Might it will help you .
We can do this in this way
private ComponentName mDeviceAdmin;
private DevicePolicyManager mDPM;
mDPM.setPasswordQuality(mDeviceAdmin, DevicePolicyManager.PASSWORD_QUALITY_SOMETHING);
Intent intent = new Intent(DevicePolicyManager.ACTION_SET_NEW_PASSWORD);
mContext.startActivity(intent);

How to run Device Police Manager From A Service

I am able to create an Activity that uses the DevicePolicyManager API's.
The tutorials show that I need use it the following fashion:
if (!mDPM.isAdminActive(mAdminName)) {
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mAdminName);
intent.putExtra("wipe-data", DeviceAdminInfo.USES_POLICY_WIPE_DATA);
startActivityForResult(intent, REQUEST_ENABLE);
}
else {
mDPM.wipeData(0);
}
However I would like this to run inside a Service.
But I cant call
startActivityForResult
from within a Service.
So what would be the best approach or strategy for me to try ?
The only reason you need to call startActivityForResult() is if your app is not presently configured as a device administrator, to lead the user to go set that up for you. Hence, put that portion of your logic inside of your user interface.
Your service itself would just skip doing anything if isAdminActive() returns false.

Categories

Resources