首页 > 代码库 > 隐式Intent的应用

隐式Intent的应用

package com.example.yinshiintent;

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

public class MainActivity extends Activity implements OnClickListener{

	Button call = null;
	Button sms = null;
	Button web = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		call = (Button) findViewById(R.id.call);
		sms = (Button) findViewById(R.id.SMS);
		web = (Button) findViewById(R.id.web);
		call.setOnClickListener(this);
		sms.setOnClickListener(this);
		web.setOnClickListener(this);
	}

	@Override
	public void onClick(View view) {
        Intent intent = new Intent();
        Uri uri = null;
		switch(view.getId()){
		case R.id.call:
			uri = Uri.parse("tel:18948393993");
			intent.setData(uri);
			intent.setAction(Intent.ACTION_DIAL);
			startActivity(intent);
			break;
		case R.id.SMS: 
			uri = Uri.parse("smsto:625016");
			intent.setData(uri);
			intent.setAction(Intent.ACTION_SENDTO);
			intent.putExtra("sms_body", "这是一条病毒测试短信,请勿回复!");
			startActivity(intent);
			break;
		case R.id.web:
			uri = Uri.parse("http://www.hstc.edu.cn");
			intent.setData(uri);
			intent.setAction(Intent.ACTION_VIEW);
			startActivity(intent);
			break;
		}
	}
}

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.yinshiintent.MainActivity" >

    <Button 
        android:id="@+id/call"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="拨号打电话"
        />
    <Button 
        android:id="@+id/SMS"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/call"
        android:text="发短信"
        />
    <Button 
        android:id="@+id/web"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/SMS"
        android:text="打开韩师主页"
        />

</RelativeLayout>

技术分享技术分享技术分享技术分享

要想能真正打电话、发短信就要装到手机里。只要电脑联网,在模拟器上面也能访问网页

源代码下载

隐式Intent的应用