新聞中心
剛學(xué)android不久,最近衛(wèi)衛(wèi)發(fā)給我了一個視頻,是怎樣制作android手機上的×××,一方面我感覺很有意思,另一方可以陪著她一起,今天弄完了,特來總結(jié)一下,雖然說比較簡單吧,但還是有總結(jié)的必要的。
×××主要是用于android下的短信發(fā)送的,其主要的界面就是輸入發(fā)送母的人的號碼和要發(fā)的信息的內(nèi)容,主要界面有兩個TextView和兩個EditText還有一個Button(在import android.widget.*),下面是布局文件(layout):
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/number" /> android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/number" /> android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/content" /> android:layout_width="fill_parent" android:layout_height="wrap_content" android:minLines="3" android:id="@+id/context" />
以上布局文件使用的string的值需要在value/string.xml進行設(shè)置,主要的代碼如下:
以上就是布局,可以到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
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ā)送(漢字會亂碼但手機不會)
但是這樣仍然不能再手機中使用,因為手機中的使用的話是要有使用權(quán)限的比如允許其扣除話費,那么獲得權(quán)限就需要修改/smsTest/AndroidManifest.xml中加一個權(quán)限的代碼
package="com.example.smstest" android:versionCode="1" android:versionName="1.0" > android:minSdkVersion="6" android:targetSdkVersion="10" /> android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > android:name="com.example.smstest.MainActivity" android:label="@string/app_name" >
紅體字是加上去的,其余是自動生成的,到此×××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)
本文來源:http://fisionsoft.com.cn/article/dpjpcp.html