码迷,mamicode.com
首页 > 移动开发 > 详细

Android 发送短信与接收短信

时间:2015-09-28 10:00:40      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:

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

Android 发送短信与接收短信

标签:

原文地址:http://www.cnblogs.com/assassin666/p/4843303.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!