青玄 发表于 2014-11-5 22:58:31

[原创]android四大组件之activity-简单的短信发送程序

本帖最后由 青玄 于 2014-11-5 23:03 编辑

源码概述:*******************************************布局文件与配置文件**************************************************************
1.布局文件:(1)activity_contacts.xml:用来显示取消和确定的;(2)activity_main.xml:用来显示短信界面的
                     (3)ui_phone_view.xml:用来显示联系人信息的;(4)contacts_items.xml:这个事显示自定义组件的
2.AndroidMainfest.xml文件的配置:
                      (1)权限的配置:            
<uses-permission android:name="android.permission.SEND_SMS"/>
    <uses-permission android:name="android.permission.READ_SMS"/>
    <uses-permission android:name="android.permission.WRITE_SMS"/>
    <uses-permission android:name="android.permission.READ_CONTACTS"/>
    <uses-permission android:name="android.permission.WRITE_CONTACTS"/>                        (2)activity的配置:
<activity android:name="com.cbd.sendsms.SelectContactsActivity" android:label="选择联系人"></activity>3.创建自定义组件时values文件夹所需要的配置文件declare.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="phone_view">
          <attr name="name" format="string" />
          <attr name="phone" format="string" />
   </declare-styleable>
</resources>***********************************************src里面的代码概述以及知识概述**************************************************
1.自定义控件:
自定义控件实质上就是继承view类或者view类的子类所实现的一些功能而已!用它我们可以编写自己所需要的控件,就拿本案例
来说:这个类继承了RelativeLayout然后在构造方法里面实现自定义控件的操作:
         首先我们需要获取局部填充器然后调用inflate方法将布局文件转化为一个view对象,然后将view里面的值赋给本类中的tv_name,
    tv_phone,然后取得配置文件中的属性值,将它设置到本类中的成员里面去。其实自定义控件说白了,就是在子类里面定义一些控件的类,
然后将其她的布局文件转化成view类,再将这个view所包含的属性赋给本类中的成员而已!一下是自定义控件的主要代码:
package com.cbd.sendsms.ui;

import com.cbd.sendsms.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class PhoneView extends RelativeLayout {

      private TextView tv_name;
      private TextView tv_phone;
      private CheckBox checkBox;

      public PhoneView(Context context, AttributeSet attrs) {
                super(context, attrs);

                // 获取局部填充器把ui_phone_view.xml文件中的各种控件对象找到并赋给本类中的tv_name,tv_phone,cb
                LayoutInflater layoutInflater = (LayoutInflater) context
                              .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View view = layoutInflater.inflate(R.layout.ui_phone_view, this);
                tv_name = (TextView) view.findViewById(R.id.tv_ui_phone_name);
                tv_phone = (TextView) view.findViewById(R.id.tv_ui_phone_content);
                checkBox = (CheckBox) view.findViewById(R.id.cb_ui_phoneview_status);
                // 取得清单文件中的属性值
                TypedArray typeArray = context.obtainStyledAttributes(attrs,
                              R.styleable.phone_view);
                // 取得清单文件中的属性值
                String name = typeArray.getString(R.styleable.phone_view_name);
                String phone = typeArray.getString(R.styleable.phone_view_phone);
                tv_name.setText(name);
                tv_phone.setText(phone);
               
      }
      
      public void setName(String name)
      {
                tv_name.setText(name);
      }
      
      public void setPhone(String phone)
      {
                tv_phone.setText(phone);
      }
      
      public boolean isChecked()
      {
                return checkBox.isChecked();
      }
      
      public void setCheckBox(boolean isChecked)
      {
                checkBox.setChecked(isChecked);
      }

      
}












2.发送短信的方法:
public void send(View v)
        {
                if(selectedFlag==0   //这个标志的意思是说,如果是用户自己输入的话,那就执行这个if
                {
                        String mobileNumber=mobile.getText().toString().trim();
                        String contents=content.getText().toString().trim();
                        if(!TextUtils.isEmpty(mobileNumber) && !TextUtils.isEmpty(contents))
                        {
                                ContentValues values=new ContentValues();
                                values.put("address",mobileNumber);
                                values.put("type",2);
                                SmsManager smsManager=SmsManager.getDefault();
                                List<String> listSms=smsManager.divideMessage(contents);
                                for(String s : listSms)
                                {
                                        smsManager.sendTextMessage(mobileNumber, null, s, null, null);
                                        values.put("body",s);
                                        values.put("date", new Date().getTime());
                                        resolver.insert(uri, values);
                                }
                                values=null;
                                mobile.setText("");
                                content.setText("");
                                Toast.makeText(this, "信息发送成功!", Toast.LENGTH_SHORT).show();
                        }else{
                                Toast.makeText(this, "电话号码或信息内容不能为空!", Toast.LENGTH_SHORT).show();
                        }
                }else                                 //如果不是用户自己输入的话就执行这段代码
                        String contents=content.getText().toString().trim();
                        if(!TextUtils.isEmpty(contents))//判断联系人为不为空
                        {
                                //循环选中的每个联系人发送信息
                                for(Contact c : selectContact)   //循环集合里面的内容
                                {
                                        ContentValues values=new ContentValues();
                                        values.put("address",c.getPhone());   //号码
                                        values.put("type",2);   //发送发的类型码
                                        SmsManager smsManager=SmsManager.getDefault();//
                                        List<String> listSms=smsManager.divideMessage(contents);   //拆分字数,分批发送每次最多发送70个字
                                        for(String s : listSms)
                                        {
                                                smsManager.sendTextMessage(c.getPhone(), null, s, null, null);   //执行发送方法
                                                values.put("body",s);   
                                                values.put("date", new Date().getTime());
                                                resolver.insert(uri, values);   //将发送的数据插入到数据库中
                                        }
                                        values=null;
                                        Toast.makeText(this, "给"+c.getName()+"发送信息成功!", Toast.LENGTH_SHORT).show();
                                }
                               
                                mobile.setText("");
                                content.setText("");
                                selectContact=null;
                                selectedFlag=0;
                                mobile.setEnabled(true); //文本框变为可用
                               
                               
                        }else{
                                Toast.makeText(this, "信息内容不能为空!", Toast.LENGTH_SHORT).show();
                        }
                }
               
               
               
        }

3.给组件设置适配器的类:

private class MyAdapter extends BaseAdapter
        {

                @Override
                public int getCount() {
                       
                        return contacts.size();   //这个返回的是联系人的总数
                }

                @Override
                public Object getItem(int position) {
                        // TODO Auto-generated method stub
                        return contacts.get(position);//返回所指定的条目
                }

                @Override
                public long getItemId(int position) {
                        // TODO Auto-generated method stub
                        return position;         //返回指定条目的位置
                }

                @Override
                public View getView(int position, View convertView, ViewGroup parent) {
                        View view=View.inflate(SelectContactsActivity.this, R.layout.contacts_items, null);
                        //将布局文件转化为view对象注意这个布局文件里面的组件就是你自定义的那个组件
                        PhoneView phoneView=(PhoneView) view.findViewById(R.id.phoneView);   //获取自定义组件的对象
                        phoneView.setName(contacts.get(position).getName());   //设置它的属性
                        phoneView.setPhone(contacts.get(position).getPhone());
                        phoneView.setCheckBox(contacts.get(position).isChecked());
                        return view;            //返回view对象
                }
               
        }


拈花小仙 发表于 2014-11-6 01:21:30

{:7_139:}支持玄玄~

青玄 发表于 2014-11-6 08:59:51

拈花小仙 发表于 2014-11-6 01:21
支持玄玄~

{:9_227:}

qq351317878 发表于 2014-11-6 09:43:12

大神啊,太牛了   {:5_111:}
页: [1]
查看完整版本: [原创]android四大组件之activity-简单的短信发送程序