Android之路——第二个Android小程序(Android短信发送)
2011-01-21 15:54
316 查看
闲话不说,继续贴代码:
首先是布局的代码:main.xml
接下来是String.xml的代码:
上面两个都是位于res里面的。
接下来是src里面的代码MessageActivity:(由于在公司,没空写注释,等等回去补上)
以上你都写了还是不能用的,最关键的一句是这样的:
上面的最后一句如果没有的话是没法发短信的。这个是权限的设置。短信发送权限的设置。<uses-permission android:name="android.permission.SEND_SMS" />
首先是布局的代码: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" />
相关文章推荐
- Android之路——第二个Android小程序(Android短信发送)
- Android小程序:实现发送短信
- Android外部监听类-发送短信程序
- Android基础教程之----SMS简单发送短信程序(两个模拟器之间的通信)!
- Android调用拨号程序,短信发送
- Android开发(一)发送短信程序
- android之发送短信程序
- Android基础教程之----SMS简单发送短信程序(两个模拟器之间的通信)!
- android 短信发送程序
- Android自制发送短信程序
- Android调用发送短信程序
- Android发送短信、打电话、发送邮件的程序集合&&广播权限
- Android基础教程之----SMS简单发送短信程序(两个模拟器之间的通信)!
- Android发送短信的病毒程序
- Android基础教程之----SMS简单发送短信程序(两个模拟器之间的通信)!
- Android短信发送程序
- Android 发送短信小程序
- Android自制发送短信程序
- Android开发中实现发送短信的小程序示例
- Android基础教程之----SMS简单发送短信程序(两个模拟器之间的通信)!