Intent的几个基本事件

来源:互联网 发布:java根据模板生成pdf 编辑:程序博客网 时间:2024/05/21 19:29

在xml添加几个按钮

<LinearLayout 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:orientation="vertical"   >   <Button android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:id="@+id/button1"       android:text="打电话给小呆呆"/>    <Button android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:id="@+id/button2"       android:text="给小呆发信息"/>    <Button android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:id="@+id/button3"       android:text="不理小呆,去百度"/>    <Button android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:id="@+id/button4"       android:text="不理小呆,看地图"/>    <Button android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:id="@+id/button5"       android:text="想想要不要给小呆打电话"/>     <Button android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:id="@+id/button6"       android:text="听歌曲"/></LinearLayout>

在main中的代码

package com.example.com;import java.util.ArrayList;import android.app.Activity;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.telephony.SmsManager;import android.view.View;import android.widget.Button;import android.widget.Toast;public class MainActivity extends Activity {    Button  button1,button2,button3,button4,button5,button6;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        button1=(Button) findViewById(R.id.button1);         button2=(Button) findViewById(R.id.button2);        button3=(Button) findViewById(R.id.button3);         button4=(Button) findViewById(R.id.button4);        button5=(Button) findViewById(R.id.button5);         button6=(Button) findViewById(R.id.button6);        button1.setOnClickListener(listener);        button2.setOnClickListener(listener);        button3.setOnClickListener(listener);        button4.setOnClickListener(listener);        button5.setOnClickListener(listener);        button6.setOnClickListener(listener);    }    private View.OnClickListener  listener =new View.OnClickListener(){        @Override        public void onClick(View v) {            // TODO Auto-generated method stub            Button button =(Button)v;            switch(button.getId()){            case R.id.button1:                Intent intent=new Intent();                intent.setAction(Intent.ACTION_CALL);                intent.setData(Uri.parse("tel:15734449289"));                startActivity(intent);                break;            case R.id.button2:                Intent intent2=new Intent();                intent2.setAction(Intent.ACTION_SENDTO);                intent2.setData(Uri.parse("smsto:15734449289"));                intent2.putExtra("sms_body", "你好,加油");                startActivity(intent2);break;               case R.id.button3:                Uri uri=Uri.parse("http://www.baidu.com");                Intent intent3=new Intent(Intent.ACTION_VIEW,uri);                startActivity(intent3);break;            case R.id.button4:                Uri uri2=Uri.parse("geo:38.899533,-77.036476");                Intent intent4=new Intent(Intent.ACTION_VIEW,uri2);                startActivity(intent4);break;            case R.id.button5:                Toast.makeText(MainActivity.this, "666", Toast.LENGTH_LONG).show();                Uri uri3=Uri.parse("tel:15734449289");                Intent intent5=new Intent(Intent.ACTION_CALL,uri3);                startActivity(intent5);break;            /*case R.id.button6:                  Uri uri4 = Uri.parse("file:///sdcard/song.mp3");                     Intent it = new Intent(Intent.ACTION_VIEW, uri4);                     it.setType("audio/mp3");                     startActivity(it);  break;*/            }        }    };    }

前5个功能都能去实现。第六个在想…..

 <uses-permission android:name="android.permission.CALL_PHONE"/>//打电话加的功能    <uses-permission android:name="android.permission.SEND_SMS"/>    //发短信加的功能

Intent学习博客推荐
http://www.cnblogs.com/engine1984/p/4146621.html