Intent的几个基本事件

在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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值