1. 程式人生 > >第一行程式碼系列第二章——更多隱式Intent用法(開啟網頁)

第一行程式碼系列第二章——更多隱式Intent用法(開啟網頁)

效果圖

修改FirstActivity中按鈕事件

Button button1 = (Button) findViewById(R.id.button_1);
		button1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//Toast .makeText(FirstActivity .this, "you click the button 1", Toast.LENGTH_SHORT).show();
			    Intent intent = new Intent(Intent.ACTION_VIEW);
			    intent.setData(Uri.parse("http://baidu.com"));
			    startActivity(intent);
			}
		});

這樣直接修改就可以

也可以自己建立一個活動,讓他也能響應網頁的Intent

新建third_layout.xml佈局檔案

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <Button
        android:id="@+id/button_3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button 3"
        />
    

</LinearLayout>

新建活動ThirdActivity繼承Activity
package com.example.activitytest;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;

public class ThirdActivity extends Activity{
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.third_layout);
	}

}

在選單檔案AndroidManifest.xml中註冊一下
<activity  android:name=".ThirdActivity">
            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <data android:scheme="http"/>
            </intent-filter>
            
        </activity>

執行效果圖

除了http,還有geo表示顯示地理位置,tel表示撥打電話

下面是呼叫系統撥號介面

		Button button1 = (Button) findViewById(R.id.button_1);
		button1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//Toast .makeText(FirstActivity .this, "you click the button 1", Toast.LENGTH_SHORT).show();
//			    Intent intent = new Intent(Intent.ACTION_VIEW);
//			    intent.setData(Uri.parse("http://baidu.com"));
				Intent intent = new Intent(Intent.ACTION_DIAL);
				intent.setData(Uri.parse("tel:10086"));
			    startActivity(intent);
			}
		});
	}

效果圖