android手機短信發(fā)送-創(chuàng)新互聯(lián)

剛學(xué)android不久,最近衛(wèi)衛(wèi)發(fā)給我了一個視頻,是怎樣制作android手機上的×××,一方面我感覺很有意思,另一方可以陪著她一起,今天弄完了,特來總結(jié)一下,雖然說比較簡單吧,但還是有總結(jié)的必要的。

站在用戶的角度思考問題,與客戶深入溝通,找到濟(jì)水街道網(wǎng)站設(shè)計與濟(jì)水街道網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:成都做網(wǎng)站、網(wǎng)站設(shè)計、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、申請域名、虛擬空間、企業(yè)郵箱。業(yè)務(wù)覆蓋濟(jì)水街道地區(qū)。

×××主要是用于android下的短信發(fā)送的,其主要的界面就是輸入發(fā)送母的人的號碼和要發(fā)的信息的內(nèi)容,主要界面有兩個TextView和兩個EditText還有一個Button(在import android.widget.*),下面是布局文件(layout):

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

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

  android:layout_width="fill_parent"

  android:layout_height="fill_parent"

 android:orientation="vertical" >

  <TextView

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="@string/number"

    />

  <EditText

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:id="@+id/number"

    />

  <TextView

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="@string/content"

    />

  <EditText

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:minLines="3"

    android:id="@+id/context"

    />

  <Button

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="@string/button"

    android:id="@+id/button"

   />

</LinearLayout>

以上布局文件使用的string的值需要在value/string.xml進(jìn)行設(shè)置,主要的代碼如下:

<?xml version="1.0" encoding="utf-8"?>

<resources>

  <string name="app_name">smsTest</string>

  <string name="action_settings">Settings</string>

  <string name="content">請輸入短信內(nèi)容</string>

  <string name="number">請輸入手機號</string>

  <string name="button">發(fā)送信息</string>

 <string name="success">發(fā)送成功</string>

</resources>

以上就是布局,可以到AVD中試試是否成功,但是我們要完成這個×××還需要在主程序.java寫上相應(yīng)的處理。

package com.example.smstest;

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

import android.telephony.gsm.SmsManager;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast ;

import java.util.ArrayList;//以上是需要的包

@SuppressWarnings( "deprecation" )

public class MainActivity extends Activity {

 private EditText numberText ;//這個變量時用來接受布局中輸入的號碼

 private EditText contentText ;//這個變量時用來接受布局中輸入的信息

 @Override

 protected void onCreate(Bundle savedInstanceState) {//此類程序會從這歌開始在執(zhí)行

 super.onCreate(savedInstanceState);//調(diào)用父類Activity中的onCreate方法

 setContentView(R.layout.activity_main);//執(zhí)行布局文件

 numberText = (EditText) this.findViewById( R.id.number ) ; //接受布局中輸入框中的號碼

 contentText = (EditText) this.findViewById(R.id.context) ;//接受布局中輸入框中的信息

 Button button = (Button) this.findViewById(R.id.button) ;//使用Button

 button.setOnClickListener( new ButtonClick() ) ;//設(shè)置Button響應(yīng)的內(nèi)容

 }

 private final class ButtonClick implements View.OnClickListener

 {

 @Override

 public void onClick( View arg0 ) {

  String number = numberText.getText().toString() ;//接受布局中輸入框中的號碼

  String content = contentText.getText().toString();

  SmsManager manger = SmsManager.getDefault();//建立短信管理對象

  ArrayList<String> texts=manger.divideMessage(content) ;//如果短信過長超過一條短信的大值那么分為多條短信發(fā)送

  for ( String text : texts ) {

  manger.sendTextMessage(number, null, text, null, null) ;//發(fā)送消息(第一個是目的號碼,第三個是消息內(nèi)容)

  }

  Toast.makeText(getApplicationContext(), R.string.success, Toast.LENGTH_LONG ).show() ;

  //使用Toast提示短信發(fā)送成功

 }

 }

 @Override

 public boolean onCreateOptionsMenu(Menu menu) {

 // Inflate the menu; this adds items to the action bar if it is present.

 getMenuInflater().inflate(R.menu.main, menu);

 return true;

 }

}

這樣就可以了,可以在兩個模擬器中發(fā)送(漢字會亂碼但手機不會)android手機短信發(fā)送

但是這樣仍然不能再手機中使用,因為手機中的使用的話是要有使用權(quán)限的比如允許其扣除話費,那么獲得權(quán)限就需要修改/smsTest/AndroidManifest.xml中加一個權(quán)限的代碼

<?xml version="1.0" encoding="utf-8"?>

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

  package="com.example.smstest"

  android:versionCode="1"

  android:versionName="1.0" >

  <uses-sdk

    android:minSdkVersion="6"

    android:targetSdkVersion="10" />

  <application

    android:allowBackup="true"

    android:icon="@drawable/ic_launcher"

    android:label="@string/app_name"

    android:theme="@style/AppTheme" >

    <activity

      android:name="com.example.smstest.MainActivity"

      android:label="@string/app_name" >

      <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.SEND_SMS"/>

</manifest>

紅體字是加上去的,其余是自動生成的,到此×××OK了。

2014/04/03 14:44

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

新聞名稱:android手機短信發(fā)送-創(chuàng)新互聯(lián)
網(wǎng)站地址:http://www.muchs.cn/article28/dpjpcp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈靜態(tài)網(wǎng)站網(wǎng)站制作、網(wǎng)站維護(hù)關(guān)鍵詞優(yōu)化、云服務(wù)器

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

手機網(wǎng)站建設(shè)