I'm trying to figure out how to configure my VPN application to toggle the Always-On flag from within the application with a toggle.
I'm aware of
DevicePolicyManager#setAlwaysOnVpnPackage
However, it's not very clear how to use this function. I have tried the following:
Admin.java
public class Admin extends DeviceAdminReceiver {
#Override
public void onEnabled(#NonNull Context context, #NonNull Intent intent) {
super.onEnabled(context, intent);
}
}
AdvancedSettings.java
public class AdvancedSettings extends AppCompatActivity
implements View.OnClickListener {
private ComponentName componentName;
private DevicePolicyManager devicePolicyManager;
private boolean alwaysOnConfiguredValue;
private static final int ALWAYS_ON_REQUEST_CODE = 11;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_advanced);
Button button = findViewById(R.id.toggleAlwaysOnButton);
button.setOnClickListener(this);
devicePolicyManager = (DevicePolicyManager) this
.getSystemService(Context.DEVICE_POLICY_SERVICE);
componentName = new ComponentName(
this.getApplicationContext(), Admin.class);
}
#Override
public void onClick(View v) {
if (v.getId() == R.id.toggleAlwaysOnButton) {
this.setAlwaysOn(true);
}
}
/**
* Handle the Activity Result.
*/
#Override
protected void onActivityResult(
int requestCode, int resultCode, #Nullable Intent data
) {
if (requestCode == ALWAYS_ON_REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
finalizeAlwaysOnToggle();
} else {
Log.w(
"Invalid result code " + resultCode
);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
/**
* Start the process of enabling "Always On" for the VPN.
*
* #param boolean value
*/
private void setAlwaysOn(boolean value) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
alwaysOnConfiguredValue = value;
if (devicePolicyManager.isAdminActive(componentName)) {
finalizeAlwaysOnToggle();
return;
}
requestAdminAccess();
} else {
Toas.makeText(this, "Not supported", Toast.LENGTH_LONG).show();
}
}
/**
* Request Admin Access for this application
* if it has not already been done.
*/
private void requestAdminAccess() {
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
intent.putExtra(
DevicePolicyManager.EXTRA_ADD_EXPLANATION,
"This is required to modify the Always-On Feature from within the Test Application."
);
this.startActivityForResult(intent, ALWAYS_ON_REQUEST_CODE);
}
/**
* Finalize setting the always on toggle after the Admin Access
* has been granted for this application.
*/
private void finalizeAlwaysOnToggle() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
try {
if (devicePolicyManager.isAdminActive(componentName)) {
devicePolicyManager.setAlwaysOnVpnPackage(
componentName, (alwaysOnConfiguredValue) ? "com.myapp" : null, true
);
} else {
Log.e(
"Device Policy Manager Admin is not yet active while " +
"trying to finalize changes to AlwaysOnToggle."
);
}
} catch (PackageManager.NameNotFoundException e) {
Log.e("Unable to set always on vpn due to NameNotFound Exception.", e);
}
}
}
}
It processes the request for adding the Device Admin just fine, however after that has completed, when it runs finalizeAlwaysOnToggle(), during the call to devicePolicyManager.setAlwaysOnVpnPackage I receive the following error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.myapp, PID: 30778
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=11, result=-1, data=null} to activity {com.myapp/com.myapp.ui.settings.AdvancedSettings}: java.lang.SecurityException: Admin ComponentInfo{com.myapp/com.myapp.provider.Admin} does not own the profile
You have to differentiate between "Device Admin", "Device Owner" and "Profile Owner".
As it is stated in the docs you need to be one of the latter twos to be able to call setAlwaysOnVpnPackage:
Called by a device or profile owner to configure an always-on VPN
connection through a specific application for the current user. This
connection is automatically granted and persisted after a reboot.
(https://developer.android.com/reference/android/app/admin/DevicePolicyManager.html#setAlwaysOnVpnPackage(android.content.ComponentName,%2520java.lang.String,%2520boolean))
Related
I'm currently working on an android app to control Bluetooth devices but no available devices show up even though I can find them from the built-in settings>bluetooth menu.
I initially followed the android starter sample out of the box, but couldn't make it work. I then came across several similar posts and tried those as well, but still couldn't make it work. This is my code so far: ```
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_LOCATION_BT = 3;
private static final int REQUEST_ENABLE_BT = 2;
private BluetoothAdapter bluetoothAdapter;
private ArrayList<String> deviceList;
private ActivityMainBinding binding;
private ArrayAdapter<String> listAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
requiredSetup();
setupListView();
}
private void setupListView() {
Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
deviceList = new ArrayList<>();
if (pairedDevices.size() > 0) {
// There are paired devices. Get the name and address of each paired device.
for (BluetoothDevice device : pairedDevices) {
deviceList.add(device.getName());
//String deviceHardwareAddress = device.getAddress(); // MAC address
}
}
listAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, deviceList);
binding.listView.setAdapter(listAdapter);
}
private void requiredSetup() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
if (ContextCompat.checkSelfPermission(getApplicationContext(),
Manifest.permission.ACCESS_BACKGROUND_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(
MainActivity.this,
new String[]{Manifest.permission.ACCESS_BACKGROUND_LOCATION},
REQUEST_LOCATION_BT);
}
}
// checking if device supports bluetooth
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
// Device doesn't support Bluetooth
Toast.makeText(this, "Device doesnt support bluetooth", Toast.LENGTH_SHORT).show();
return;
}
// enabling bluetooth if disabled
if (!bluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_ENABLE_BT && resultCode == RESULT_OK) {
Toast.makeText(this, "Bluetooth enabled", Toast.LENGTH_SHORT).show();
} else if (requestCode == REQUEST_ENABLE_BT && resultCode == RESULT_CANCELED) {
Toast.makeText(this, "App requires bluetooth to function", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String permissions[], #NonNull int[] grantResults) {
if (requestCode == REQUEST_LOCATION_BT) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission granted, yay! Start the Bluetooth device scan.
startScan();
} else {
// Alert the user that this application requires the location permission to perform the scan.
Toast.makeText(this, "Requires location permission to work", Toast.LENGTH_SHORT).show();
}
}
}
/**
* initializes device discovery. The list of devices are sent to the broadcast receiver
* The discovery process usually involves an inquiry scan of about 12 seconds,
* followed by a page scan of each device found to retrieve its Bluetooth name.
*/
private void startScan() {
// If we're already discovering, stop it
if (bluetoothAdapter.isDiscovering()) {
Toast.makeText(this, "stopping discovery", Toast.LENGTH_SHORT).show();
bluetoothAdapter.cancelDiscovery();
} else {
try {
// Register for broadcasts when a device is discovered
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
this.registerReceiver(mReceiver, filter);
// Request discover from BluetoothAdapter
bluetoothAdapter.startDiscovery();
binding.progressBar.setVisibility(View.VISIBLE);
} catch (IllegalArgumentException e) {
e.printStackTrace();
Toast.makeText(this, "receivers not registered", Toast.LENGTH_SHORT).show();
}
}
}
/**
* #param view the button view that is pressed and the scanning begins
*/
public void searchDevices(View view) {
// checking if location permissions enabled
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_LOCATION_BT);
} else {
startScan();
}
}
/**
* The BroadcastReceiver that listens for discovered devices and changes the title when
* discovery is finished
*/
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Toast.makeText(getApplicationContext(), "reeiver working", Toast.LENGTH_SHORT).show();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// If it's already paired, skip it, because it's been listed already
if (device != null && device.getBondState() != BluetoothDevice.BOND_BONDED) {
listAdapter.add(device.getName() + "\n" + device.getAddress());
Log.i("NEW DEVICE", device.getName());
listAdapter.notifyDataSetChanged();
}
// When discovery is finished, change the Activity title
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
//setProgressBarIndeterminateVisibility(false);
//setTitle(R.string.select_device);
binding.progressBar.setVisibility(View.INVISIBLE);
if (listAdapter.getCount() == 0) {
Toast.makeText(context, "no devices found", Toast.LENGTH_SHORT).show();
}
} else if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
Toast.makeText(context, "Bluetooth state changed", Toast.LENGTH_SHORT).show();
} else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
Toast.makeText(context, "Discovery started", Toast.LENGTH_SHORT).show();
}
}
};
#Override
protected void onDestroy() {
super.onDestroy();
// Make sure we're not doing discovery anymore
if (bluetoothAdapter != null) {
bluetoothAdapter.cancelDiscovery();
}
// Unregister broadcast listeners
this.unregisterReceiver(mReceiver);
}}
This is my manifest permissions:
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
This is my repository. It would be very helpful if anyone could shed some light here.
It seems like the code was just fine. The problem was with the target SDK version. Upon changing the version from 30 to 28, it worked. Thanks to MatejC's answer.
I need to open overlay setting window for my app. Of course, my manifest file use SYSTEM_ALERT_WINDOW permission already.
public class MainActivity extends ReactActivity {
private static final int CODE_DRAW_OVER_OTHER_APP_PERMISSION = 2084;
#Override
protected String getMainComponentName() {
return "MyToolbox";
}
#Override
protected ReactActivityDelegate createReactActivityDelegate() {
return new ReactActivityDelegate(this, getMainComponentName()) {
#Override
protected ReactRootView createRootView() {
return new RNGestureHandlerEnabledRootView(MainActivity.this);
}
};
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requireDrawOverlayPermission();
}
//Ask draw overlay permission
void requireDrawOverlayPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(this)) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package: " + getPackageName()));
startActivityForResult(intent, CODE_DRAW_OVER_OTHER_APP_PERMISSION);
} else {
//TODO: Permission granted
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CODE_DRAW_OVER_OTHER_APP_PERMISSION) {
if (Settings.canDrawOverlays(this)) {
//TODO: Permission granted
} else {
Toast.makeText(this, "Draw over the app is not available", Toast.LENGTH_SHORT).show();
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}
The first debug run from my Android Studio, my app got crashed.
And next time, it's not crash, but it doesn't show overlay setting window for me. I can see a screen is flashed but close immediately.
What wrong in my code?
Thank you!
Finally, I figured it out afterall. That settings window need package name to know which app require this permission. And the uri can't parse my package name because I put a space between "package:" and "getPackageName()". I removed it as below, and everything works fine!
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
I've been looking for a solution to this and I came across several answers that didn't fit the use-case. The sample code from Google works fine if imported as is, but the code sits in an Activity and is messy if you want to integrate into a real project where the code would ideally be in a fragment.
The issue is with the enableAutoManage functionality and what to do in the fresh install case where the googleApiClient is built but the authentication/account-pick screen is not yet done and you end up with connection failed on the googleApiClient. I'm going to answer this in full, below.
Here is a sample Activity that will load the GoogleFit_Fragment into the FrameLayout:
public class Main_Activity extends AppCompatActivity {
public static int USER_AUTHORISED_REQUEST_CODE = 5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*
This will load the GoogleFit_Fragment into the FrameLayout,
which will cause the buildFitnessClient() function to be called from the Fragment,
which will cause this Activity to popup the authentication screen
*/
getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.fade_in, R.anim.fade_out)
.replace(R.id.fragment_container, new GoogleFit_Fragment()).commit();
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
/*
The result of the account chooser is here,
send it to the fragment so we can handle it inside
*/
fragmentReplace.onActivityResult(USER_AUTHORISED_REQUEST_CODE, resultCode, data);
}
}
The GoogleFit_Fragment:
public class GoogleFit_Fragment extends Fragment {
public static final String TAG = "Sample";
private static final int REQUEST_PERMISSIONS_REQUEST_CODE = 1;
private GoogleApiClient googleApiFitnessClient;
private OnDataPointListener mListener;
private View fragmentView;
private Activity parentActivity;
#Override
public void onStop() {
super.onStop();
if (googleApiFitnessClient != null) {
Log.d(TAG, "onStop REACHED, client not null and is connected");
googleApiFitnessClient.stopAutoManage(getActivity());
googleApiFitnessClient.disconnect();
}
}
#Override
public void onResume() {
super.onResume();
if (googleApiFitnessClient != null) {
Log.d(TAG, "onResume REACHED, client not null");
googleApiFitnessClient.stopAutoManage(getActivity());
googleApiFitnessClient.disconnect();
googleApiFitnessClient.connect();
} else {
Log.d(TAG, "onResume REACHED, client null, buildingClient");
buildFitnessClient();
googleApiFitnessClient.connect();
}
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof Activity) {
parentActivity = (Activity) context;
}
}
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
fragmentView = inflater.inflate(R.layout.fragment_google_fit, container, false);
initializeLogging();
if (!checkPermissions()) {
requestPermissions();
}
return fragmentView;
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == Main_Activity.USER_AUTHORISED_REQUEST_CODE && googleApiFitnessClient != null) {
Log.d(TAG, "Activity result finished authorissation, disconnect the client and reconnect");
googleApiFitnessClient.stopAutoManage(getActivity());
googleApiFitnessClient.disconnect();
googleApiFitnessClient.connect();
}
}
private void buildFitnessClient() {
googleApiFitnessClient = new GoogleApiClient.Builder(parentActivity)
.addApi(Fitness.SENSORS_API)
// .addScope(new Scope(Scopes.FITNESS_LOCATION_READ))
.addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE))
// .addScope(new Scope(Scopes.FITNESS_NUTRITION_READ_WRITE))
// .addScope(new Scope(Scopes.FITNESS_BODY_READ_WRITE))
.addConnectionCallbacks(
new GoogleApiClient.ConnectionCallbacks() {
#Override
public void onConnected(Bundle bundle) {
Log.i(TAG, "Connected!!!");
// Now you can make calls to the Fitness APIs.
findFitnessDataSources();
}
#Override
public void onConnectionSuspended(int i) {
// If your connection to the sensor gets lost at some point,
// you'll be able to determine the reason and react to it here.
if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_NETWORK_LOST) {
Log.i(TAG, "Connection lost. Cause: Network Lost.");
} else if (i
== GoogleApiClient.ConnectionCallbacks.CAUSE_SERVICE_DISCONNECTED) {
Log.i(TAG,
"Connection lost. Reason: Service Disconnected");
}
}
}
)
.addOnConnectionFailedListener(
new GoogleApiClient.OnConnectionFailedListener() {
#Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.d(TAG, "Connection failed! " + connectionResult.getErrorMessage());
}
}
)
.enableAutoManage((FragmentActivity) parentActivity, 0, new GoogleApiClient.OnConnectionFailedListener() {
#Override
public void onConnectionFailed(ConnectionResult result) {
Log.i(TAG, "Google Play services connection failed. Cause: " +
result.toString());
}
})
.build();
}
private void findFitnessDataSources() {
// Note: Fitness.SensorsApi.findDataSources() requires the ACCESS_FINE_LOCATION permission.
Fitness.SensorsApi.findDataSources(googleApiFitnessClient, new DataSourcesRequest.Builder()
// At least one datatype must be specified.
.setDataTypes(DataType.TYPE_STEP_COUNT_CUMULATIVE)
// Can specify whether data type is raw or derived.
.setDataSourceTypes(DataSource.TYPE_RAW)
.build())
.setResultCallback(new ResultCallback<DataSourcesResult>() {
#Override
public void onResult(DataSourcesResult dataSourcesResult) {
Log.i(TAG, "Result: " + dataSourcesResult.getStatus().toString());
for (DataSource dataSource : dataSourcesResult.getDataSources()) {
Log.i(TAG, "Data source found: " + dataSource.toString());
Log.i(TAG, "Data Source type: " + dataSource.getDataType().getName());
//Let's register a listener to receive Activity data!
if (dataSource.getDataType().equals(DataType.TYPE_STEP_COUNT_CUMULATIVE)
&& mListener == null) {
Log.i(TAG, "Data source for " + dataSource.getDataType() + " found! Registering.");
registerFitnessDataListener(dataSource,
DataType.TYPE_STEP_COUNT_CUMULATIVE);
}
}
}
});
}
/**
* Register a listener with the Sensors API for the provided {#link DataSource} and
* {#link DataType} combo.
*/
private void registerFitnessDataListener(DataSource dataSource, DataType dataType) {
mListener = new OnDataPointListener() {
#Override
public void onDataPoint(DataPoint dataPoint) {
for (Field field : dataPoint.getDataType().getFields()) {
Value val = dataPoint.getValue(field);
Log.i(TAG, "Detected DataPoint field: " + field.getName());
Log.i(TAG, "Detected DataPoint value: " + val);
}
}
};
Fitness.SensorsApi.add(
googleApiFitnessClient,
new SensorRequest.Builder()
.setDataSource(dataSource) // Optional but recommended for custom data sets.
.setDataType(DataType.TYPE_STEP_COUNT_CUMULATIVE) // Can't be omitted.
.setSamplingRate(1, TimeUnit.SECONDS)
.build(),
mListener)
.setResultCallback(new ResultCallback<Status>() {
#Override
public void onResult(Status status) {
if (status.isSuccess()) {
Log.i(TAG, "Listener registered!");
} else {
Log.i(TAG, "Listener not registered.");
}
}
});
}
private void initializeLogging() {
/*
Not really needed, you can just log to Logcat without having a view in this fragment
*/
LogWrapper logWrapper = new LogWrapper();
Log.setLogNode(logWrapper);
MessageOnlyLogFilter msgFilter = new MessageOnlyLogFilter();
logWrapper.setNext(msgFilter);
LogView logView = (LogView) fragmentView.findViewById(R.id.sample_logview);
logView.setTextAppearance(parentActivity, R.style.Log);
logView.setBackgroundColor(Color.WHITE);
msgFilter.setNext(logView);
Log.i(TAG, "Ready");
}
/**
* Return the current state of the permissions needed.
*/
private boolean checkPermissions() {
int permissionState = ActivityCompat.checkSelfPermission(parentActivity,
Manifest.permission.ACCESS_FINE_LOCATION);
return permissionState == PackageManager.PERMISSION_GRANTED;
}
private void requestPermissions() {
boolean shouldProvideRationale =
ActivityCompat.shouldShowRequestPermissionRationale(parentActivity,
Manifest.permission.ACCESS_FINE_LOCATION);
if (shouldProvideRationale) {
Log.i(TAG, "Displaying permission rationale to provide additional context.");
Snackbar.make(
fragmentView.findViewById(R.id.main_activity_view),
R.string.permission_rationale,
Snackbar.LENGTH_INDEFINITE)
.setAction(R.string.ok, new View.OnClickListener() {
#Override
public void onClick(View view) {
// Request permission
ActivityCompat.requestPermissions(parentActivity,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.BODY_SENSORS},
REQUEST_PERMISSIONS_REQUEST_CODE);
}
})
.show();
} else {
Log.i(TAG, "Requesting permission");
ActivityCompat.requestPermissions(parentActivity,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.BODY_SENSORS},
REQUEST_PERMISSIONS_REQUEST_CODE);
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions,
#NonNull int[] grantResults) {
Log.i(TAG, "onRequestPermissionResult");
if (requestCode == REQUEST_PERMISSIONS_REQUEST_CODE) {
if (grantResults.length <= 0) {
Log.i(TAG, "User interaction was cancelled.");
} else if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
buildFitnessClient();
} else {
Snackbar.make(
fragmentView.findViewById(R.id.main_activity_view),
R.string.permission_denied_explanation,
Snackbar.LENGTH_INDEFINITE)
.setAction(R.string.settings, new View.OnClickListener() {
#Override
public void onClick(View view) {
// Build intent that displays the App settings screen.
Intent intent = new Intent();
intent.setAction(
Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package",
BuildConfig.APPLICATION_ID, null);
intent.setData(uri);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
})
.show();
}
}
}
}
Flow:
Launch app
Google API client is built, causing a connection failed callback, but also causing the Account Picker dialog to appear
Account is picked, onActivityResult from Activity is called, result is sent to onActivityResult in Fragment
Google API client is restarted (stopAutoManage, disconnect, connect)
Logging starts STEP_COUNT_CUMULATIVE or any other data type you choose
This works and is tested a lot, please ask if you're unsure of anything
Whenever I start/resume my android phone app, it will prompt me to enable my bluetooth twice, regardless what I choose on the first prompt message. What is wrong with my code?
public class MainActivity extends AppCompatActivity {
// BLE management
private static BluetoothManager btManager;
private static BluetoothAdapter btAdapter;
// Set the enable bluetooth code
private final static int REQUEST_ENABLE_BT = 0;
// String for LogCat documentation
private final static String DEBUG_TAG= "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
btAdapter = btManager.getAdapter();
if (btAdapter != null) {
if (!btAdapter.isEnabled()) {
// Request Bluetooth Adapter to be turned on
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
}
}
else
{
Log.i(DEBUG_TAG, "No bluetooth available");
}
Button launchReminderButton = (Button) findViewById(R.id.button);
launchReminderButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Launch Reminder
// Used Context's startActivity() method
// Create an intent stating which Activity you would like to
// start
Intent reminder = new Intent(MainActivity.this, Reminder.class);
// Launch the Activity using the intent
startActivity(reminder);
}
}
);
}
#Override
public void onStart() {
super.onStart();
}
#Override
public void onResume() {
super.onResume();
// check for Bluetooth enabled on each resume
if(btAdapter != null && !btAdapter.isEnabled()){
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
}
}
#Override
public void onPause() {
super.onPause();
}
#Override
public void onStop() {
super.onStop();
}
#Override
public void onRestart() {
super.onRestart();
}
#Override
public void onDestroy() {
super.onDestroy();
btAdapter = null;
}
#Override
protected void onActivityResult(int request_enable_bt, int result_enable_bt, Intent data)
{
super.onActivityResult(request_enable_bt, result_enable_bt, data);
if(result_enable_bt == RESULT_OK) {
Toast.makeText(this, "Turned On", Toast.LENGTH_SHORT).show();
}
else if (result_enable_bt == RESULT_CANCELED)
{
Toast.makeText(this, "Didn't Turn On", Toast.LENGTH_SHORT).show();
finish();
}
}
Thanks.
Pay attention to the following code,the key is finish() you used here:
#Override
protected void onActivityResult(int request_enable_bt,
int result_enable_bt, Intent data)
{
super.onActivityResult(request_enable_bt, result_enable_bt, data);
if (result_enable_bt == RESULT_OK)
{
Toast.makeText(this, "Turned On", Toast.LENGTH_SHORT).show();
}
else if (result_enable_bt == RESULT_CANCELED)
{
Toast.makeText(this, "Didn't Turn On", Toast.LENGTH_SHORT).show();
finish();
}
}
When you start your app,the system will remind you to turn on your Bluetooth,because your request Bluetooth on in onCreate method.When you deny the request, and the method onActivityResult call back,you will run the following code:
else if (result_enable_bt == RESULT_CANCELED)
{
Toast.makeText(this, "Didn't Turn On", Toast.LENGTH_SHORT).show();
finish();
}
The finish() will finish your Activity,every time when you click your app,when you click deny first time,and then you click deny second time.Every time you got this: onCreate -> onResume.So you get the request to enable your Bluetooth twice!
here is the code provided by the official guide, while this is a snippet causing problems.
#Override
public void onConnectionFailed(ConnectionResult result) {
if (mResolvingError) {
// Already attempting to resolve an error.
return;
} else if (result.hasResolution()) {
try {
mResolvingError = true;
result.startResolutionForResult(this, REQUEST_RESOLVE_ERROR);
} catch (IntentSender.SendIntentException e) {
// There was an error with the resolution intent. Try again.
mGoogleApiClient.connect();
}
} else {
// Show dialog using GooglePlayServicesUtil.getErrorDialog()
mResolvingError = true;
GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this, REQUEST_RESOLVE_ERROR)
.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
mResolvingError = false;
}
});
}
}
If I use it in a Service, when you read the variable this passed as argument to those functions, they expect an Activity type.
How should I do? It's a Service.
For the same reason I can't get activity result
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_RESOLVE_ERROR) {
mResolvingError = false;
if (resultCode == RESULT_OK) {
// Make sure the app is not already connected or attempting to connect
if (!mGoogleApiClient.isConnecting() &&
!mGoogleApiClient.isConnected()) {
mGoogleApiClient.connect();
}
}
}
}
This answer assumes your service is a "started" service. If it is a bound service or intent service, indicate that in a comment and I'll update the description and code included here.
The solution I suggest is to implement the activity shown below to handle the resolution UI. Replace the onConnectionFailed() method in your service with this code to hand off the resolution processing to the ResolverActivity:
#Override
public void onConnectionFailed(ConnectionResult result) {
Intent i = new Intent(this, ResolverActivity.class);
i.putExtra(ResolverActivity.CONNECT_RESULT_KEY, result);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}
Add the activity shown below to your app. When the connection request in your service fails, the connection result, which is a Parcelable, is passed to the activity. The activity handles the resolution UI and when finished, returns the status to the service as an intent extra. You will need to modify the code in your service's onStartCommand() to examine the extras in the intent to determine if it is being called to start the service for the first time, or to receive resolution status from the ResolverActivity.
An enhancement to this approach would be to post a notification with a PendingIntent for ResolverActivity instead of launching the activity immediately. That would give the user the option of deferring resolution of the connection failure.
public class ResolverActivity extends AppCompatActivity {
public static final String TAG = "ResolverActivity";
public static final String CONNECT_RESULT_KEY = "connectResult";
public static final String CONN_STATUS_KEY = "connectionStatus";
public static final int CONN_SUCCESS = 1;
public static final int CONN_FAILED = 2;
public static final int CONN_CANCELLED = 3;
// Request code to use when launching the resolution activity
private static final int REQUEST_RESOLVE_ERROR = 1111;
private static final String ERROR_CODE_KEY = "errorCode";
private static final String DIALOG_FRAG_TAG = "errorDialog";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "onCreate()");
// No content needed.
//setContentView(R.layout.activity_main);
Intent i = getIntent();
ConnectionResult result = i.getParcelableExtra(CONNECT_RESULT_KEY);
if (result.hasResolution()) {
try {
Log.i(TAG, "Starting error resolution...");
result.startResolutionForResult(this, REQUEST_RESOLVE_ERROR);
} catch (IntentSender.SendIntentException e) {
// There was an error with the resolution intent.
sendStatusToService(CONN_FAILED);
finish();
}
} else {
// Show dialog using GooglePlayServicesUtil.getErrorDialog()
ErrorDialogFragment.newInstance(result.getErrorCode())
.show(getSupportFragmentManager(), DIALOG_FRAG_TAG);
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent result) {
if (requestCode == REQUEST_RESOLVE_ERROR) {
if (resultCode == RESULT_OK) {
Log.i(TAG, "onActivityResult(): Connection problem resolved");
sendStatusToService(CONN_SUCCESS);
} else {
sendStatusToService(CONN_CANCELLED);
Log.w(TAG, "onActivityResult(): Resolution cancelled");
}
// Nothing more to do in this activity
finish();
}
}
private void sendStatusToService(int status) {
Intent i = new Intent(this, MyGoogleApiService.class);
i.putExtra(CONN_STATUS_KEY, status);
startService(i);
}
// Fragment to display an error dialog
public static class ErrorDialogFragment extends DialogFragment {
public static ErrorDialogFragment newInstance(int errorCode) {
ErrorDialogFragment f = new ErrorDialogFragment();
// Pass the error that should be displayed
Bundle args = new Bundle();
args.putInt(ERROR_CODE_KEY, errorCode);
f.setArguments(args);
return f;
}
#Override
#NonNull
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Get the error code and retrieve the appropriate dialog
int errorCode = getArguments().getInt(ERROR_CODE_KEY);
return GooglePlayServicesUtil.getErrorDialog(
errorCode, getActivity(), REQUEST_RESOLVE_ERROR);
}
#Override
public void onDismiss(DialogInterface dialog) {
Log.i(TAG, "Dialog dismissed");
}
}
}