onActivityResult() never gets called - java

I have a class used as interface inside my MainActivity.java:
public class prova{
prova(){
}
#JavascriptInterface
public void displayGPSRequest() {
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
#Override
public void onResult(LocationSettingsResult result) {
final Status status = result.getStatus();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
Log.i(TAG, "All location settings are satisfied.");
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
Log.i(TAG, "Location settings are not satisfied. Show the user a dialog to upgrade location settings ");
try {
// Show the dialog by calling startResolutionForResult(), and check the result
// in onActivityResult().
status.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException e) {
Log.i(TAG, "PendingIntent unable to execute request.");
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
Log.i(TAG, "Location settings are inadequate, and cannot be fixed here. Dialog not created.");
break;
}
}
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
//final LocationSettingsStates states = LocationSettingsStates.fromIntent(data);
switch (requestCode)
{
case REQUEST_CHECK_SETTINGS:
switch (resultCode)
{
case Activity.RESULT_OK:
{
// All required changes were successfully made
Toast.makeText(MainActivity.this, "Location enabled by user!", Toast.LENGTH_LONG).show();
break;
}
case Activity.RESULT_CANCELED:
{
// The user was asked to change settings, but chose not to
Toast.makeText(MainActivity.this, "Location not enabled, user cancelled.", Toast.LENGTH_LONG).show();
break;
}
default:
{
break;
}
}
break;
}
}
});
}
}
Inside the function displayGPSRequest() I have startResolutionForResult() that should call the method onActivityResult() but it never does. I tried to see other posts where they used fragments but I don't have really understood them. I hope you can help with this.

onActivityResult should be overriden in the associated Activity - currently you're just declaring a method in the ResultCallback which never gets called.

Related

addOnFailureListener() doesnot call if user once enabled and after sometime disable location. when using LocationServices.getSettingsClient()

I have used LocationServices.getSettingsClient() to remove depricated
LocationServices.SettingsApi.checkLocationSettings().
It is asking user to enable location
first time when mainActivity launch if location is disabled.
If in between i disable location . it is not asking to enable location. means
result.addOnSuccessListener is called even if my location is disabled.
if Location is disabled inbetweeen it should ask result.addOnFailureListener
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(getContext());
mSettingsClient = LocationServices.getSettingsClient(getContext());
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
builder.addLocationRequest(mLocationRequest);
mLocationSettingsRequest = builder.build();
mLocationService = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
Task<LocationSettingsResponse> result =
LocationServices.getSettingsClient(getContext()).checkLocationSettings(builder.build());
result.addOnSuccessListener(getActivity(), new OnSuccessListener<LocationSettingsResponse>() {
#Override
public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
LocationSettingsResponse response =
task.getResult(ApiException.class);
LocationSettingsStates locationSettingsStates = response.getLocationSettingsStates();
if (!locationSettingsStates.isGpsPresent() || !locationSettingsStates.isGpsUsable()) {
//check point 4
}
});
result.addOnFailureListener(getActivity(), new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
if (e instanceof ResolvableApiException) {
// Location settings are not satisfied, but this can be fixed
// by showing the user a dialog.
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
ResolvableApiException resolvable = (ResolvableApiException) e;
resolvable.startResolutionForResult(getActivity(),
REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException sendEx) {
// Ignore the error.
}
}
}
});
here I want need to open location enabling dialog using following code:
final Status status = result.getStatus();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
Log.i(TAG, "All location settings are satisfied.");
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
Log.i(TAG, "Location settings are not satisfied. Show the user a dialog to upgrade location settings ");
try {
// Show the dialog by calling startResolutionForResult(), and check the result
// in onActivityResult().
status.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException e) {
Log.i(TAG, "PendingIntent unable to execute request.");
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
Log.i(TAG, "Location settings are inadequate, and cannot be fixed here. Dialog not created.");
break;
}
5 .but using SettingsClient onSucess gives
Task<LocationSettingsResponse> result which does not give statusCode that I recieve in catch block
Update
First Tag is try part of OnComplete() when location is by default on
second time I turn off location and go to other Fragment and again came to main fragment.
Tt executed try part even I location was disabled

App goes to the background after running onActivityResult

I am using FirebaseUI to log in to my app. On returning from the login activity, the following onActivityResult-override prints Here1-1, which is as expected. However, immediately after, the activity finishes, which is not what I want. I feel really lost, does anyone have any idea what could be happening?
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode,resultCode,data);
switch (requestCode) {
case RC_SIGN_IN:
if (resultCode == RESULT_CANCELED) {
finish();
} else {
System.out.println("Here" + Integer.toString(requestCode) + Integer.toString(resultCode));
}
case RC_START_APP:
if (resultCode == RESULT_LOGOUT) {
signout();
} else {
finish();
}
}
}
Your cases need a break to stop execution so they don't fall through to the next case. Your activity is actually just finishing due to executing the finish() in the second RC_START_APP case.
switch (requestCode) {
case RC_SIGN_IN:
if (resultCode == RESULT_CANCELED) {
finish();
} else {
System.out.println("Here" + Integer.toString(requestCode) + Integer.toString(resultCode));
}
break; // add this break
case RC_START_APP:
if (resultCode == RESULT_LOGOUT) {
signout();
} else {
finish();
}
break; // add this break
}

Update location in background and check LocationSettings

I would like to update user's location every few minutes in background.I used this sample code googlesamples/android-play-location
but changing it to use with Service
public class MyService extends Service implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener, ResultCallback<LocationSettingsResult>
but I can't check location settings
protected void checkLocationSettings() {
PendingResult<LocationSettingsResult> result =
LocationServices.SettingsApi.checkLocationSettings(
mGoogleApiClient,
mLocationSettingsRequest
);
result.setResultCallback(this);
}
#Override
public void onResult(LocationSettingsResult locationSettingsResult) {
final Status status = locationSettingsResult.getStatus();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
Log.i(TAG, "All location settings are satisfied.");
startLocationUpdates();
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
Log.i(TAG, "Location settings are not satisfied. Show the user a dialog to" +
"upgrade location settings ");
try {
// Show the dialog by calling startResolutionForResult(), and check the result
// in onActivityResult().
status.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException e) {
Log.i(TAG, "PendingIntent unable to execute request.");
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
Log.i(TAG, "Location settings are inadequate, and cannot be fixed here. Dialog " +
"not created.");
break;
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
// Check for the integer request code originally supplied to startResolutionForResult().
case REQUEST_CHECK_SETTINGS:
switch (resultCode) {
case Activity.RESULT_OK:
Log.i(TAG, "User agreed to make required location settings changes.");
startLocationUpdates();
break;
case Activity.RESULT_CANCELED:
Log.i(TAG, "User chose not to make required location settings changes.");
break;
}
break;
}
}
Because this method requires an Activity
status.startResolutionForResult(MainActivity.this,
REQUEST_CHECK_SETTINGS);
and if I put the previous code in the MainActvity, I would not have the references to mGoogleApiClient and mLocationSettingsRequest
LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient,
mLocationSettingsRequest);
I would like to upgrade and save in background the position even if the activity is destroyed.
Use the service is the correct way to do this?
And how do I check the location settings?
[EDIT]
I tried to use the PendingIntent and an IntentService
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,
mLocationRequest, mPendingIntent);
This method is suited for the background use cases, more specifically
for receiving location updates, even when the app has been killed by
the system.
What is the difference compared to the version with the Service and LocationListner?
I found a solution for my problem.
To access the Main Activity I used the method
MainActivity.getInstance()
MainActivity
public class MainActivity extends AppCompatActivity {
private static MainActivity instance;
public static MainActivity getInstance() {
Log.i("MainActivity", "getInstance");
return instance;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Instance of the MainActivity
instance = this;
}
...
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i(TAG, "onActivityResult");
switch (requestCode) {
// Check for the integer request code originally supplied to startResolutionForResult().
case REQUEST_CHECK_SETTINGS:
switch (resultCode) {
case Activity.RESULT_OK:
Log.i(TAG, "User agreed to make required location settings changes.");
startService(intent);
break;
case Activity.RESULT_CANCELED:
Log.i(TAG, "User chose not to make required location settings changes.");
break;
}
break;
}
}
}
Service
#Override
public void onResult(LocationSettingsResult locationSettingsResult) {
final Status status = locationSettingsResult.getStatus();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
// All location settings are satisfied. The client can
// initialize location requests here.
Log.i(TAG, "All location settings are satisfied.");
startLocationUpdates();
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
// Location settings are not satisfied, but this can be fixed
// by showing the user a dialog.
Log.i(TAG, "Location settings are not satisfied. Show the user a dialog to " +
"upgrade location settings ");
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
status.startResolutionForResult(MainActivity.getInstance(), REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException e) {
// Ignore the error.
Log.i(TAG, "PendingIntent unable to execute request.");
}
break;
// The status will never be SETTINGS_CHANGE_UNAVAILABLE if use builder.setAlwaysShow(true);
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
// Location settings are not satisfied. However, we have no way
// to fix the settings so we won't show the dialog.
Log.i(TAG, "Location settings are inadequate, and cannot be fixed here. Dialog " +
"not created.");
break;
}
}

How do I switch layout when connected to a Bluetooth device in a Handler?

So I would like to change the layout once i'm successfully connected to a Bluetooth device. I would like to do this in my Handler, but I don't know how. As you can see in my Handler code below , I've tried this in my case BTHandler.STATE_CONNECTED: (this is just copied from BluetoothChat) but I dont know how.
private Handler mHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
switch (msg.what) {
case Constants.MESSAGE_STATE_CHANGE:
switch (msg.arg1) {
case BTHandler.STATE_CONNECTED:
setStatus(getString(R.string.title_connected_to, mConnectedDeviceName));
mConversationArrayAdapter.clear();
break;
case BTHandler.STATE_CONNECTING:
Toast.makeText(getApplicationContext(), "Connecting…", Toast.LENGTH_SHORT).show();
Log.v("Log", "connecting");
break;
case BTHandler.STATE_LISTEN:
case BTHandler.STATE_NONE:
Toast.makeText(getApplicationContext(), "Connected", Toast.LENGTH_LONG).show();
Log.v("Log", "connected");
break;
}
}
}
};
Solved the problem by myself.
guiHandler(Constants.CONNECTION_STATUS, Constants.STATE_CONNECTED, "");
This is written in my run method that's inside a ConnectThread.

Android application getting killed because of incoming call

In my application I am copying all the Call Logs into a Database in an Async Task. But If a call comes in between my application can get terminated leading to incomplete data. So first of all I want to stop my application from getting terminated in such scenario, so what should I do? Also I want to keep track of cursor such that when application is opened again it will continue from the exact same place where it stopped and complete the job.
This is the code that helped me save my game state during incoming call.. Hope it helps you too...
TelephonyManager tm;
private PhoneStateListener mPhoneListener = new PhoneStateListener() {
public void onCallStateChanged(int state, String incomingNumber) {
try {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
Toast.makeText(GameActivity.this, "CALL_STATE_RINGING", Toast.LENGTH_SHORT).show();
//Your function to save state right here...
stopTimer();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Toast.makeText(GameActivity.this, "CALL_STATE_OFFHOOK", Toast.LENGTH_SHORT).show();
break;
case TelephonyManager.CALL_STATE_IDLE:
Toast.makeText(GameActivity.this, "CALL_STATE_IDLE", Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(GameActivity.this, "default", Toast.LENGTH_SHORT).show();
Log.i("Default", "Unknown phone state=" + state);
}
} catch (Exception e) {
Log.i("Exception", "PhoneStateListener() e = " + e);
}
}
};

Categories