android发短信

作者:追风剑情 发布于:2015-5-18 17:54 分类:Android

package com.test.message;

import java.util.List;

import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.telephony.SmsManager;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

	private static final String SENT_SMS_ACTION = "SENT_SMS_ACTION";

	private static final String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";

	private Button bt;
	
	private BroadcastReceiver sendMessage;
	private BroadcastReceiver receiver;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		if (savedInstanceState == null) {
			getSupportFragmentManager().beginTransaction()
					.add(R.id.container, new PlaceholderFragment()).commit();
		}
		
		sendMessage = new BroadcastReceiver() {
			@Override
			public void onReceive(Context c, Intent intent) {
				// TODO Auto-generated method stub
				//判断短信是否发送成功   
				switch (getResultCode()) {  
				case Activity.RESULT_OK:  
					Toast.makeText(c, "短信发送成功", Toast.LENGTH_SHORT).show();  
					break;  
				default:  
					Toast.makeText(c, "发送失败", Toast.LENGTH_LONG).show();  
					break;  
				}  
			}
		};
			
		receiver = new BroadcastReceiver() {  
			@Override  
			public void onReceive(Context context, Intent intent) {  
				//表示对方成功收到短信   
				Toast.makeText(context, "对方接收成功",Toast.LENGTH_LONG).show();  
			}
		};
		
		registerReceiver(sendMessage, new IntentFilter(SENT_SMS_ACTION));
		registerReceiver(receiver, new IntentFilter(DELIVERED_SMS_ACTION));
		
		
		
		bt = (Button)findViewById(R.id.button1);
		bt.setOnClickListener(new Button.OnClickListener(){
			@Override
            public void onClick(View v)
            {
				String destinationAddress = "电话号码";
				String content = "内容";
				SmsManager smsManager = SmsManager.getDefault();
               
				Intent sentIntent = new Intent(SENT_SMS_ACTION);
				PendingIntent sentPI = PendingIntent.getBroadcast(MainActivity.this,0,sentIntent,0);
				
				Intent deliveryIntent = new Intent(DELIVERED_SMS_ACTION);
				PendingIntent deliverPI = PendingIntent.getBroadcast(MainActivity.this,0,deliveryIntent,0);
				
                
                if(content.length() >= 70)
                {
                    //短信字数大于70,自动分条
                    List<String> ms = smsManager.divideMessage(content);
                    
                    for(String str : ms )
                    {
                        //短信发送
                        smsManager.sendTextMessage(destinationAddress, null, str, sentPI, deliverPI);
                    }
                }
                else
                {
                    smsManager.sendTextMessage(destinationAddress, null, content, sentPI, deliverPI);
                }
                
                //--------保存信息记录--------
                ContentValues values = new ContentValues();
                //发送时间
                values.put("date", System.currentTimeMillis()); 
                //阅读状态            
                values.put("read", 0);
                //1为收 2为发           
                values.put("type", 2);
                //送达号码            
                values.put("address",destinationAddress);
                //送达内容          
                values.put("body", content);
                //插入短信库  
                getContentResolver().insert(Uri.parse("content://sms/sent"), values);
            }
		});
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_main, container,
					false);
			return rootView;
		}
	}

}

AndroidManifest.xml中添加权限配置

 <uses-permission android:name="android.permission.SEND_SMS"/>
 <uses-permission android:name="android.permission.READ_SMS"/>
 <uses-permission android:name="android.permission.WRITE_SMS"/>

标签: Android

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号