第二个Android小程序(Android短信发送),继续贴代码:
首先是布局的代码:main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:text="@string/inputMobile" android:layout_width="fill_parent" android:layout_height="wrap_content"> </TextView> <EditText android:id="@+id/mobile" android:layout_width="fill_parent" android:layout_height="wrap_content"> </EditText> <TextView android:text="@string/inputMessage" android:layout_width="fill_parent" android:layout_height="wrap_content"> </TextView> <EditText android:id="@+id/Message" android:layout_width="fill_parent" android:layout_height="wrap_content"> </EditText> <Button android:text="@string/Submit" android:id="@+id/Submit" android:layout_width="wrap_content" android:layout_height="wrap_content"> </Button> </LinearLayout>
接下来是String.xml的代码:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">短信发送器</string> <string name="inputMobile">请输入手机号:</string> <string name="inputMessage">请输入短信内容:</string> <string name="Submit">发送短信</string> </resources>
上面两个都是位于res里面的。
接下来是src里面的代码MessageActivity:(由于在公司,没空写注释,等等回去补上)
package cn.jason.android; import java.util.List; import android.app.Activity; import android.app.PendingIntent; import android.content.Intent; 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; public class messageActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final EditText mobileNoEditText = (EditText) findViewById(R.id.mobile); final EditText messageEditText = (EditText) findViewById(R.id.Message); Button submitButton = (Button) findViewById(R.id.Submit); submitButton.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { String moblileString = mobileNoEditText.getText().toString(); String messageString = messageEditText.getText().toString(); SmsManager smsManager = SmsManager.getDefault(); PendingIntent sendingIntent = PendingIntent.getBroadcast(messageActivity.this, 0, new Intent(), 0); List<String> msgsList = smsManager.divideMessage(messageString); for (String msg : msgsList) { smsManager.sendTextMessage(moblileString, null, msg, sendingIntent, null); } Toast.makeText(messageActivity.this, "短信发送成功", Toast.LENGTH_LONG).show(); } }); } }
以上你都写了还是不能用的,最关键的一句是这样的:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.jason.android" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".messageActivity" 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-sdk android:minSdkVersion="4" /> <uses-permission android:name="android.permission.SEND_SMS" /> </manifest>
上面的最后一句如果没有的话是没法发短信的。这个是权限的设置。短信发送权限的设置。<uses-permission android:name="android.permission.SEND_SMS" />
转载自:http://www.linuxidc.com/Linux/2011-08/40647.htm
Android开发之路——走进Android(工程结构剖析)
http://wisdomdd.cn/Wisdom/resource/articleDetail.htm?resourceId=556
Android开发之路——第一个Android小程序(Android电话拨号器)
http://wisdomdd.cn/Wisdom/resource/articleDetail.htm?resourceId=557
Android开发之路——第三个Android小程序(Android的Activity显示
http://wisdomdd.cn/Wisdom/resource/articleDetail.htm?resourceId=559
Android开发之路——Android的布局初步
http://wisdomdd.cn/Wisdom/resource/articleDetail.htm?resourceId=560
Android开发之路——Android的布局初步2——TableLayout布局
http://wisdomdd.cn/Wisdom/resource/articleDetail.htm?resourceId=561
Android开发之路——单选框,复选框,弹出框等控件操作
http://wisdomdd.cn/Wisdom/resource/articleDetail.htm?resourceId=562
Android开发学习笔记补充记录——Activity的生命周期 h
http://wisdomdd.cn/Wisdom/resource/articleDetail.htm?resourceId=563