1. 程式人生 > >Intent傳送簡訊(跳轉到傳送介面)

Intent傳送簡訊(跳轉到傳送介面)

 

 

在main.xml中:

<LinearLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical"

    android:gravity="center_horizontal"

    android:background="#000000">

    <TableLayout

        android:orientation="vertical"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:layout_margin="8dp">

        <TableRow >

            <TextView

                android:layout_width="80dp"

                android:layout_height="wrap_content"

                android:textColor="#ffffff"

                android:text="收信人"/>

            <EditText

                android:id="@+id/tel"

                android:numeric="integer"

                android:width="260dp"

                android:layout_height="wrap_content"/>

        </TableRow>

        <View

            android:layout_height="1dp"

            android:background="#ff9090"/>

        <TableRow

            android:layout_marginTop="8dp">

            <TextView

                android:layout_width="80dp"

                android:layout_height="wrap_content"

                android:textColor="#ffffff"

                android:layout_marginTop="20dp"

                android:text="內容"/>

            <EditText

                android:id="@+id/content"

                android:numeric="integer"

                android:lines="6"

                android:width="260dp"

                android:gravity="top"

                android:layout_height="wrap_content"/>

        </TableRow>

        <View

            android:layout_height="1dp"

            android:background="#ff9090"/>

  </TableLayout>

     <Button

         android:id="@+id/mybut"

         android:layout_width="80dp"

         android:layout_height="40dp"

         android:layout_marginTop="18dp"

         android:background="#3399ff"

         android:textColor="#ffffff"

         android:text="傳送簡訊"/>

</LinearLayout>

在MyIntentCaseDemo.xml中:

package com.li.intentcaseproject;

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;

import android.widget.EditText;

public class MyIntentCaseDemo extends Activity {

  private Button mybut = null ;

  private EditText tel = null ;

  private EditText content = null ;

  @Override

  public void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);

     super.setContentView(R.layout.main);

     this.mybut = (Button) super.findViewById(R.id.mybut) ;  // 取得元件

     this.tel = (EditText) super.findViewById(R.id.tel) ;  // 取得元件

     this.content = (EditText) super.findViewById(R.id.content) ;  // 取得元件

     this.mybut.setOnClickListener(new OnClickListenerImpl()) ;

  }

  private class OnClickListenerImpl implements OnClickListener{

     public void onClick(View v) {

       String telStr = MyIntentCaseDemo.this.tel.getText().toString() ; // 取得輸入資訊

       String note = MyIntentCaseDemo.this.content.getText().toString() ;  // 取得內容

       Uri uri = Uri.parse("smsto:" + telStr) ;  // 設定操作的路徑

       Intent it = new Intent() ;

       it.setAction(Intent.ACTION_SENDTO) ; // 設定要操作的Action

       it.putExtra("sms_body",note) ; // 設定簡訊內容

       it.setType("vnd.android-dir/mms-sms") ; // 簡訊的MIME型別

       it.setData(uri) ;  // 要設定的資料

       MyIntentCaseDemo.this.startActivity(it) ; // 執行跳轉

     }

  }

}

修改AndroidManifest.xml檔案:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.li.intentcaseproject"

    android:versionCode="1"

    android:versionName="1.0" >

    <uses-sdk

        android:minSdkVersion="8"

        android:targetSdkVersion="15" />

    <application

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

            android:name=".MyIntentCaseDemo"

            android:label="@string/title_activity_my_itent_case_demo" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>

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

</manifest>