Sunday, April 20, 2014
How to Add End User License Agreement to Android App
1. Open your application's '/android/src' folder and create a new file. Name it 'Eula.java' and open it in a text editor, such as WordPad.
2. Insert the following code in the Eula.java file://package com.google.android.divideandconquer;
package org.warmux;import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.Closeable;class Eula {
private static final String ASSET_EULA = 'EULA';
private static final String PREFERENCE_EULA_ACCEPTED = 'eula.accepted';
private static final String PREFERENCES_EULA = 'eula';
static interface OnEulaAgreedTo {
void onEulaAgreedTo();
}
static boolean show(final Activity activity) {
final SharedPreferences preferences = activity.getSharedPreferences(PREFERENCES_EULA,
Activity.MODE_PRIVATE);
if (!preferences.getBoolean(PREFERENCE_EULA_ACCEPTED, false)) {
final AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle(R.string.eula_title);
builder.setCancelable(true);
builder.setPositiveButton(R.string.eula_accept, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
accept(preferences);
if (activity instanceof OnEulaAgreedTo) {
((OnEulaAgreedTo) activity).onEulaAgreedTo();
}
}
});
builder.setNegativeButton(R.string.eula_refuse, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
refuse(activity);
}
});
builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
public void onCancel(DialogInterface dialog) {
refuse(activity);
}
});
builder.setMessage(readEula(activity));
builder.create().show();
return false;
}
return true;
}
private static void accept(SharedPreferences preferences) {
preferences.edit().putBoolean(PREFERENCE_EULA_ACCEPTED, true).commit();
}
private static void refuse(Activity activity) {
activity.finish();
}
private static CharSequence readEula(Activity activity) {
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(activity.getAssets().open(ASSET_EULA)));
String line;
StringBuilder buffer = new StringBuilder();
while ((line = in.readLine()) != null) buffer.append(line).append('\n');
return buffer;
} catch (IOException e) {
return '';
} finally {
closeStream(in);
}
}
private static void closeStream(Closeable stream) {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
// Ignore
}
}
}
}
3. Save and close the file. This Java code displays the Eula text and draws two buttons: Accept and Decline. If the user clicks Accept, the application starts. If he clicks Decline, the app closes.
4. Open your application's Assets folder and create a new text file. Name it 'EULA' and do not include any extension.
5. Open the EULA file in any text editor and insert your end user license agreement text.
6. Save and close the EULA file.
7. Open your application's MainActivity.java file in any text editor.
8. Locate the 'protected void onCreate(Bundle savedInstanceState)' function and insert this code between its '{' and '}' brackets:Eula.show(this);
9. Save and close the MainActivity.java file.
10. Open the 'res/values' folder and open the strings.xml file in any text editor.
11. Add this code to the end of the file, before the '
' line:
License
Accept
Decline
12. Save and close the strings.xml file.
13. Build the Android app and test it. The EULA is displayed the first time you launch it. If you select 'Accept,' the app starts. If you select 'Decline,' the app closes.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment