Android example to build an app that will access your data on pryv.io using the Pryv Java library
This sample app contains the code that provides the login to your pryv account on the platform that you will have defined here. For example, when using the pryv.me demo platform:
public final static String DOMAIN = "pryv.me";
After signing in your account the app is able to fetch all Events from a Stream and create a text note on the push of a button.
Get the code git clone https://github.com/pryv/example-app-android
To use the example app in Android Studio, go to File>open
and select the folder that was generated by the git clone
. Now run in on your emulated device or Android phone.
If you already developed your own Android app and you want to integrate Pryv into it, here is a concise procedure to setup the only elements that you will need and in the shortest time.
First of all, you need to include the Pryv Java library in your project.
Moreover, do not forget to add the Internet permission in your AndroidManifest.xml as follow:
<uses-permission android:name="android.permission.INTERNET" />
You will need to copy the following classes in your project: LoginActivity and Credentials. Do not forget to declare the LoginActivity in your manifest and also copy the corresponding xml file.
LoginActivity will handle all the process of account creation and login through a WebView. As soon as a login is successful, a pair of username and token will be stored in the Android SharedPreferences using a Credentials object.
You can load the LoginActivity on the push of a login button for example and check if the user is log by calling the function hasCredentials() from your Credentials object.
Note that you can modify LoginActivity to adapt the domain and app ID to be used:
public final static String DOMAIN = "pryv.me";
public final static String APPID = "app-android-example";
There is still few necessary additions to be made in your MainActivity.
First of all, you need to initialize the connection by providing the credentials previously stored during the login phase:
Credentials credentials = new Credentials(MainActivity.this);
Connection connection = new Connection(credentials.getUsername(), credentials.getToken(), LoginActivity.DOMAIN);
You can then follow the end of the Getting-started:java guide in order to manage your Pryv resources.
Note that Android enforces HTTP calls to the Pryv API (Get, Create, Update, Delete) to be executed in a different thread. Thus, you could encapsulate them in an AsyncTask or use the following basic alternative:
new Thread() {
public void run() {
// Do you HTTP calls to the Pryv API here (Get, Create, Update, Delete)
}
}.start();
In the same spirit, Android also verify that only the thread that created a View can modify it. Thus, if we want our updateStatusText function to update the UI, we have to force it to run on the UI thread in the following way:
private void updateStatusText(final String text) {
runOnUiThread(new Runnable() {
@Override
public void run() {
progressView.setText(text);
}
});
}
If you still have misunderstandings when integrating Pryv into your app or if you want to see more concrete examples, do not hesitate to take a look at the sample MainActivity.
Pryv provides this software for educational and demonstration purposes with no support or warranty.