现在的位置: 首页 > 综合 > 正文

Intent发送短信(跳转到发送界面)

2017年12月15日 ⁄ 综合 ⁄ 共 4069字 ⁄ 字号 评论关闭

 

 

 

 

在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>

 

 

【上篇】
【下篇】

抱歉!评论已关闭.