android学习笔记3--------------短信发送器

效果图:

android学习笔记3--------------短信发送器_第1张图片


import java.util.List;

import android.app.Activity;
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.Toast;

public class SendMessageActivity extends Activity
{
	private EditText mobileEditText;
	private EditText contenText;
	private Button sendbButton;
	private Button exitButton;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mobileEditText=(EditText)findViewById(R.id.mobile);
		contenText=(EditText)findViewById(R.id.content);
		sendbButton=(Button)findViewById(R.id.send);
		exitButton=(Button)findViewById(R.id.exit);
		
		sendbButton.setOnClickListener(listener);
		exitButton.setOnClickListener(listener);
	}
	
	private OnClickListener listener=new OnClickListener()
	{
		
		@Override
		public void onClick(View v)
		{
			Button button=(Button)v;
			switch (button.getId())
			{
				case R.id.send:
					String mobile=mobileEditText.getText().toString();
					String contentString=contenText.getText().toString();
					SmsManager smsManager=SmsManager.getDefault();
					
					//Intent intent=new Intent();				
					//intent.setAction(Intent.ACTION_SENDTO);
					//intent.setData(Uri.parse("smsto:"+mobile));
					if(contentString.length()>70)
					{
						List<String> contents=smsManager.divideMessage(contentString);
						for(String sms:contents)
						{
							smsManager.sendTextMessage(mobile, null, sms, null, null);
							//intent.putExtra("sms_body", sms);
							//SendMessageActivity.this.startActivity(intent);
						}
					}
					else 
					{
						smsManager.sendTextMessage(mobile, null, contentString, null, null);
						//intent.putExtra("sms_body", contentString);
						//SendMessageActivity.this.startActivity(intent);
					}
					Toast.makeText(SendMessageActivity.this, "短信已发送", Toast.LENGTH_SHORT).show();
					break;
					
				case R.id.exit:
					android.os.Process.killProcess(android.os.Process.myPid());
					break;
	
				default:
					break;
			}
			
		}
	};
}



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="请输入手机号"
    />
    
<EditText 
	android:layout_height="wrap_content" 
	android:id="@+id/mobile" 
	android:layout_width="match_parent" 
	></EditText>
	
<TextView 	
	android:layout_width="wrap_content" 
	android:layout_height="wrap_content"
	android:text="请输入短信内容" 
	></TextView>
	
<EditText 
	android:layout_height="wrap_content" 	
	android:layout_width="match_parent" 
	android:id="@+id/content"
	android:lines="3" 
	></EditText>
	
<Button 
	android:layout_width="wrap_content" 
	android:layout_height="wrap_content"
	android:id="@+id/send" 
	android:text="发送短信" 
	></Button>
	
<Button 
	 android:layout_width="wrap_content" 
	 android:layout_height="wrap_content"
	 android:id="@+id/exit"
	 android:text="退出软件" 
	 ></Button>
</LinearLayout>




加入权限:
 <uses-permission android:name="android.permission.SEND_SMS"></uses-permission>


你可能感兴趣的:(android,layout,null,mobile,button,sms)