Android 发送短信与接收短信

package com.example.testsms;

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.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    private Button send;
    private TextView number;
    private TextView content;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        send = (Button) this.findViewById(R.id.button1);
        number = (TextView) this.findViewById(R.id.textView2);
        content = (TextView) this.findViewById(R.id.textView1);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com");
        MyReceiver myReceiver = new MyReceiver();
        registerReceiver(myReceiver,intentFilter);
        send.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                SmsManager smsManager = SmsManager.getDefault();
                Intent sentIntent = new Intent("com");
                PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, sentIntent, 0);
                smsManager.sendTextMessage((String)number.getText(),null,(String)content.getText(),pendingIntent,null);
            }
        });
        
    }
    class MyReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            // TODO Auto-generated method stub
            if(getResultCode()==RESULT_OK){
                Toast.makeText(MainActivity.this,"success",1).show();
            }
        }
    }
}
package com.example.mysms6;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.app.Activity;
import android.telephony.SmsMessage;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {
    private TextView textView1;
    private TextView textView2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView1 = (TextView) this.findViewById(R.id.textView);
        textView2 = (TextView) this.findViewById(R.id.textView3);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.setPriority(800);
        intentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
        MyReceiver myReceiver = new MyReceiver();
        registerReceiver(myReceiver,intentFilter);
    }
    class MyReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            Bundle bundle = intent.getExtras();
            Object[] pdus = (Object[])bundle.get("pdus");
           SmsMessage[] messages = new SmsMessage[pdus.length];
            for(int i= 0;i<messages.length;i++){
                messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
            }
            String address = messages[0].getOriginatingAddress();
            String fullMessage = "";
            for(SmsMessage message:messages){
                fullMessage += message.getMessageBody();
            }
            textView1.setText(address);
            textView2.setText(fullMessage);
            abortBroadcast();
        }
    }
}

方便自己以后查阅吧 一个用eclipse  一个用Android Studio

posted @ 2015-09-28 09:41  邻家小书童  阅读(334)  评论(0编辑  收藏  举报