Android学习第十四天----发送短信的两种方式

方式一:

xml中

<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"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView1"
        android:layout_marginBottom="90dp"
        android:layout_toLeftOf="@+id/textView1"
        android:text="Button" />

</RelativeLayout>

java代码:

package com.example.androidi18n;

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
{
    private Button mButton ;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mButton = (Button)findViewById(R.id.button1);
        
        mButton.setOnClickListener(new OnClickListener()
        {
            
            @Override
            public void onClick(View v)
            {
                
                Intent intent = new Intent(Intent.ACTION_SENDTO,Uri.parse("smsto:5556"));
                MainActivity.this.startActivity(intent);
                
            }
        });
    }
}

另外在权限那边记得加上这句话

 <uses-permission android:name="android.permission.SEND_SMS"/>

加上权限就可以实现发送短信的功能

 

posted @ 2013-03-21 23:04  小三小山  阅读(137)  评论(0编辑  收藏  举报