Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android短信監聽(一)——利用BroadcastReceiver實現短信監聽

Android短信監聽(一)——利用BroadcastReceiver實現短信監聽

編輯:關於Android編程

MainActivity如下:

package cc.testsmsbroadcastreceiver;
import cc.testsmsbroadcastreceiver.SMSBroadcastReceiver.MessageListener;
import android.os.Bundle;
import android.widget.TextView;
import android.app.Activity;
/**
 * Demo描述:
 * 利用BroadcastReceiver實現監聽短信
 * 
 * 注意權限:
 * 
 * 
 * 詳細資料:
 * http://blog.csdn.net/lfdfhl/article/details/8195400
 * 
 */
public class MainActivity extends Activity{
    private TextView mTextView;
    private SMSBroadcastReceiver mSMSBroadcastReceiver;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		init();
	}
    private void init(){
    	mTextView=(TextView) findViewById(R.id.textView);
    	mSMSBroadcastReceiver=new SMSBroadcastReceiver();
    	mSMSBroadcastReceiver.setOnReceivedMessageListener(new MessageListener() {
			@Override
			public void OnReceived(String message) {
				mTextView.setText(message);
			}
		});
    }

}

SMSBroadcastReceiver如下:

package cc.testsmsbroadcastreceiver;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsMessage;
/**
 * 配置廣播接收者:
 *  
 *     
 *         
 *     
 *  
 *  
 *  注意:
 *  表示:
 *  設置此廣播接收者的級別為最高
 */

public class SMSBroadcastReceiver extends BroadcastReceiver {
    private static MessageListener mMessageListener;
	public SMSBroadcastReceiver() {
		super();
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		     Object [] pdus= (Object[]) intent.getExtras().get("pdus");
		     for(Object pdu:pdus){
		    	SmsMessage smsMessage=SmsMessage.createFromPdu((byte [])pdu);
		    	String sender=smsMessage.getDisplayOriginatingAddress();
		    	String content=smsMessage.getMessageBody();
		    	long date=smsMessage.getTimestampMillis();
		    	Date timeDate=new Date(date);
		    	SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		    	String time=simpleDateFormat.format(timeDate);
		    	
		    	System.out.println("短信來自:"+sender);
		    	System.out.println("短信內容:"+content);
		    	System.out.println("短信時間:"+time);
		    	
		    	mMessageListener.OnReceived(content);
		    	
		    	//如果短信來自5556,不再往下傳遞
		    	if("5556".equals(sender)){
		    		System.out.println(" abort ");
		    		abortBroadcast();
		    	}
		    	
		     }
	}
	
	// 回調接口
		public interface MessageListener {
			public void OnReceived(String message);
		}

		public void setOnReceivedMessageListener(MessageListener messageListener) {
			this.mMessageListener=messageListener;
		}
}

main.xml如下:



    


AndroidManifest.xml如下:




    
    
    
    

    
        
            
                

                
            
        
        
        
			
				
			
		
    



  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved