Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Below is a sample custom achievement. It uses an alert dialog to display the achievement information. This is meant to illustrate how to make custom achievement API calls and shouldn't be used in a real app.
package test.com.sessionm.app;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.util.Log;
import com.sessionm.api.AchievementData;
import com.sessionm.api.AchievementActivity;
import com.sessionm.api.AchievementActivityIllegalStateException;
import com.sessionm.api.SessionM;
public class DialogAchievementActivity extends AchievementActivity {
private AlertDialog dialog;
private Activity presentingActivity;
public DialogAchievementActivity(Activity activity, AchievementData achievement) {
super(achievement);
presentingActivity = activity;
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle(achievement.getName());
builder.setMessage(String.format("%s\n+%d mPoints", achievement.getMessage(), achievement.getMpointValue()));
builder.setPositiveButton("Claim", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
DialogAchievementActivity.this.claim();
}
});
builder.setNegativeButton("Dismiss", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
DialogAchievementActivity.this.dismiss();
}
});
dialog = builder.create();
}
public void present() {
if (dialog != null && !dialog.isShowing()) {
presentingActivity.runOnUiThread(new Runnable() {
public void run() {
dialog.show();
try {
DialogAchievementActivity.this.notifyPresented();
} catch (AchievementActivityIllegalStateException e) {
Log.e(SessionM.TAG, "Exception while presenting native achievement." + e);
}
}
});
}
}
public void claim() {
presentingActivity.runOnUiThread(new Runnable() {
public void run() {
dialog.dismiss();
try {
DialogAchievementActivity.this.notifyDismissed(AchievementDismissType.CLAIMED);
} catch (AchievementActivityIllegalStateException e) {
Log.e(SessionM.TAG, "Exception while claiming native achievement." + e);
}
}
});
}
public void dismiss() {
presentingActivity.runOnUiThread(new Runnable() {
public void run() {
dialog.dismiss();
try {
DialogAchievementActivity.this.notifyDismissed(AchievementDismissType.CANCELLED);
} catch (AchievementActivityIllegalStateException e) {
Log.e(SessionM.TAG, "Exception while dismissing native achievement." + e);
}
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment