Advertisement
Guest User

App Rating

a guest
Jan 24th, 2013
407
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.95 KB | None | 0 0
  1. public class Rating
  2. {
  3.     private final static String APP_TITLE = "AppName";
  4.     private final static String APP_PNAME = "com.yourapp.appname";
  5.    
  6.     private final static int DAYS_UNTIL_PROMPT = 3;
  7.     private final static int LAUNCHES_UNTIL_PROMPT = 5;
  8.    
  9.     public static void app_launched(Context _context)
  10.     {
  11.         SharedPreferences prefs = _context.getSharedPreferences("apprater", 0);
  12.         if (prefs.getBoolean("dontshowagain", false))
  13.         {
  14.             return;
  15.         }
  16.        
  17.         SharedPreferences.Editor editor = prefs.edit();
  18.        
  19.         // Increment launch counter
  20.         long launch_count = prefs.getLong("launch_count", 0) + 1;
  21.         editor.putLong("launch_count", launch_count);
  22.        
  23.         // Get date of first launch
  24.         Long date_firstLaunch = prefs.getLong("date_firstlaunch", 0);
  25.         if (date_firstLaunch == 0)
  26.         {
  27.             date_firstLaunch = System.currentTimeMillis();
  28.             editor.putLong("date_firstlaunch", date_firstLaunch);
  29.         }
  30.        
  31.         // Wait at least n days before opening
  32.         if (launch_count >= LAUNCHES_UNTIL_PROMPT)
  33.         {
  34.             if (System.currentTimeMillis() >= date_firstLaunch + (DAYS_UNTIL_PROMPT * 24 * 60 * 60 * 1000))
  35.             {
  36.                 try
  37.                 {
  38.                     showRateDialog(_context, editor);
  39.                 } catch (Exception _ex)
  40.                 {
  41.                     // Possible Market not installed on phone.
  42.                     _ex.printStackTrace();
  43.                 }
  44.             }
  45.         }
  46.        
  47.         editor.commit();
  48.     }
  49.    
  50.     private static void showRateDialog(final Context _context, final SharedPreferences.Editor _editor) throws Exception
  51.     {
  52.         String title = _context.getString(R.string.rateTitle);
  53.         String msg = _context.getString(R.string.rateMsg);
  54.         String rateButtonText = _context.getString(R.string.rateOKButtonText);
  55.        
  56.         final Dialog dialog = new Dialog(_context);
  57.         dialog.setTitle(title + " " + APP_TITLE);
  58.        
  59.         LinearLayout ll = new LinearLayout(_context);
  60.         ll.setOrientation(LinearLayout.VERTICAL);
  61.        
  62.         TextView tv = new TextView(_context);
  63.         tv.setText(msg);
  64.         tv.setWidth(240);
  65.         tv.setPadding(4, 0, 4, 10);
  66.         ll.addView(tv);
  67.        
  68.         Button b1 = new Button(_context);
  69.         b1.setText(rateButtonText);
  70.         b1.setOnClickListener(new OnClickListener()
  71.         {
  72.             public void onClick(View v)
  73.             {
  74.                 _editor.putBoolean("dontshowagain", true);
  75.                 _editor.commit();
  76.                 _context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + APP_PNAME)));
  77.                 dialog.dismiss();
  78.             }
  79.         });
  80.         ll.addView(b1);
  81.        
  82. //      Button b2 = new Button(_context);
  83. //      b2.setText("Remind me later");
  84. //      b2.setOnClickListener(new OnClickListener()
  85. //      {
  86. //          public void onClick(View v)
  87. //          {
  88. //              dialog.dismiss();
  89. //          }
  90. //      });
  91. //      ll.addView(b2);
  92. //     
  93. //      Button b3 = new Button(_context);
  94. //      b3.setText("No, thanks");
  95. //      b3.setOnClickListener(new OnClickListener()
  96. //      {
  97. //          public void onClick(View v)
  98. //          {
  99. //              if (_editor != null)
  100. //              {
  101. //                  _editor.putBoolean("dontshowagain", true);
  102. //                  _editor.commit();
  103. //              }
  104. //              dialog.dismiss();
  105. //          }
  106. //      });
  107. //      ll.addView(b3);
  108.        
  109.         dialog.setContentView(ll);
  110.         dialog.show();
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement