• 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

  • 相关阅读:
    Light oj 1082 Array Queries(区间最小值)
    Codeforces Round #179 (Div. 2)A、B、C、D
    poj 1976 A Mini Locomotive(01背包)
    Codeforces Round #178 (Div. 2)
    hackerrank challenges median
    poj 1961 Period(kmp最短循环节)
    poj 2182 Lost Cows(树状数组)
    ZOJ1117 POJ1521 HDU1053 Huffman编码
    poj 2352 Stars 树状数组
    这可能是最适合萌新入门Web安全的路线规划
  • 原文地址:https://www.cnblogs.com/assassin666/p/4843303.html
Copyright © 2020-2023  润新知