短信拦截
这次写的软件是拦截垃圾短信的程序,使用了广播,自定义适配器和listview控件,其实还是挺好实现的,那我就直接把代码共享出来,
MainActivity代码:
package com.example.carr.smsspammanager; import Android.content.BroadcastReceiver; import android.content.context; import android.content.intent; import android.content.IntentFilter; import android.os.Bundle; import android.os.environment; import android.support.v7.APP.AppCompatActivity; import android.telephony.Smsmessage; import android.widget.ListView; import android.widget.toast; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity{ private ListView lv; //private File sdRoot; private String body; private String address; private BATteryBroadcastReceiver batteryBroadcastReceiver; private LvAdapter adapter; private List<LvBean> lvBeans; @Override protected void onCreate(Bundle savedinstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_main); lv = findViewById(R.id.lv); //判断SD卡是否挂载 if (!Environment.getExternalStorageState().equals(Environment.MEDIA_mountED)){ toast.maketext(MainActivity.this,"SD卡没有挂载成功,数据将会保存在根目录的Data文件夹",Toast.LENGTH_SHORT).show(); } //sdRoot = Environment.getDataDirectory(); //Toast.makeText(MainActivity.this,"SD卡数据目录"+sdRoot.toString(),Toast.LENGTH_SHORT).show(); loadData(); } public void loadData(){ lvBeans = new ArrayList<LvBean>(); LvBean currLvBean = new LvBean(); currLvBean.setPhone("手机号码"); currLvBean.setbody("拦截的内容"); lvBeans.add(currLvBean); adapter = new LvAdapter(this, lvBeans); lv.setAdapter(adapter); } @Override protected void onResume() { IntentFilter filter = new IntentFilter(); filter.addAction("android.provider.Telephony.SMS_RECEIVED"); batteryBroadcastReceiver = new BatteryBroadcastReceiver(); registerreceiver(batteryBroadcastReceiver,filter); super.onResume(); } @Override protected void onpause() { unregisterReceiver(batteryBroadcastReceiver); super.onPause(); } public class BatteryBroadcastReceiver extends BroadcastReceiver { @Override public void onreceive(Context context, Intent intent) { //int i = 1; Bundle bun = intent.getExtras(); Object[] objs = (Object[]) bun.get("pdus"); for (Object obj : objs) { byte[] pdu = (byte[]) obj; SmsMessage msg = SmsMessage.createFromPdu(pdu); body = msg.getMessageBody(); //获取发件人 address = msg.getOriginatingAddress(); Toast.makeText(MainActivity.this,"拦截短信:"+address,Toast.LENGTH_SHORT).show(); //如果发件人是15555215554那么就终止广播 if ("15555215556".equals(address)) { abortBroadcast(); } //i++; LvBean mLvBeans = new LvBean(); mLvBeans.setPhone(address); mLvBeans.setBody(body); //mLvBeans.setId(i); lvBeans.add(mLvBeans); adapter.notifydatasetChanged(); } } } }
LvBean类:
package com.example.carr.smsspammanager; import java.io.File; /** * Created by carr on 2018/1/13. */ public class LvBean { private String phone; private String body; private File file; public File getFile() { return file; } public void setFile(File file) { this.file = file; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getBody() { return body; } public void setBody(String body) { this.body = body; } }LvAdapter自定义适配器:
package com.example.carr.smsspammanager; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import java.util.List; /** * Created by carr on 2018/1/13. */ public class LvAdapter extends BaseAdapter { private List<LvBean> mLvList; private LayoutInflater inflater; //第一次查找控件的时候把找到的控件加入viewhold中 ViewHold iv=null; public LvAdapter(Context con,List<LvBean> LvBeanList){ mLvList=LvBeanList; inflater=LayoutInflater.from(con); } @Override public int getCount() { return mLvList.size(); } @Override public LvBean getItem(int i) { return mLvList.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { if (view==null){ view=inflater.inflate(R.layout.lv_view,null); iv=new ViewHold(); iv.tv_phone = view.findViewById(R.id.tv_phone); iv.tv_body = view.findViewById(R.id.tv_body); view.setTag(iv); }else{ iv= (ViewHold) view.getTag(); } LvBean lvBean = getItem(i); iv.tv_phone.setText(lvBean.getPhone()); iv.tv_body.setText(lvBean.getBody()); return view; } static class ViewHold{ TextView tv_phone; TextView tv_body; } }
大家可以好好的看下代码,也是挺好理解的,实在有不懂的地方也可以私信我。
其实是真的不愿意写注释。
相关阅读
七夕促销短信内容,最新七夕促销短信大全如下,七夕节马上就要来临了哦,各位卖家们,您可以趁此机会,向您的店铺粉丝们发送七夕促销短
Android7.0中文文档(API) -- HorizontalScrollView
完整内容,请查看:http://www.zhdoc.net/android/reference/android/widget/HorizontalScrollView.html HorizontalScrollView publi
public static void main(String[] args)throws IOException,Exception { for(int j=0;j<10;j++) { Thread.current
节假日一直就是短信平台使用最频繁的时间,大家都希望能够把握节假日的消费高峰时段,进行推广和宣传,在节日里给客户送出祝福,或者节日
随着互联网行业的发展,信息化时代的到来。移动智能手机变得越发普及,如何在手机上营销成为许多商家的难题。而短信是一个成本较低的