Using Intent to open other Activity
package app.test;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.Toast;
class MyBrowserActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.row);
Uri url = getIntent().getData();
WebView webView = (WebView) findViewById(R.id.WebView01);
webView.setWebViewClient(new Callback());
webView.loadUrl(url.toString());
}
private class Callback extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return (false);
}
}
}
public class Test extends Activity {
Button b1, b2, b3, b4, b5;
int request_Code = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b1 = (Button) findViewById(R.id.btn_webbrowser);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent i = new Intent("android.intent.action.VIEW");
i.setData(Uri.parse("http://www.amazon.com"));
startActivity(i);
}
});
b2 = (Button) findViewById(R.id.btn_makecalls);
b2.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent i = new Intent(android.content.Intent.ACTION_CALL, Uri.parse("tel:+651234567"));
startActivity(i);
}
});
b3 = (Button) findViewById(R.id.btn_showMap);
b3.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent i = new Intent(android.content.Intent.ACTION_VIEW, Uri
.parse("geo:37.827500,-122.481670"));
startActivity(i);
}
});
b4 = (Button) findViewById(R.id.btn_chooseContact);
b4.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent i = new Intent(android.content.Intent.ACTION_PICK);
i.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(i, request_Code);
}
});
b5 = (Button) findViewById(R.id.btn_launchMyBrowser);
b5.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent i = new Intent("app.test.MyBrowser", Uri
.parse("http://www.amazon.com"));
i.addCategory("app.test.OtherApps");
i.addCategory("app.test.SomeOtherApps");
startActivity(i);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == request_Code) {
if (resultCode == RESULT_OK) {
Toast.makeText(this, data.getData().toString(),
Toast.LENGTH_SHORT).show();
Intent i = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse(data.getData().toString()));
startActivity(i);
}
}
}
}
//main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/btn_webbrowser"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Web Browser" />
<Button
android:id="@+id/btn_makecalls"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Make Calls" />
<Button
android:id="@+id/btn_showMap"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Show Map" />
<Button
android:id="@+id/btn_chooseContact"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Choose Contact" />
<Button
android:id="@+id/btn_launchMyBrowser"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Launch My Browser" />
</LinearLayout>
//row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<WebView
android:id="@+id/WebView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Related examples in the same category