WEB开发网
开发学院手机开发Android 开发 andriod发送中文短信方法 阅读

andriod发送中文短信方法

 2012-10-10 16:58:33 来源:WEB开发网   
核心提示: 此方法主要是利用另外一个接口:SmsManager.sendMultipartTextMessage()此代码在android4.0;测试通过package com.example.sendsms;import java.util.ArrayList;import android.app.Activity;impor

 此方法主要是利用另外一个接口:
SmsManager.sendMultipartTextMessage()

此代码在android4.0;测试通过

package com.example.sendsms;

import java.util.ArrayList;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.telephony.SmsManager;

public class smsHelp {
	
	  public boolean sendSMS(Context mContext, final String sendtext)
	  {
		// to send SMS		
		if (sendtext == null) return false;
		String sendText = new String(sendtext);

	    Intent localIntent = new Intent("SENT_SMS_ACTION");
//	    PendingIntent localPendingIntent = PendingIntent.getBroadcast(mService.this, 0, localIntent, 0);

	    SendSMSCallback(mContext);
	    SmsManager localSmsManager = SmsManager.getDefault();
	    try {
	    	ArrayList<String> messages = localSmsManager.divideMessage(sendText);
	    	int messageCount = messages.size();
	    	ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>(messageCount);
	    	for (int i = 0; i < messageCount; i++) {
	    		int requestCode = 0;
	            if (i == messageCount -1) {
	                // Changing the requestCode so that a different pending intent
	                // is created for the last fragment with
	                requestCode = 1;
	            }
	    		sentIntents.add(PendingIntent.getBroadcast(mContext, requestCode, localIntent, 0));
	    	}
	    	localSmsManager.sendMultipartTextMessage("189607264923", null, messages, sentIntents, null);
//	    	localSmsManager.sendTextMessage(mSendPhoneNumber, null, sendText, localPendingIntent, null);
	    }
	    catch (Exception localException)
	    {
	    	localException.printStackTrace();
	    	return false;
	    }	
	    return true;
	    /**/	    
	  }	  
	  
	  public void SendSMSCallback(Context mContext)
	  {    
		   SendReceive sendReceive = new SendReceive();
	       IntentFilter sendFilter = new IntentFilter();
	       sendFilter.addAction("SENT_SMS_ACTION");
	       mContext.registerReceiver(sendReceive, sendFilter); 
	  }	  
	  
	  public class SendReceive extends BroadcastReceiver {

		    @Override
		    public void onReceive(Context context, Intent intent) {
		        switch (getResultCode()) {  
		        case Activity.RESULT_OK:  
		        	//send success
		            break;  
		        default:  
		        	//send fail
		            break;  
		        }  		   
		    }
	  }	  
}

Tags:andriod 发送 短信

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接