1.duanxinMoachine項目
com.credream.duanxin下
DuanxinMoachineActivity.java
package com.credream.duanxin;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.SimpleAdapter.ViewBinder;
public class DuanxinMoachineActivity extends Activity {
private EditText numberText;
private EditText conteText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
numberText=(EditText) this.findViewById(R.id.number);
conteText=(EditText)this.findViewById(R.id.content);
Button button=(Button)this.findViewById(R.id.button);
button.setOnClickListener(new ButtononClickListener());
}
private final class ButtononClickListener implements View.OnClickListener{
@Override
public void onClick(View v)
{
String number=numberText.getText().toString();
String contenxtString=conteText.getText().toString();
SmsManager manager=SmsManager.getDefault();//發送短信的
類.用getDefault(),取得系統的默認短信系統
ArrayList<String> texts=manager.divideMessage
(contenxtString);//超過70字就會分條發送
for(String text:texts){
manager.sendTextMessage(number, null, text, null,
null);//number:發送號碼,短信中心地址,短信內容,
//發送的成功與否狀態,對方是否收到狀態這兩個參數需要移動
網絡返回電信號來判斷;移動會把狀態放到廣播裡,以後
//會學習
}
Toast.makeText(DuanxinMoachineActivity.this, R.string.success,
Toast.LENGTH_LONG).show() ;//應用的包名等上下文信息,
//getApplicationContext(),應用上下文,或者把
DuanxinMoachineActivity
//放到裡面,第二個參數:顯示的內容,第三個指的是顯示的實際長短;
//發送短信的權限
}
}
}
-------------------------------------------------------------
main.xml
<?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/app_name" />
<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/content"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button"
android:id="@+id/button"
/>
</LinearLayout>
------------------------------
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, DuanxinMoachineActivity!</string>
<string name="app_name">短信發送器</string>
<string name="number">請輸入手機號</string>
<string name="content">請輸入短信內容</string>
<string name="button">發送</string>
<string name="success">發送完成</string>
</resources>
--------------------------------------------------
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.credream.duanxin"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".DuanxinMoachineActivity" >
<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>
--------------------------------------------
2.三種通知:
狀態欄通知;
對話框通知:
Toast(吐西通知);
<uses-permission android:name="android.permission.SEND_SMS"/>
//發送短信需要的權限