android - How to write rate this app code

activity_main.xml
 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:id="@+id/rl"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:padding="16dp"     tools:context=".MainActivity"     >     <ImageView         android:id="@+id/iv"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:src="@drawable/animals"         android:layout_centerInParent="true"         />     <TextView         android:id="@+id/tv_rate"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="Rate This App!"         android:layout_alignParentTop="true"         android:layout_alignParentRight="true"         android:textSize="10dp"         />     <ImageButton         android:id="@+id/ib"         android:layout_width="85dp"         android:layout_height="24dp"         android:scaleType="fitCenter"         android:foregroundGravity="right"         android:src="@drawable/stars"         android:layout_alignParentRight="true"         android:background="@android:color/transparent"         android:layout_below="@+id/tv_rate"         /> </RelativeLayout> 
MainActivity.java
 package com.cfsuman.me.androidcode;  import android.content.ActivityNotFoundException; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ImageButton; import android.widget.RelativeLayout;   public class MainActivity extends AppCompatActivity {     private Context mContext;     private String mMarketUri = "market://details?id=";     private String mPlayStoreRootUrl = "http://play.google.com/store/apps/details?id=";      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          // Get the application context         mContext = getApplicationContext();          // Get the widgets reference from XML layout         RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);         ImageButton ib = (ImageButton) findViewById(R.id.ib);          // Set an click listener for ImageButton         ib.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View view) {                 // Call the custom method to go google play store app page                 rateThisApp();             }         });     }      // Custom method to request app rating     public void rateThisApp(){         /*             public abstract String getPackageName ()                 Return the name of this application's package.         */         Uri uri = Uri.parse(mMarketUri + mContext.getPackageName());          /*             Intent                 An intent is an abstract description of an operation to be performed. It can be used                 with startActivity to launch an Activity, broadcastIntent to send it to any interested                 BroadcastReceiver components, and startService(Intent) or                 bindService(Intent, ServiceConnection, int) to communicate with a background Service.         */         /*             public static simpulan String ACTION_VIEW                 Activity Action: Display the data to the user. This is the most common action performed                 on data -- it is the generic action you can use on a piece of data to get the most                 reasonable thing to occur. For example, when used on a contacts entry it will view                 the entry; when used on a mailto: URI it will bring up a compose window filled with                 the information supplied by the URI; when used with a tel: URI it will invoke the dialer.                  Input: getData() is URI from which to retrieve data.                 Output: nothing.                 Constant Value: "android.intent.action.VIEW"         */         Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);          /*             public Intent addFlags (int flags)                 Add additional flags to the intent (or with existing flags value).                  Parameters                     flags : The new flags to set.                 Returns                     Returns the same Intent object, for chaining multiple calls into a single statement.         */          /*             public static simpulan int FLAG_ACTIVITY_NO_HISTORY                 If set, the new activity is not kept in the history stack. As soon as the user                 navigates away from it, the activity is finished. This may also be set with the                 noHistory attribute.                  If set, onActivityResult() is never invoked when the current activity starts a new                 activity which sets a result and finishes.              public static simpulan int FLAG_ACTIVITY_NEW_DOCUMENT                 This flag is used to open a document into a new task rooted at the activity launched                 by this Intent. Through the use of this flag, or its equivalent attribute,                 documentLaunchMode multiple instances of the same activity containing different                 documents will appear in the recent tasks list.              public static simpulan int FLAG_ACTIVITY_MULTIPLE_TASK                 This flag is used to create a new task and launch an activity into it. This flag is                 always paired with either FLAG_ACTIVITY_NEW_DOCUMENT or FLAG_ACTIVITY_NEW_TASK. In                 both cases these flags alone would search through existing tasks for ones matching                 this Intent. Only if no such task is found would a new task be created.         */         goToMarket.addFlags(                 Intent.FLAG_ACTIVITY_NO_HISTORY |                         // For old API version                         //Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET |                         Intent.FLAG_ACTIVITY_NEW_DOCUMENT |                         Intent.FLAG_ACTIVITY_MULTIPLE_TASK         );         try {             /*                 public abstract void startActivity (Intent intent)                     Same as startActivity(Intent, Bundle) with no options specified.                  public abstract void startActivity (Intent intent, Bundle options)                     Launch a new activity. You will not receive any information about when the                     activity exits.                      Note that if this method is being called from outside of an Activity Context, then                     the Intent must include the FLAG_ACTIVITY_NEW_TASK launch flag. This is because,                     without being started from an existing Activity, there is no existing task in which                     to place the new activity and thus it needs to be placed in its own separate task.                      This method throws ActivityNotFoundException if there was no Activity found to                     run the given Intent.             */             startActivity(goToMarket);         }         /*             ActivityNotFoundException                 This exception is thrown when a call to startActivity(Intent) or one of its variants                 fails because an Activity can not be found to execute the given Intent.         */         catch (ActivityNotFoundException e)         {             startActivity(new Intent(Intent.ACTION_VIEW,                     // Use another app package name for testing purpose only                     //Uri.parse(mPlayStoreRootUrl + "com.cfsuman.me.kidsabc")));                      /*                         Uri                             Immutable URI reference. A URI reference includes a URI and a fragment,                             the component of the URI following a '#'. Builds and parses URI references                             which conform to RFC 2396.                          public static Uri parse (String uriString)                             Creates a Uri which parses the given encoded URI string.                              Parameters                                 uriString : an RFC 2396-compliant, encoded URI                             Returns                                 Uri : for this given uri string                             Throws                                 NullPointerException : if uriString is null                     */                     Uri.parse(mPlayStoreRootUrl + mContext.getPackageName())));         }     } } 
More android examples

Komentar