短信监控器

(1)新建继承类
(2)获取短信的信息的内容和发送者信息
(3)配置 , 以及 , 打开短信权限
二、---布局创建---
短信监控器的显示结果如下 , 当有短信到来的时候打印一个吐司

短信监控器

文章插图
三、---代码---
(1)如下是的代码
package com.iigt.smslistener;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.telephony.SmsMessage;import android.widget.Toast;public class SmsListenerReceiver extends BroadcastReceiver{@Overridepublic void onReceive(Context context, Intent intent) {// TODO Auto-generated method stubObject[] objects = (Object[]) intent.getExtras().get("pdus");for(Object pdu : objects){//获取smsMessage的实例SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdu);//获取短信的发送内容String messageBody = smsMessage.getMessageBody();//获取短信的发送者String originatingAddress = smsMessage.getOriginatingAddress();//吐司处理System.out.println("body:"+messageBody+"---"+originatingAddress);Toast.makeText(context, originatingAddress+"发送的内容为:"+messageBody, Toast.LENGTH_LONG).show();}}}
短信监控器

文章插图
(2)如下是配置文件的源代码

包括短信权限的打开和代码的执行
-------------------------------------------------------对话框--------------------------------------------------------------------------
一、---效果---
如下是普通选型框:
短信监控器

文章插图
下面这个是 单选框:
短信监控器

文章插图

短信监控器

文章插图
二、---代码---
package com.iigt.dialog;import android.os.Bundle;import android.app.Activity;import android.app.AlertDialog;import android.app.AlertDialog.Builder;import android.content.DialogInterface;import android.content.DialogInterface.OnClickListener;import android.view.Menu;import android.view.View;import android.widget.Toast;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}//点击普通对话框public void click1(View v){AlertDialog.Builder builder = new Builder(this);builder.setTitle("警告信息");builder.setMessage("我是普通对话框");builder.setPositiveButton("同意", new OnClickListener() {public void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubToast.makeText(getApplicationContext(), "这家伙居然同意了", 1).show();}});builder.setNegativeButton("否定", new OnClickListener() {public void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubToast.makeText(getApplicationContext(), "这家伙居然否定了", 1).show();}});// 最后一步一定要记得 show出来builder.show();}// 下面是单选的对话框public void click2(View v){AlertDialog.Builder builder = new Builder(this);builder.setTitle("这个是单选框");final String items[] = {"苹果", "鸭梨", "橘子", "西红柿", "香蕉"};builder.setSingleChoiceItems(items, -1, new OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub// 取出点中的条目String item = items[which];Toast.makeText(getApplicationContext(), item, 1).show();// 关闭当前对话框dialog.dismiss();}});// 最后一步一定要记得 show出来builder.show();} }
和上面的类似 , 如下是一个多选对话款和进度条对话框:
短信监控器

文章插图
如下是进度条对话框:
短信监控器

文章插图
以下是上面两个对话框的代码:
【短信监控器】 // 点击按钮 弹出一个多选对话框public void click3(View v) {// 构建AlertDialogAlertDialog.Builder builder = new Builder(this);builder.setTitle("请选择您喜欢吃的水果");final String items[] = { "榴莲", "苹果", "葡萄", "香蕉", "黄瓜", "火龙果", "荔枝" };final boolean[] checkedItems = { true, false, false, false, false,false, true };builder.setMultiChoiceItems(items, checkedItems,new OnMultiChoiceClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which,boolean isChecked) {}});builder.setPositiveButton("确定", new OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {StringBuffer sb = new StringBuffer();// [1]把你选中的水果给取出来for (int i = 0; i < checkedItems.length; i++) {if (checkedItems[i]) {// 就证明是选中的String fruit = items[i];sb.append(fruit + " ");}}Toast.makeText(getApplicationContext(), sb.toString(), 1).show();// 关闭对话框dialog.dismiss();}});// 最后一步一定要记得 show出来builder.show();}//进度条对话框public void click4(View v) {//与进度相关的控件都可以直接在子线程更新ui final ProgressDialog dialog = new ProgressDialog(this);dialog.setTitle("正在玩命加载ing");dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);dialog.show();//设置进度条的最大值 dialog.setMax(100);new Thread(){public void run() {for (int i = 0; i <= 100; i++) {//设置当前的进度 SystemClock.sleep(50);//睡眠50毫秒dialog.setProgress(i);}//关闭对话框dialog.dismiss();};}.start();}}