我的手机管家骚扰拦截 逻辑代码
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我的手机管家骚扰拦截 逻辑代码相关的知识,希望对你有一定的参考价值。
骚扰拦截:
加载数据库中的数据到ListViw;
加载是一个耗时的操作, 在加载完了通知listview加载数据,
(1)这就用到了线程和Handler结合
/* * 加载数据库中的黑名单 */ public void initData() { progressBar.setVisibility(View.VISIBLE); isLoading = true;// 表示正在加载数据 new Thread(new Runnable() { @Override public void run() { if(list == null){//第一次加载。 list = BlackNumberDao.getInstance(getApplicationContext()).findPart(index); }else { list = BlackNumberDao.getInstance(getApplicationContext()).findPart(index); } myHandler.sendEmptyMessage(0); } }).start(); } private Handler myHandler = new Handler(){ public void handleMessage(Message msg) { if(adapter == null){ adapter = new BlackNumberAdapter(getApplicationContext(), list); lv.setAdapter(adapter); lv.setOnItemClickListener(listener); } adapter.notifyDataSetChanged();//加载刷新数据 progressBar.setVisibility(View.GONE); index = list.size(); isLoading = false;//不在刷新数据 }; };
(2)添加黑名单:
/** * 添加拦截信息, 弹出一个对话框, 添加拦截信息 * @param v */ public void onAdd(View v){ showDialog(); } /** * 显示对话框 */ public void showDialog(){//自定义提示框 Builder builder = new Builder(this); final AlertDialog dialog = builder.create(); View view = View.inflate(this, R.layout.dialog_add_black_number, null); dialog.setView(view, 0, 0, 0, 0);// 去掉上下左右边距 Button btnOK = (Button) view.findViewById(R.id.btn_ok); Button btnCancel = (Button) view.findViewById(R.id.btn_cancel); final EditText etNumber = (EditText) view.findViewById(R.id.et_number); final RadioGroup rgMode = (RadioGroup) view.findViewById(R.id.rg_mode); btnOK.setOnClickListener(new OnClickListener() { public void onClick(View v) { String number = etNumber.getText().toString().trim(); if (TextUtils.isEmpty(number)) { Toast.makeText(BlackNumberActivity.this, "输入内容不能为空!", Toast.LENGTH_SHORT).show(); return; } int mode = 1; int id = rgMode.getCheckedRadioButtonId();// 获取当前选中的radiobutton的id switch (id) { case R.id.rb_phone: mode = 1; break; case R.id.rb_sms: mode = 2; break; case R.id.rb_all: mode = 3; break; } //保存到数据库中 BlackNumberDao.getInstance(BlackNumberActivity.this).add(number, mode); dialog.dismiss(); //显示到列表的第一个位置 BlackNumberInfo info = new BlackNumberInfo(number, mode); list.add(0,info); adapter.notifyDataSetChanged(); } }); btnCancel.setOnClickListener(new OnClickListener() { public void onClick(View v) { dialog.dismiss(); } }); dialog.show(); } }
(3)响应listView的点击事件
/** * ListView的item点击事件监听 */ private OnItemClickListener listener = new OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { BlackNumberInfo info = list.get(position); //删除黑名单 BlackNumberDao.getInstance(BlackNumberActivity.this).delete(info.number); list.remove(position); adapter.notifyDataSetChanged(); } };
package com.chb.myphonesave.activity; import java.util.List; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.text.TextUtils; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.ProgressBar; import android.widget.RadioGroup; import android.widget.Toast; import com.chb.myphonesave.R; import com.chb.myphonesave.adapter.BlackNumberAdapter; import com.chb.myphonesave.dao.BlackNumberDao; import com.chb.myphonesave.entity.BlackNumberInfo; public class BlackNumberActivity extends Activity { private ListView lv; private ProgressBar progressBar; private List<BlackNumberInfo> list; private int index;//加载黑名单条数 private boolean isLoading;// 是否正在加载 private BlackNumberAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_blacknumber); lv = (ListView) findViewById(R.id.abn_lv); progressBar = (ProgressBar) findViewById(R.id.abn_progressbar); initData(); } /* * 加载数据库中的黑名单 */ public void initData() { progressBar.setVisibility(View.VISIBLE); isLoading = true;// 表示正在加载数据 new Thread(new Runnable() { @Override public void run() { if(list == null){//第一次加载。 list = BlackNumberDao.getInstance(getApplicationContext()).findPart(index); }else { list = BlackNumberDao.getInstance(getApplicationContext()).findPart(index); } myHandler.sendEmptyMessage(0); } }).start(); } private Handler myHandler = new Handler(){ public void handleMessage(Message msg) { if(adapter == null){ adapter = new BlackNumberAdapter(getApplicationContext(), list); lv.setAdapter(adapter); lv.setOnItemClickListener(listener); } adapter.notifyDataSetChanged();//加载刷新数据 progressBar.setVisibility(View.GONE); index = list.size(); isLoading = false;//不在刷新数据 }; }; /** * ListView的item点击事件监听 */ private OnItemClickListener listener = new OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { BlackNumberInfo info = list.get(position); //删除黑名单 BlackNumberDao.getInstance(BlackNumberActivity.this).delete(info.number); list.remove(position); adapter.notifyDataSetChanged(); } }; /** * 添加拦截信息, 弹出一个对话框, 添加拦截信息 * @param v */ public void onAdd(View v){ showDialog(); } /** * 显示对话框 */ public void showDialog(){ Builder builder = new Builder(this); final AlertDialog dialog = builder.create(); View view = View.inflate(this, R.layout.dialog_add_black_number, null); dialog.setView(view, 0, 0, 0, 0);// 去掉上下左右边距 Button btnOK = (Button) view.findViewById(R.id.btn_ok); Button btnCancel = (Button) view.findViewById(R.id.btn_cancel); final EditText etNumber = (EditText) view.findViewById(R.id.et_number); final RadioGroup rgMode = (RadioGroup) view.findViewById(R.id.rg_mode); btnOK.setOnClickListener(new OnClickListener() { public void onClick(View v) { String number = etNumber.getText().toString().trim(); if (TextUtils.isEmpty(number)) { Toast.makeText(BlackNumberActivity.this, "输入内容不能为空!", Toast.LENGTH_SHORT).show(); return; } int mode = 1; int id = rgMode.getCheckedRadioButtonId();// 获取当前选中的radiobutton的id switch (id) { case R.id.rb_phone: mode = 1; break; case R.id.rb_sms: mode = 2; break; case R.id.rb_all: mode = 3; break; } //保存到数据库中 BlackNumberDao.getInstance(BlackNumberActivity.this).add(number, mode); dialog.dismiss(); //显示到列表的第一个位置 BlackNumberInfo info = new BlackNumberInfo(number, mode); list.add(0,info); adapter.notifyDataSetChanged(); } }); btnCancel.setOnClickListener(new OnClickListener() { public void onClick(View v) { dialog.dismiss(); } }); dialog.show(); } }
以上是关于我的手机管家骚扰拦截 逻辑代码的主要内容,如果未能解决你的问题,请参考以下文章