Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Rating
- {
- private final static String APP_TITLE = "AppName";
- private final static String APP_PNAME = "com.yourapp.appname";
- private final static int DAYS_UNTIL_PROMPT = 3;
- private final static int LAUNCHES_UNTIL_PROMPT = 5;
- public static void app_launched(Context _context)
- {
- SharedPreferences prefs = _context.getSharedPreferences("apprater", 0);
- if (prefs.getBoolean("dontshowagain", false))
- {
- return;
- }
- SharedPreferences.Editor editor = prefs.edit();
- // Increment launch counter
- long launch_count = prefs.getLong("launch_count", 0) + 1;
- editor.putLong("launch_count", launch_count);
- // Get date of first launch
- Long date_firstLaunch = prefs.getLong("date_firstlaunch", 0);
- if (date_firstLaunch == 0)
- {
- date_firstLaunch = System.currentTimeMillis();
- editor.putLong("date_firstlaunch", date_firstLaunch);
- }
- // Wait at least n days before opening
- if (launch_count >= LAUNCHES_UNTIL_PROMPT)
- {
- if (System.currentTimeMillis() >= date_firstLaunch + (DAYS_UNTIL_PROMPT * 24 * 60 * 60 * 1000))
- {
- try
- {
- showRateDialog(_context, editor);
- } catch (Exception _ex)
- {
- // Possible Market not installed on phone.
- _ex.printStackTrace();
- }
- }
- }
- editor.commit();
- }
- private static void showRateDialog(final Context _context, final SharedPreferences.Editor _editor) throws Exception
- {
- String title = _context.getString(R.string.rateTitle);
- String msg = _context.getString(R.string.rateMsg);
- String rateButtonText = _context.getString(R.string.rateOKButtonText);
- final Dialog dialog = new Dialog(_context);
- dialog.setTitle(title + " " + APP_TITLE);
- LinearLayout ll = new LinearLayout(_context);
- ll.setOrientation(LinearLayout.VERTICAL);
- TextView tv = new TextView(_context);
- tv.setText(msg);
- tv.setWidth(240);
- tv.setPadding(4, 0, 4, 10);
- ll.addView(tv);
- Button b1 = new Button(_context);
- b1.setText(rateButtonText);
- b1.setOnClickListener(new OnClickListener()
- {
- public void onClick(View v)
- {
- _editor.putBoolean("dontshowagain", true);
- _editor.commit();
- _context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + APP_PNAME)));
- dialog.dismiss();
- }
- });
- ll.addView(b1);
- // Button b2 = new Button(_context);
- // b2.setText("Remind me later");
- // b2.setOnClickListener(new OnClickListener()
- // {
- // public void onClick(View v)
- // {
- // dialog.dismiss();
- // }
- // });
- // ll.addView(b2);
- //
- // Button b3 = new Button(_context);
- // b3.setText("No, thanks");
- // b3.setOnClickListener(new OnClickListener()
- // {
- // public void onClick(View v)
- // {
- // if (_editor != null)
- // {
- // _editor.putBoolean("dontshowagain", true);
- // _editor.commit();
- // }
- // dialog.dismiss();
- // }
- // });
- // ll.addView(b3);
- dialog.setContentView(ll);
- dialog.show();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement