android - Intent to open an URL in a Browser

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"     android:background="#fdedfd"     >     <Button         android:id="@+id/btn"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_centerInParent="true"         android:text="Go To Yahoo.com"         android:textStyle="bold"         android:textColor="#3e87ec"         /> </RelativeLayout> 
MainActivity.java
 package com.cfsuman.me.androidcodesnippets;  import android.app.Activity; 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.Button; import android.widget.RelativeLayout;   public class MainActivity extends AppCompatActivity {      private Context mContext;     private Activity mActivity;      private RelativeLayout mRelativeLayout;     private Button mButton;       @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          // Get the application context         mContext = getApplicationContext();          // Get the activity         mActivity = MainActivity.this;          // Get the widgets reference from XML layout         mRelativeLayout = (RelativeLayout) findViewById(R.id.rl);         mButton = (Button) findViewById(R.id.btn);          // Set a click listener for the button         mButton.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View view) {                 // Specify the url to browse                 String url = "https://www.yahoo.com";                  /*                     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.                          An Intent provides a facility for performing late runtime binding between                         the code in different applications. Its most significant use is in the                         launching of activities, where it can be thought of as the glue between                         activities. It is basically a passive data structure holding an abstract                         description of an action to be performed.                 */                 /*                     public static selesai 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"                 */                 // Initialize a new Intent                 Intent intent = new Intent(Intent.ACTION_VIEW);                  /*                     public Intent setData (Uri data)                         Set the data this intent is operating on. This method automatically clears                         any type that was previously set by setType(String) or setTypeAndNormalize(String).                          Note: scheme matching in the Android framework is case-sensitive, unlike the                         formal RFC. As a result, you should always write your Uri with a lower case                         scheme, or use normalizeScheme() or setDataAndNormalize(Uri) to ensure                         that the scheme is converted to lower case.                      Parameters                         data : Uri: The Uri of the data this intent is now targeting.                      Returns                         Intent : Returns the same Intent object, for chaining multiple calls into a                                  single statement.                 */                 /*                     public static Uri parse (String uriString)                         Creates a Uri which parses the given encoded URI string.                      Parameters                         uriString : String: an RFC 2396-compliant, encoded URI                      Returns                         Uri : Uri for this given uri string                      Throws                         NullPointerException : if uriString is null                 */                 // Set the data for the Intent                 intent.setData(Uri.parse(url));                  // Start the activity                 startActivity(intent);             }         });   } } 

Komentar