第一行代码系列第二章——更多隐式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);
			}
		});
	}

效果图

猜你喜欢

转载自blog.csdn.net/asdaosidasu/article/details/52503655