Thursday, 4 November 2010

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.

Try the exercise with:
geo:37.422, -122.084
http://www.google.com

main.xml
<?xml version="1.0" encoding="utf-8"?&t;
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
&t;
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/&t;
<EditText
android:id="@+id/inputuri"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/&t;
<Button
android:id="@+id/startintent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="startActivity with ACTION_VIEW"
/&t;
</LinearLayout&t;


Android_ACTION_VIEW.java
package com.Android_ACTION_VIEW;

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);

}});

}
}


Download the files.

No comments:

Post a Comment