为Android APP添加最终用户许可协议

对一个android app来说,加入EULA已经是一个必不可少的内容了。下面将演示如何创建一个通用的Eula类。任何一个app都可以使用Eula.show()来显示自己的EULA,也可以通过实现Eula.OnEulaAgreedTo接口做进一步处理。

Eula.java

/**
最终用户协议文件名由ASSERT_EULA定义,存放位置为项目的assets目录
*/
public class Eula {
	private static final String ASSERT_EULA = "EULA";
	private static final String PREFERENCE_EULA_ACCEPTED = "eula.accepted";
	private static final String PREFERENCE_EULA = "eula";
	/**
        当用户选择同意后的回调函数。
        */
	static interface OnEulaAgreedTo {
		void onEulaAgreedTo();
	}
	
	private static void accept(SharedPreferences preferences) {
		preferences.edit().putBoolean(PREFERENCE_EULA_ACCEPTED, true).commit();
	}
	
	private static void refuse(Activity activity) {
		activity.finish();
	}
	
	private static void closeStream(Closeable stream) {
		if(null != stream) {
			try {
				stream.close();
			} catch(IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	private static CharSequence readEula(Activity activity) {
		BufferedReader in = null;
		try {
			in = new BufferedReader(new InputStreamReader(activity.getAssets().open(ASSERT_EULA)));
			String line;
			StringBuilder buffer = new StringBuilder();
			while((line = in.readLine()) != null) {
				buffer.append(line).append("\n");
			}
			return buffer;
		} catch(IOException e) {
			e.printStackTrace();
			return "";
		} finally {
			closeStream(in);
		}
	}
	
	static boolean show(final Activity activity) {
		final SharedPreferences preferences = activity.getSharedPreferences(PREFERENCE_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() {
				
				@Override
				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() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					refuse(activity);
				}
			});
			builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
				
				@Override
				public void onCancel(DialogInterface dialog) {
					refuse(activity);
				}
			});
			builder.setMessage(readEula(activity));
			builder.create().show();
			return false;
		}
		return true;
	}
}

使用上面的EULA类: 测试的时候,你可能需要删除或者修改/data/目录下app对应的eula.xml文件,因为这里把用户的授权信息存放在了SharedPreferences中,而这是个永久存储的方法。 EulaExample.java

public class EulaExample extends Activity implements Eula.OnEulaAgreedTo {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_eula_example);
		Eula.show(this);
	}

	@Override
	public void onEulaAgreedTo() {
		Toast.makeText(this, "Think you !", Toast.LENGTH_SHORT).show();
	}
}