文章标题 Android系统广播的发送短信和接收短信的一些用法

//<1>实现的一些功能
按钮:(设置安全号码)—–>对话框(手动添加/从通讯录有中获取)
立马:发短信(SMSManager)给安全号码发送短信

要在AndroidManifest.xml这里里面加上权限 如下:

 
    <uses-permission android:name="android.permission.SEND_SMS">uses-permission>``
    
    <uses-permission android:name="android.permission.READ_CONTACTS">uses-permission>
    
    <uses-permission android:name="android.permission.RECEIVE_SMS">uses-permission>

主要功能有1.通过 Intent跳转到自带的手机联系人,在通过获取联系人的获得联系人的ID号, 获得联系人电话的cursor,通过ID可以获取号码,2.发送通过 SmsManager smsManager = SmsManager.getDefault();短信来的



    private EditText num;
    private EditText content;
    private Button btnSends;
    private SharedPreferences.Editor ed;
    private String strNo;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //得到号码的编辑器
        num = (EditText) findViewById(R.id.Number);
               //得到内容的编辑器
        content = (EditText) findViewById(R.id.Content);
        //得到联系人
      btnSends = (Button) findViewById(R.id.btnSends);

//可以SharedPreference储存数据

        SharedPreferences sp= getSharedPreferences("loginfo", Context.MODE_PRIVATE);
        //使号码进行第二次打开时可以保存
        ed = sp.edit();
        String name=sp.getString("name","");
        num.setText(name);
//
        btnSends.setOnClickListener(new Button.OnClickListener() {

            @Override
            public void onClick(View v) {
            //跳转手机自带的联系人
                // TODO Auto-generated method stub
                Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);

                MainActivity.this.startActivityForResult(intent, 1);
            }

        });
    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 1:
                if (resultCode == RESULT_OK) {
                    Uri contactData = data.getData();
                    Cursor cursor = managedQuery(contactData, null, null, null,
                            null);
                    cursor.moveToFirst();
                    //得到你选择的手机号码
                    String nums = this.getContactPhone(cursor);
                    Log.i("test", "所选手机号为:" + nums);
                    //将编辑器直接赋值
                   num.setText( nums);
                }
                break;

            default:
                break;
        }
    }

    private String getContactPhone(Cursor cursor) {
        // TODO Auto-generated method stub
        int phoneColumn = cursor
                .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
        int phoneNum = cursor.getInt(phoneColumn);
       String result = "";
        if (phoneNum > 0) {
            // 获得联系人的ID号
            int idColumn = cursor.getColumnIndex(ContactsContract.Contacts._ID);
            String contactId = cursor.getString(idColumn);
            // 获得联系人电话的cursor
            Cursor phone = getContentResolver().query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "="
                            + contactId, null, null);
            if (phone.moveToFirst()) {
                for (; !phone.isAfterLast(); phone.moveToNext()) {
                    int index = phone
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                    int typeindex = phone
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
                    int phone_type = phone.getInt(typeindex);
                    String phoneNumber = phone.getString(index);
                    result = phoneNumber;
//                  switch (phone_type) {//此处请看下方注释
//                  case 2:
//                      result = phoneNumber;
//                      break;
//
//                  default:
//                      break;
//                  }
                }
                if (!phone.isClosed()) {
                    phone.close();
                }
            }
        }
        return result;
    }
//保存手机号码
    public void ok(View view) {
        strNo = num.getText().toString();
        ed.putString("name", strNo);
        ed.commit();
    }

    ////////发送
    public void send(View view) {
        strNo = num.getText().toString();
        ed.putString("name", strNo);
        ed.commit();

        String strContent = content.getText().toString();
        String as="1.报警(音乐)";
        strContent+=as;
        SmsManager smsManager = SmsManager.getDefault();
        //如果字数超过5,需拆分成多条短信发送
        if (strContent.length() > 5) {
            ArrayList msgs = smsManager.divideMessage(strContent);
            for (String msg : msgs) {
                smsManager.sendTextMessage(strNo, null, msg, null, null);
            }
        } else {
            smsManager.sendTextMessage(strNo, null, strContent, null, null);
        }

        //content.setText("1.*#*#7780#*#*");

        //Toast.makeText(SendSMS.this, "短信发送完成", Toast.LENGTH_LONG).show();
    }





你可能感兴趣的:(系统广播,安卓)