`
alen456
  • 浏览: 2202 次
  • 性别: Icon_minigender_1
  • 来自: 广州
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

短信发送器

阅读更多
短信发送器
建一个sms的工程
string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, SMSActivity!</string>
    <string name="app_name">短信发送器</string>
    <string name="mobile">请输入手机号</string>
    <string name="content">请输入短信内容</string>
    <string name="button">发送短信</string>
    <string name="info">发送成功</string>
</resources>
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:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/mobile"
    />
<EditText 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/mobile"
    />
<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="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button"
    android:id="@+id/button"
/>
</LinearLayout>
SMSActivity.Java

package cn.itcast.activity;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SMSActivity extends Activity {
private EditText mobileText;
private EditText contentText;
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mobileText = (EditText)this.findViewById(R.id.mobile);
        contentText = (EditText)this.findViewById(R.id.content);
        Button button = (Button)this.findViewById(R.id.button);
       
        button.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
String mobile = mobileText.getText().toString();
String content = contentText.getText().toString();

SmsManager smsManager = SmsManager.getDefault();

if(content.length() > 70){
List<String> contents = smsManager.divideMessage(content);
for(String sms : contents){
smsManager.sendTextMessage(mobile, null, sms, null, null);
}
}else{
smsManager.sendTextMessage(mobile, null, content, null, null);
}
Toast.makeText(SMSActivity.this, R.string.info, Toast.LENGTH_LONG).show(); //吐西
}
});
       
    }
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="cn.itcast.activity"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".SMSActivity"
                  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="7" />
<uses-permission android:name="android.permission.SEND_SMS"/>
</manifest>
  • 大小: 49.2 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics