android通讯录之短信
来源:互联网 发布:php开源三级分销商城 编辑:程序博客网 时间:2024/06/05 14:09
本篇文章记录通讯录短信功能的实现
效果图:
1、短信列表布局:sms_list_view.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" ><LinearLayout android:id="@+id/LinearLayout1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#c0c0c0" android:orientation="vertical"> <TextView android:text="短信" android:textColor="#000" android:textSize="30dip" android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center"> </TextView></LinearLayout><strong></strong><FrameLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ff666666"> </ListView> <ImageButton android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/new_sms_btn" android:layout_gravity="bottom|center" android:layout_marginBottom="20dp<strong>" </strong> android:padding="20dp" android:background="@drawable/btn_bg_sms_send"></ImageButton></FrameLayout></LinearLayout><strong></strong>2、sms_list_item.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:paddingTop="5dip" > <QuickContactBadge android:id="@+id/qcb" android:layout_width="75dip" android:layout_height="75dip" android:layout_marginBottom="3dip" android:src="@drawable/ic_a9" /> <LinearLayout android:id="@+id/ll" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="3dip" android:layout_toRightOf="@+id/qcb" android:gravity="center_vertical" android:orientation="horizontal" > <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="true" android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="#ffffff" /> <TextView android:id="@+id/count" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dip" android:singleLine="true" android:textAppearance="?android:attr/textAppearanceSmall" android:textColor="#ffffff" /> </LinearLayout> <TextView android:id="@+id/date" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/ll" android:gravity="right" android:singleLine="true" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:textAppearance="?android:attr/textAppearanceSmall" android:textColor="#ffffff" /> <TextView android:id="@+id/content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/date" android:layout_gravity="bottom" android:layout_marginLeft="3dip" android:layout_toRightOf="@+id/qcb" android:singleLine="true" android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="#5CACEE" /> </RelativeLayout>
3、代码实现(SMSListActivity.java)
package com.example.SMS;import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.content.pm.ActivityInfo;import android.net.Uri;import android.os.Bundle; import android.view.View;import android.view.View.OnClickListener;import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener;import android.widget.ImageButton;import android.widget.ListView;import com.example.SMS.RexseeSMS;import com.example.SMS.SMSAdapter;import com.example.SMS.SMSBean;import com.example.service.MyService;import com.example.telephone.ContactsActivity;import com.example.telephone.ContactsActivity.MyReceiver;import com.example.R; /** * 短信列表 * * @author Administrator * */ public class SMSListActivity extends Activity { private ListView smsListView; private SMSAdapter smsAdapter; private RexseeSMS rsms;private MyReceiver receiver=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.sms_list_view); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR); smsListView = (ListView) findViewById(R.id.list); init(); } protected void onStart(){ super.onStart(); startService(new Intent(SMSListActivity.this,MyService.class));receiver = new MyReceiver();IntentFilter filter = new IntentFilter();filter.addAction("com.example.service.MyService");SMSListActivity.this.registerReceiver(receiver,filter); } public void init(){ smsAdapter = new SMSAdapter(SMSListActivity.this); rsms = new RexseeSMS(SMSListActivity.this); List<SMSBean> list_mmt = rsms.getThreadsNum(rsms.getThreads(0)); // 注入短信列表数据 smsAdapter.assignment(list_mmt); // 填充数据 smsListView.setAdapter(smsAdapter); // 短信列表项点击事件 smsListView.setOnItemClickListener(new OnItemClickListener(){@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position, long id) {// TODO Auto-generated method stubMap<String,String>map = new HashMap<String,String>();SMSBean sb = smsAdapter.getItem(position);map.put("phoneNumber", sb.getAddress());map.put("threadId", sb.getThread_id());BaseIntentUtil.intentSysDefault(SMSListActivity.this, HuiHuaActivity.class, map);} }); ImageButton b = (ImageButton)this.findViewById(R.id.button1); b.setOnClickListener(new OnClickListener(){ private String smsBody; @Override public void onClick(View v) {// TODO Auto-generated method stubUri smsToUri = Uri.parse("smsto:"); Intent intent = new Intent(Intent.ACTION_SENDTO, smsToUri); intent.putExtra("sms_body", smsBody); startActivity(intent); } }); } protected void onDestroy(){ stopService(new Intent(SMSListActivity.this,MyService.class)); super.onDestroy(); } public class MyReceiver extends BroadcastReceiver{@Overridepublic void onReceive(Context context, Intent intent) {// TODO Auto-generated method stubBundle bundle = intent.getExtras();int i = bundle.getInt("i");smsListView.setFilterText(i+"");} } }4、适配器
package com.example.SMS;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.List;import com.example.R;import com.example.bohao.CallLogBean;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.TextView;public class SMSAdapter extends BaseAdapter { private List<SMSBean> smsList; private Date date; private SimpleDateFormat sdf; private Context context; public SMSAdapter(Context context) { this.smsList = new ArrayList<SMSBean>(); this.date = new Date(); this.context = context; this.sdf = new SimpleDateFormat("MM/dd"); } // HH:mm@Overridepublic int getCount() {// TODO Auto-generated method stubreturn smsList!=null?smsList.size():0;}@Overridepublic SMSBean getItem(int position) {// TODO Auto-generated method stubreturn smsList.get(position);}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stub return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stubViewHolder holder = null;if(convertView == null){ convertView = LayoutInflater.from(context).inflate(R.layout.sms_list_item,parent,false); holder = new ViewHolder(); holder.name = (TextView) convertView.findViewById(R.id.name); holder.count = (TextView) convertView.findViewById(R.id.count); holder.date = (TextView) convertView.findViewById(R.id.date); holder.content = (TextView) convertView.findViewById(R.id.content); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } SMSBean smsBean = smsList.get(position); holder.name.setText(smsBean.getAddress()); holder.count.setText('('+smsBean.getCount()+')'); this.date.setTime(smsBean.getDate()); holder.date.setText(this.sdf.format(date)); holder.content.setText(smsBean.getSnippet());return convertView;} public class ViewHolder { public TextView name; public TextView count; public TextView date; public TextView content;}public void assignment(List<SMSBean> list_mmt) {// TODO Auto-generated method stub //this.smsList = smsList; this.smsList = list_mmt; } }
5、短信内容布局(huihua_item_view.xml)
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" ><RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/re" android:background="#c0c0c0"> <ImageView android:id="@+id/im" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="返回" android:textColor="#000" android:layout_marginTop="8dip" android:layout_marginLeft="10dip" android:background="@drawable/abc_ic_ab_back_mtrl_am_alpha"/> <TextView android:id="@+id/text" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="信息" android:textSize="30dip" android:textColor="#ffffffff" android:layout_gravity="center" android:layout_centerInParent="true"> </TextView> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toLeftOf="@+id/image1" android:background="@drawable/tab_contact_selected" android:layout_marginTop="10dip" android:layout_marginRight="8dip"/> <ImageView android:id="@+id/image1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:background="@drawable/call" android:layout_marginTop="10dip" android:layout_marginRight="13dip"/> </RelativeLayout><RelativeLayout android:id="@+id/Re" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@+id/bottom_ll"> </ListView> <LinearLayout android:id="@+id/bottom_ll" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_below="@+id/lin" android:layout_gravity="bottom" android:background="@drawable/main_tab_bg" android:orientation="horizontal" android:padding="5dip" > <Button android:id="@+id/duomeiti" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical|left" android:background="@android:drawable/stat_notify_chat" /> <EditText android:id="@+id/ed" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginLeft="5dip" android:layout_marginRight="5dip" android:layout_weight="1" android:background="@drawable/bg_sms_input" android:gravity="left|top" android:hint="输入短信内容" android:maxLines="4" android:padding="5dip" /> <Button android:id="@+id/fasong" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical|right" android:background="@android:drawable/ic_menu_send" android:textColor="#ffffff" /> </LinearLayout></RelativeLayout></LinearLayout>
6、huihua_1_item.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="left" android:orientation="horizontal" android:paddingBottom="10dip" android:paddingLeft="5dip" android:paddingRight="50dip" android:paddingTop="10dip" > <LinearLayout android:id="@+id/ll" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="left" android:orientation="vertical" android:gravity="center_vertical" android:background="@android:drawable/divider_horizontal_bright"> <com.example.SMS.CustomTextView android:id="@+id/messagedetail_row_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="top|left" android:padding="5dip" android:textColor="#000000" /> <TextView android:id="@+id/date" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:textAppearance="?android:attr/textAppearanceSmall" android:textColor="#65879e" /> </LinearLayout>"</LinearLayout>7、huihua_2_item.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="right" android:orientation="horizontal" android:paddingBottom="10dip" android:paddingLeft="50dip" android:paddingRight="5dip" android:paddingTop="10dip" > <LinearLayout android:id="@+id/ll" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:gravity="center_vertical" android:orientation="vertical" android:background="@android:drawable/divider_horizontal_bright"> android:background="@android:drawable/btn_default"> <com.example.SMS.CustomTextView android:id="@+id/messagedetail_row_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="top|left" android:padding="5dip" android:textColor="#000000" /> <TextView android:id="@+id/date" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:textAppearance="?android:attr/textAppearanceSmall" android:textColor="#65879e" /> </LinearLayout></LinearLayout>
8、适配器
package com.example.SMS;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.*;import com.example.R;import com.example.SMS.SMSAdapter.ViewHolder;import java.util.*;import android.content.Context;import android.graphics.Color;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.LinearLayout;import android.widget.TextView;public class HuiHuaAdapter extends BaseAdapter { private List<MessageBean> List;private Context context;private Date date;private LayoutInflater vi; private LinearLayout convert;private LinearLayout layout_child; private TextView idate; private CustomTextView text;public HuiHuaAdapter(Context mContext, List<MessageBean> List) {// TODO Auto-generated constructor stubcontext = mContext;this.List = List;vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn List!=null?List.size():0;}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn List.get(position);}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stub//ViewHolder holder;//if(convertView == null){//convertView = LayoutInflater.from(mContext).inflate(R.layout.huihua_1_item, null);//holder = new ViewHolder();//holder.date = (TextView)convertView.findViewById(R.id.text1);//holder.text = (CustomTextView)convertView.findViewById(R.id.messagedetail_row_text);//convertView.setTag(holder);//}else { // holder = (ViewHolder) convertView.getTag(); // } MessageBean m = List.get(position);//holder.date.setText(m.getDate());//holder.text.setText(m.getText());int itemLayout = m.getLayoutID();convert = new LinearLayout(context);vi.inflate(itemLayout,convert,true);convert.setBackgroundColor(Color.TRANSPARENT);layout_child = (LinearLayout) convert.findViewById(R.id.ll); text = (CustomTextView) convert.findViewById(R.id.messagedetail_row_text); text.setText(m.getText()); idate = (TextView)convert.findViewById(R.id.date);idate.setText(m.getDate());return convert;}}
0 0
- android通讯录之短信
- Android备份、还原通讯录和短信
- android通讯录:拨打电话和发短信
- 短信 通讯录备份APP(针对android)
- android 获取短信 通讯录 电话记录
- android通讯录之联系人
- android通讯录之通话记录
- Android中如何在QQ通讯录,360之前拦截短信
- 在QQ通讯录之前拦截短信(android)
- Android获取手机通讯录调用系统短信功能
- Android中如何在QQ通讯录,360之前拦截短信
- Android-通讯录:获取短信会话列表(包括群发的)
- android 中电话记录,通讯录与短信的查询
- Android手机获取通讯录联系人信息及短信广播实现
- Android -- Contentprovider---listview 实现调用通讯录和短信接收
- 无需越狱,Android通话记录、通讯录、短信同步到iphone6
- Android通讯录管理(获取联系人、通话记录、短信消息)
- Android 提取用户通讯录以及短信,兼容高低版本
- 神奇的栈帧
- 面向对象——this和super
- [Java] 对象的声明与实例化
- C++ 理解二维数组的指针
- Shell脚本编程之函数
- android通讯录之短信
- win10系统下搭建cocos2d-x v3.11.1版本开发环境
- Jzptab [Bzoj 2693]
- Linux修改用户所在组方法
- 设计模式-工厂
- 第十六周程序阅读(2)
- [Java]Session,Cookie知识与应用
- CSS布局属性
- HDU1561 The more, The Better(树形DP)