Tuesday, July 12, 2011

startActivity() with action Intent.ACTION_VIEW

When call startActivity() with action Intent.ACTION_VIEW, the system will start an activity to display the data to the user. ACTION_VIEW 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.


In the exercise, Uri.parse() is used to creates a Uri which parses the URI string entered in the EditText. The text entered in the EditText should be an RFC 2396-compliant string.


LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
;
TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/;
EditText
android:id="@+id/inputuri"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/;
Button
android:id="@+id/startintent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="startActivity with ACTION_VIEW"
/;
/LinearLayout


import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class Android_ACTION_VIEW extends Activity {
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
      
       final EditText inputUri = (EditText)findViewById(R.id.inputuri);
       Button buttonStartIntent = (Button)findViewById(R.id.startintent);
      
       buttonStartIntent.setOnClickListener(new Button.OnClickListener(){

   @Override
   public void onClick(View arg0) {
    // TODO Auto-generated method stub
    String uriString = inputUri.getText().toString();
    Uri intentUri = Uri.parse(uriString);
    
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    intent.setData(intentUri);
    
    startActivity(intent);
    
   }});

   }
}

No comments:

Post a Comment