Android的第三個應用—簡訊發送器

來源:互聯網
上載者:User

一、

二、

因為應用要使用手機的簡訊服務,所以要在資訊清單檔AndroidManifest.xml中添加簡訊服務許可權:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="cn.itcast.sms"
      android:versionCode="1"
      android:versionName="1.0">
     略....
     <uses-sdk android:minSdkVersion=“4" />
    <uses-permission android:name="android.permission.SEND_SMS"/>
</manifest>

三、布局介面

<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"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".SmsActivity" >    <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/phone_num" />    <EditText        android:id="@+id/phonenum"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/textView1"        android:layout_below="@+id/textView1"        android:layout_marginTop="15dp"        android:ems="10"        android:inputType="phone" >        <requestFocus />    </EditText>        <TextView         android:id="@+id/sms_content"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@+id/phonenum"        android:layout_marginTop="15dp"        android:text="@string/sms_content"                />    <EditText        android:id="@+id/smsContent"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/sms_content"        android:layout_below="@+id/sms_content"        android:layout_marginTop="26dp"        android:ems="10"        android:inputType="textMultiLine" />    <Button        android:id="@+id/sendButton"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/smsContent"        android:layout_below="@+id/smsContent"        android:layout_marginTop="58dp"        android:text="@string/send_button" /></RelativeLayout>

 

四、布局介面中引用的string屬性

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="app_name">lession01-Sms</string>    <string name="action_settings">Settings</string>    <string name="hello_world">Hello world!</string><string name="phone_num">請輸入電話號碼</string><string name="sms_content">請輸入簡訊內容</string><string name="send_button">發送資訊</string></resources>

 

五、SmsActivity代碼:

package com.example.lession01_sms;import android.net.Uri;import android.os.Bundle;import android.app.Activity;import android.app.PendingIntent;import android.content.Intent;import android.telephony.SmsManager;import android.view.Menu;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class SmsActivity extends Activity {//聲明組件public Button sendButton;public EditText smsContent;public EditText phonenum;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        //設定顯示的視圖        setContentView(R.layout.activity_sms);                //擷取組件        sendButton=(Button) findViewById(R.id.sendButton);        smsContent=(EditText) findViewById(R.id.smsContent);        phonenum=(EditText) findViewById(R.id.phonenum);                //給按鈕註冊事件        sendButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//擷取電話號碼String phone_num=phonenum.getText().toString();//建立意圖對象Intent intent = new Intent(Intent.ACTION_SEND);intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/20.jpg"));intent.putExtra("address", phone_num);intent.putExtra("exit_on_sent", true);intent.putExtra("subject", "subject:I love you!!!!!!");intent.putExtra("sms_body", "content ::XXXX");intent.setType("image/jpeg");   startActivity(intent);}});    }            public void send(){    //擷取電話號碼String phone_num=phonenum.getText().toString();//擷取簡訊的內容String sms_content=smsContent.getText().toString();//擷取簡訊管理器對象SmsManager smsManager=SmsManager.getDefault();//意圖對象PendingIntent pendingIntent=PendingIntent.getBroadcast(SmsActivity.this, 0, new Intent(), 0);//發送資訊操作smsManager.sendTextMessage(phone_num, null, sms_content, pendingIntent, null);//Toast的效果Toast.makeText(SmsActivity.this, "簡訊發送成功", Toast.LENGTH_LONG).show();    }        @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.sms, menu);        return true;    }    }

 

六、測試步驟

1>在Eclipse中運行此應用
 2>在Dos視窗中進入android SDK安裝路徑的tools目錄,輸入以下命令再開啟一個Android模擬器:
  emulator -data csdn
   註:icsdn為使用者資料存取檔案,如果該檔案不存在,預設在tools目錄建立該檔案

3>在簡訊發送器的手機號中輸入現顯的電話號碼

註:目前Android系統對中文簡訊尚未支援,所以發送中文簡訊會有亂碼,這個問題日後會被解決的。

 

 

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.