两个模拟器之间对发短信

看了一些模拟器之间实现短信的文章。我也写一下。这样记得会更清晰一些。

①创建一个DEMO,我将它命名为SMSTest

②先来设计一个UI,这里使用的是AbsoluteLayout布局。

main.xml

<?xml version="1.0" encoding="utf-8"?>

<AbsoluteLayout android:id="@+id/AbsoluteLayout01" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/myTextView01"
android:layout_width="wrap_content"
android:layout_height="26px"
android:text="收件人"
android:layout_x="6px"
android:layout_y="21px"
>
</TextView>
<Button
android:id="@+id/myButton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送"
android:layout_x="11px"
android:layout_y="302px"
>
</Button>
<EditText
android:id="@+id/myEditText01"
android:layout_width="200px"
android:layout_height="49px"
android:textSize="18sp"
android:layout_x="108px"
android:layout_y="10px"
>
</EditText>
<EditText
android:id="@+id/myEditText02"
android:layout_width="fill_parent"
android:layout_height="226px"
android:textSize="18sp"
android:layout_x="5px"
android:layout_y="64px"
>
</EditText>

</AbsoluteLayout>

 ③主程序SMSTest.java

 

package com.android.zhoufei;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class SMSTest extends Activity {
	//声明控件
	Button myButton01;
	EditText myEditText01;
	EditText myEditText02;
	TextView myTextView01;
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //获取资源
        myButton01=(Button)findViewById(R.id.myButton01);
        myTextView01=(TextView)findViewById(R.id.myTextView01);
        myEditText01=(EditText)findViewById(R.id.myEditText01);
        myEditText02=(EditText)findViewById(R.id.myEditText02);
        //点击发送按钮后的事件
        myButton01.setOnClickListener(new OnClickListener(){
        	public void onClick(View v){
        		//获取收件人和信息内容 
        		String mgAddress=(myEditText01).getText().toString();
        		String mgContent=(myEditText02).getText().toString();
        		//构建一个取得default instance的SMSManager对象
        		SmsManager smsManager=SmsManager.getDefault();    
        		//检查输入的收件人和信息内容是否为空
        		if(mgAddress.trim().length()!=0&&mgContent.trim().length()!=0){
        			try{
        				PendingIntent pintent=PendingIntent.getBroadcast(SMSTest.this, 0, new Intent(), 0);
        				smsManager.sendTextMessage(mgAddress, null, mgContent, pintent, null);
        			}catch(Exception e){
        				e.getStackTrace();
        			}
        			Toast.makeText(SMSTest.this, "发送成功", Toast.LENGTH_LONG).show();
        		}else{
        			Toast.makeText(SMSTest.this, "发送失败", Toast.LENGTH_LONG).show();
        		}
        		}
        });
    }
}

 在这里应该注意的是SmsManager这个对象是用来处理发送文本,数据,短信的。获得这个对象需要通过SmsManager.getDefalut()这个方法来调用。

④修改一下androidmanifest.xml添加一下发送短消息的权限.

添加这句<uses-permission android:name="android.permission.SEND_SMS"/>,就OK了。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.android.zhoufei"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".SMSTest"
                  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"></uses-permission>
</manifest> 

 ⑤运行一下吧。

需要打开连个模拟器

运行:emulator -data <filename>

eg:emulator -data d:\test\

 

如果sdk的版本低的话,应该显示不出中文来。恩,应该是这样。

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值