Android Shared Preferences Updating - java

I have two projects/packages part of the same application. The main project of the application does virtually everything on the app. However, we used a 2nd project to manage updates to the application. We are using shared preferences that are updated from the sqlite3 database and applied in the main application using
editor.putString("string", sString).apply();
We also use the following logic to see if its a new version and if so restart the autoupdate package
if (!sCurrentVersion.equals(ver)) {
Intent intent1 = new Intent(Intent.ACTION_MAIN);
PackageManager manager = getPackageManager();
intent1 = manager.getLaunchIntentForPackage("com.pack.autoupdate");
intent1.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(intent1);
}
Then in the autoupdate package, we simply just use getSharedPreferences(). The values we need in the shared preferences are the update urls that the autoupdate package needs to check.
Our problem is that we updated those urls in the database, however we are still getting some devices that are using the old urls in the autoupdate check. Do I need to make sure and restart autoupdate or is there something i'm doing wrong with sharedpreferences?

Did you do call the .commit() method to update your SharedPreference value?
Update :
More information regarding the difference between .commit() and .apply() Here

Related

Published app making 2 shortcuts and debug 1

I have published my app now and found that it is creating two shortcut icons where as when I install through android studio it creates only one shortcut. I have added duplicate false and sharedpreference has also been used to check once icon is created. Why the app behaving different and how can I fix it now? This is my code for creating shortcut.
public void createShortCut() {
SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(StartupActivity.this).edit();
editor.putBoolean("shortcut", true).apply();
Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
shortcutintent.putExtra("duplicate", false);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Smart App");
Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.mipmap.ic_launcher);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplicationContext(), SplashScreen.class));
sendBroadcast(shortcutintent);
}
and before calling above method I have below code which runs on activity start.
if (!sharedPreferences.getBoolean("shortcut", false)) {
createShortCut();
}
When you install from Android Studio (directly from an .apk), no shortcut is made. However, apps installed from the Google Play Store will automatically sometimes create a shortcut after installation.
So when a user installs your app from the play store, two shortcuts are made, one from your app and one from the installation.
EDIT: This solution might prove useful to you: How to detect shortcut in Home screen

Initiliazing ParseLoginUI?

Where does one actually place the code to launch the ParseLoginUI activity?
ParseLoginBuilder builder = new ParseLoginBuilder(MainActivity.this);
startActivityForResult(builder.build(), 0);
Is it in the ParseLoginDispatchActivity? This was not made very clear at all within any of the official documentation:
https://github.com/ParsePlatform/ParseUI-Android
https://www.parse.com/docs/android/guide#user-interface
I'm importing ParseLoginUI into my existing app. What do I once I've installed everything, updated my manifests, my build.gradle and now want to actually launch the Login activity once my app launches?
Do I put something in my manifest to indicate that the ParseLoginActivity should launch first? That doesn't seem to work as an Activity from my main application is required to launch as the initial intent. I'm a little lost here... Any thoughts?
Well I did find one solution, albeit a trivial one:
Intent loginIntent = new Intent(MainActivity.this, ParseLoginActivity.class); startActivity(loginIntent);
I launched the above Intent with an options menu item, but you could do it with a button or whatever else suits your needs.
If you're importing ParseLoginUI into an existing app, it appears you can just launch ParseLoginActivity with a simple Intent. I wish they mentioned this on their integration tutorial. Seems like the most straightforward way to get it running.
This solution definitely launches the Activity you want, but it doesn't check for whether the user is logged in or not and hence doesn't redirect you to the appropriate pages in your log-in flow (which I believe has more to do with your Manifest). It does, however, allow you to successfully register a user and log in with Parse, which is a great start.
A better solution would be to add the following to the onCreate method in the Activity that launches when your app launches. So if when your app launches you land on FirstActivity, the following will check to see if you are logged in. If you are not, you will be sent the login screen, and if you are logged in you will be sent to the second Activity, which is presumably where your users will want to be when they open your app.
ParseUser currentUser = ParseUser.getCurrentUser();
if (currentUser != null) {
Intent launchMainActivity = new Intent(this, SecondActivity.class);
startActivity(launchMainActivity );
} else {
ParseLoginBuilder builder = new ParseLoginBuilder(FirstActivity.this);
startActivityForResult(builder.build(), 0);
}

Android - Call an activity of an integrated application

Mixare has an application (Open source) that lets you view POIs with your camera. It gives you the possibility to call the app from your application thanks to this :
Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
i.setDataAndType(Uri.parse("http://ws.geonames.org/findNearbyWikipediaJSON"), "application/mixare-json");
startActivity(i);
The problem is that user must have the app installed in addition to my app, so what I did is that I imported the whole app within mine, with all its resources and stuff.
But I don't know how to call the main activity MainActivity.java, which resides in the package org.mixare.
How can I make an intent to call this activity ? And how do I declare it in the manifest ?
If you have added the code and resources of the app to your own app, then you should declare and call it's activities as they were your own.
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
This being said, it's not a trivial task. You need to merge AndroidManifest and could get into trouble if you don't know what you're doing. For instance, user can have the Mixare app in addition to yours and intent could have same actions etc.
There is an alternative to this. You could check if Mixare app is installed and if not ask user to do so. This could be more "android way of doing things", depending on your use case.
Look at,
http://code.google.com/p/mixare/wiki/DisplayYourOwnData for how to start mixare via Intent.
Alternatively, you can use mixare as your library project and then call its MainActivity class directly from your application as Using an Android library project Activity within another project.
Quoting the same here -
Declaring library components in the manifest file
In the manifest file of the application project, you must add
declarations of all components that the application will use that are
imported from a library project. For example, you must declare any
, , , , and so on, as well as
, , and similar elements.
Declarations should reference the library components by their
fully-qualified package names, where appropriate.
Then you can definitely call,
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
No, it is very hard to do a 2+2=4 kind of addition of manifest files etc.
I see there are two ways to handle this:
Use the external app: Check if the user has external app you want him to have. Else, direct him to the right link. You can get the package name of the publiched app and use it in this function:
private boolean appInstalledOrNot(String uri) {
PackageManager pm = getPackageManager();
boolean app_installed = false;
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
app_installed = true;
}
catch (PackageManager.NameNotFoundException e) {
app_installed = false;
}
return app_installed ;
}
Combining code: This has no direct/correct answer. You need to study the code and integrate with your existing one.
//appPackageName,appClassName can be found in Logcat
ComponentName component = new ComponentName("appPackageName","appClassName");
Intent intent = new Intent();
intent.setComponent(component);
startActivity(intent);

Mixare - How to display local data

After a make a call for a web service to get POIs from a database and show them in a list, I then click a button that opens up the camera displaying these POIs thanks to Mixare that I imported into my app. The problem is that Mixare only accepts URLs to be passed on to get data. Like so :
Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
i.setDataAndType(Uri.parse("http://ws.geon ames.org/findNearbyWikipediaJSON"), "application/mixare-json");
startActivity(i);
Has anyone succeeded to use local data in Mixare ?
NB: I can't make another call to the web service, I want to be able to use the data I already have

Getting Preferences API working on both Android and PC

I'd like to save user preferences from a sketch running either on a PC or an Android phone, using the same code in as standard "Java-way" as possible.
Ideal candidate for my purposes seems to be the java.util.prefs.Preferences class. So, I wrote a little test script to see if it's working in processing:
String prId = "counter";
Preferences prefs = Preferences.userNodeForPackage(this.getClass());
int counter = prefs.getInt(prId, 0);
println(counter);
prefs.putInt(prId, 1+counter);
This program outputs an increasing number each time it is executed - on a PC. On Android, the default value (0) is always shown.
Are there any additional steps required to get this working on Android? Permissions to be requested?
Are there any alternatives for saving name - value pairs on both platforms?
Obs.: "WRITE_EXTERNAL_STORAGE" permission is already enabled
This is a known Android bug unfortunately: https://code.google.com/p/android/issues/detail?id=22232
Basically the app does not have write permissions for the generated path, which should be a system-specific location where the user has write permissions instead.
Simplest (cross platform) workaround could be to use java.util.Properties, where you have control over the storage location.
Another workaround (if you are tied to the Preferences API for some reason) might be to provide your own implementation of AbstractPreferences (perhaps backed by Android SharedPreferences?), see this SO post: Is there a way to use java.util.Preferences under Windows without it using the Registry as the backend?
P.S.: Another workaround option might be to explicitly export / import the data using Preferences.exportSubtree(OutputStream os) and
Preferences.importPreferences(InputStream is)
In Android, the preferred way to store preferences is to make use of SharedPreferences. The equivalent code would be like this:
String prId = "counter";
SharedPreferences prefs = getPreferences(MODE_PRIVATE);
int counter = prefs.getInt(prId,0); // Get int, default is 0
SharedPreferences.Editor prefsEditor = prefs.edit();
prefsEditor.putInt(prId, counter++); // Put counter back
prefsEditor.commit(); //Don't forget to commit the changes
I don't know exactly why java.util.prefs.Preferences would fail in Android. I suspect the reason is data would be deleted after the current Activity or the application is destroyed.
Because directory structure is different on each platform, it's hard to get preferences by just using one single same method. For example, data is stored in /.java/.userPrefs/**yourpacakgename**/prefs.xml on Android while it's in Registry on Windows and in ~/Library/Preferences/**yourpacakgename**/prefs.xml on Mac OS X. Also, you can't use Preferences.userRoot() in Android because an application cannot get root access.

Categories