溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

android手機(jī)短信發(fā)送

發(fā)布時(shí)間:2020-07-15 18:18:15 來(lái)源:網(wǎng)絡(luò) 閱讀:418 作者:lakeside_12138 欄目:移動(dòng)開(kāi)發(fā)

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

×××主要是用于android下的短信發(fā)送的,其主要的界面就是輸入發(fā)送母的人的號(hào)碼和要發(fā)的信息的內(nèi)容,主要界面有兩個(gè)TextView和兩個(gè)EditText還有一個(gè)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">請(qǐng)輸入短信內(nèi)容</string>

    <string name="number">請(qǐng)輸入手機(jī)號(hào)</string>

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

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

 

</resources>

以上就是布局,可以到AVD中試試是否成功,但是我們要完成這個(gè)×××還需要在主程序.java寫(xiě)上相應(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 ;//這個(gè)變量時(shí)用來(lái)接受布局中輸入的號(hào)碼

 private EditText contentText ;//這個(gè)變量時(shí)用來(lái)接受布局中輸入的信息

 @Override

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

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

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

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

  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() ;//接受布局中輸入框中的號(hào)碼

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

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

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

   for ( String text : texts ) {

   

    manger.sendTextMessage(number, null, text, null, null) ;//發(fā)送消息(第一個(gè)是目的號(hào)碼,第三個(gè)是消息內(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;

 }

}

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

但是這樣仍然不能再手機(jī)中使用,因?yàn)槭謾C(jī)中的使用的話是要有使用權(quán)限的比如允許其扣除話費(fèi),那么獲得權(quán)限就需要修改/smsTest/AndroidManifest.xml中加一個(gè)權(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>

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

2014/04/03 14:44


向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI