I'm managing the callback this way but the result is always success when it shouldn't.
Example:
I search a file that doesn't exist and it doesn't show the log
I search a file when disconnected from the internet and still it doesn't show the log
Even if i create a file on drive when disconnected from the internet and set a callback for its creation, the result is success.
Here is the callback code
ResultCallback<DriveApi.DriveContentsResult> searchCallback =
new ResultCallback<DriveApi.DriveContentsResult>() {
#Override
public void onResult(DriveApi.DriveContentsResult result) {
if (!result.getStatus().isSuccess()) {
Log.e(TAG, "cant open file");
}
}
};
What am i doing wrong?
The Android API has offline support, so performing an operation while offline will not fail. Instead, any changes are queued up to occur when the device comes back online.
Similarly, searching for something that doesn't exist doesn't fail, it just returns an empty result.
You handling of success looks fine, you just aren't testing cases that will actually fail.
Related
I have an Android app that serves content via webview. I've implemented a network connectivity alert so if my users are in a location where they can't get internet, I show a message. This takes place inside the onReceivedError method. It works as expected; if I disable all internet connections, I get a warning message. However, even when internet connection exists, the warning message still shows. If I minimize my app, open the native browser and visit 8-10 links, when I switch back to my app, I am given the network warning even though the device is connected to the network.
This only happens on one of my 6 test devices (my current phone, a Samsung Galaxy S10e. The problem does not occur on 5 different Android devices, both newer and older. I'm just looking for a way to ignore the warning when the app is in the background state. I've searched for an easy way to do this, but I haven't found anything simple that doesn't involve writing a new class.
How can I write a conditional statement to do nothing when the onReceivedError method is triggered while the app is in the background? Here is a portion of my code, along with what I would like to have happen:
#override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
// the part I need help with
if ("app in background"){
// do nothing
} else {
// this is where I have employed my notification, a snackbar
}
}
Thank you for your help!
In my current Java project, it's easy to track server-side user events in the "old" Google Analytics Universal Project with simple REST calls to Google Analytics. So that location tracking was working, i could override the server ip with the user ip, according to the parameter "&uip=1.2.3.4" (https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters?hl=de#uip).
As upgrading to GA4 is recommended, I was able to change all the REST parameters in my project and show my events in the new dashboard, except for the user location. I can't find any information about such a parameter. I tried using still "uip" but now all my requests are located to the country of my server.
Unfortunately it's not possible to track the event client side, because my project is a simple REST API, returning only JSON data.
Does anyone have an idea, if there's such a parameter like "uip" for ga4 or if this isn't possible anymore?
In the following way I setup my parameters:
private String getQueryParameters(MeasurementEvent event) {
StringBuilder body = new StringBuilder();
body.append("?v=").append(version);
body.append("&tid=").append(trackingId);
body.append("&cid=").append(event.getClientId());
body.append("&en=").append(eventName);
body.append("&aip=1");
if (StringUtils.hasText(event.getAction())) {
body.append("&ep.useraction=").append(event.getAction());
}
if (StringUtils.hasText(event.getCategory())) {
body.append("&ep.awsregion=").append(event.getCategory());
}
if (StringUtils.hasText(event.getLabel())) {
body.append("&ep.softwarename=").append(event.getLabel());
}
if (StringUtils.hasText(event.getRemoteAddress())) {
body.append("&uip=").append(event.getRemoteAddress());
}
if (StringUtils.hasText(event.getUrl())) {
body.append("&dl=").append(event.getUrl());
}
return body.toString();
}
For the past few days i've been trying to show the online/offline status of a user.. For this i have a register activity where they register and their info gets saved in firebase and if they exit an activity i have overriden its onstop method and made the value to set to offline... but if the user suddenly loses internet connection it still shows online.. i cant change it to offline because internet is needed to make a change in the database and the use doesn't have internet... SO how do i set the database value to offline... i googled quite some stuff about this but didnt find anything... Can anyone please help me out please
My code
#Override
protected void onStart() {
super.onStart();
fetchData();
// mDatabaseReference.child("UserData").child(UID).child("Online").setValue("True");
}
#Override
protected void onStop() {
super.onStop();
fetchData();
// mDatabaseReference.child("UserData").child(UID).child("Online").setValue(false);
}
What you're trying to do is known as a presence system. The Firebase Database has a special API to allow this: onDisconnect(). When you attach a handler to onDisconnect(), the write operation you specify will be executed on the server when that server detects that the client has disconnected.
From the documentation on managing presence:
Here is a simple example of writing data upon disconnection by using the onDisconnect primitive:
DatabaseRef presenceRef = FirebaseDatabase.getInstance().getReference("disconnectmessage");
// Write a string when this client loses connection
presenceRef.onDisconnect().setValue("I disconnected!");
In your case this could be as simple as:
protected void onStart() {
super.onStart();
fetchData();
DatabaseReference onlineRef = mDatabaseReference.child("UserData").child(UID).child("Online");
onlineRef.setValue("True");
onlineRef.onDisconnect().setValue("False");
}
Note that this will work in simple cases, but will start to have problems for example when your connection toggles rapidly. In that case it may take the server longer to detect that the client disappears (since this may depends on the socket timing out) than it takes the client to reconnect, resulting in an invalid False.
To handle these situations better, check out the sample presence system in the documentation, which has more elaborate handling of edge cases.
I am using the Spotify Android SDK version spotifysdk-1.0.0-beta8.aar. I have a valid OAuth token which I am refreshing each hour successfully. This is how I am getting the Spotify Player Object initially
mPlayer = Spotify.getPlayer(playerConfig, this, new Player.InitializationObserver() {
#Override
public void onInitialized(Player player) {
player.addConnectionStateCallback(JukeSpotDashboardActivity.this);
player.addPlayerNotificationCallback(JukeSpotDashboardActivity.this);
player.login(utils.getPreferenceValue(getString(R.string.spotify_access_token)));
}
#Override
public void onError(Throwable throwable) {
Log.e(JukeSpotDashboardActivity.class.getName(),
"Could not initialize player: " + throwable.getMessage());
}
});
At the end of each hour the access token gets refreshed and stored in the shared preferences. How do you guys suggest I use the new access token in the same mPlayer object so that which ever song is being streamed currently won't get stopped and I won't need to recreate this Spotify player object?
I know there is the
mPlayer.login(accessToken);
function, but it did not work when I got the new accessToken and relogged in. Any idea what I am doing wrong?
Once logged in, your application will be able to stream until the application is stopped, the network connection is lost, or the user's premium account has ended. (There may be other events as well.) This means that you don't need to update the Player object with the refreshed access token to continue streaming.
Note that if you're making any other requests using an access token such as adding a track to a playlist, the access token must be updated in whatever tool you're using to perform the request. For example, if you're using kaaes' excellent Spotify Web API client for Android, you'd need to get a new SpotifyService instance based on the refreshed access token.
How can I register for SMS database changes?
I tried:
mCursor = mActivity.getContentResolver().query(Sms.CONTENT_URI, new String[] {
Sms.ADDRESS
}, null, null, null);
mCursor.registerDataSetObserver(mydataSetObserver);
where mydataSetObserver is implemented like this:
private class MyDataSetObserver extends DataSetObserver {
public void onChanged() {
System.out.println ("1");
}
public void onInvalidated() {
System.out.println ("2");
}
}
But when I tried sending a SMS message in the emulator,
MyDataSetObserver never get called.
Can you please tell me why?
Thank you.
It sounds like all you are trying to do is have the ability to make changes to the SMS database on the device.
The way I have done it in the past is by using tags in the AndroidManifest.xml. The application I made needed to use the READ_SMS permission as well as the READ_CONTACTS permission, however gaining permission for writing to the database would be done in the same way.
I defined these desired permissions in the AndroidManifest.xml file with the following tag:
Included in the list of permissions you can use is WRITE_SMS, which should give you the desired capability.
Please note: because I am a new user, StackOverflow would only let me post one hyperlink for this post, I tried including alot more information however was unable to do so. Please go to the android developer website and search for the AndroidManifest.xml file and see more info if need be.
DataSetObservers only observe DataSetObservables they are registered with. Your MyDataSetObserver is registered with your mCursor and will be notified whenever mCursor changes (e.g. after requery) but not when the content is written by another process (like the Messaging application).
Unfortunately there is currently no good way to listen for the event of sent text messages, the best alternative seems to be polling content://sms/sent, potentially using a ContentObserver.
This question is related.