五月的第三天,这次我给大家带来的是一个关于短信弹窗提示的爱屁屁,效果如下:

    

而需要完成对系统接收短信的监听,相关全权限的获取以及监听系统广播是必须实现的两个过程。

关于权限一块,android系统关于短信的相关权限如下所示:

<!--  发送消息-->
<p><uses-permissionandroid:name="android.permission.SEND_SMS"/>
<!-- <wbr> 阅读消息-->
<uses-permissionandroid:name="android.permission.READ_SMS"/>
<!-- <wbr> 写入消息-->
<uses-permissionandroid:name="android.permission.WRITE_SMS" />
<!-- 接收消息 -->
<uses-permissionandroid:name="android.permission.RECEIVE_SMS"/></wbr></wbr></p>

而本次我们需要实现的是接收短信,所以需要打开相应的权限,其实还有一个短信回复的功能,该功能在这个Demo中我并没有实现,不过在指定部位我有代码备注,可以留给有兴趣的朋友添加相关代码进行实现,不过记得添加相关的权限。

其次是监听系统广播,广播接收需要添加Action,而系统接收短信广播发出的Action如下:

android.provider.Telephony.SMS_RECEIVED

这个Demo中我使用的是BroadcastReceiver的静态注册方式,在注册广播的时候我们给它加上这个Action即可。

完成以上两步,监控系统短信的基础条件就已经是满足了。完成了基础条件的配置,我们还需要的就是一个BroadcastReceiver用于接收系统广播并由它启动我们的窗口DialogActivity。BroadcastReceiver代码如下:

package com.mariostudio.broadcastreceiver;import java.text.SimpleDateFormat;
import java.util.Date;import com.mariostudio.messagewatcher.DialogActivity;import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;public class MessageReceiver extends BroadcastReceiver{private static String MESSAGE_RECEIVER = "android.provider.Telephony.SMS_RECEIVED";@SuppressLint("SimpleDateFormat") @Overridepublic void onReceive(Context context, Intent intent1) {if(intent1.getAction().equals(MESSAGE_RECEIVER)){Object pdus[] = (Object[]) intent1.getExtras().get("pdus");for(Object object:pdus){byte pdu[] = (byte[]) object;SmsMessage message = SmsMessage.createFromPdu(pdu);//获取短信的内容String msmContent = message.getMessageBody();//获取短信接收时间Date date = new Date(message.getTimestampMillis());SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");String msmDate = format.format(date);//获取发信人号码String msmFrom = message.getOriginatingAddress();Intent intent = new Intent();intent.setClass(context,DialogActivity.class);Bundle bundle = new Bundle();bundle.putString("content",msmContent);bundle.putString("date",msmDate);bundle.putString("from",msmFrom);intent.putExtra("Message",bundle);//使用context.startActivity()需要新开辟一个Taskintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//使用一下语句表示在此Receiver接收到收到短信后将不再讲出广播发送给其他接收器,用于屏蔽系统推送短信Notificationthis.abortBroadcast();//启动短信弹窗Activitycontext.startActivity(intent);}}}
}

然后就是静态注册这个BroadcastReceiver:

        <receiverandroid:name="com.mariostudio.broadcastreceiver.MessageReceiver"><intent-filter android:priority="1000"><action android:name="android.provider.Telephony.SMS_RECEIVED"/></intent-filter></receiver>

接下来,就是Activity的实现,既然是弹窗提示,那么Activity就不能填满整个屏幕出现咯,而是要以一个窗体的样式出现,为做到这一点我们在style中继承Theme.Dialog主题样式,并进行相应的属性修改已达到我们的需求:

    <style name="DialogActivityTheme" parent="android:style/Theme.Dialog"><item name="android:windowNoTitle">true</item><!--除去title标题栏--><item name="android:windowBackground">@android:color/transparent</item><!-- 窗体背景设为透明 --><item name="android:backgroundDimEnabled">false</item><!-- 窗体之后的背景模糊化设为false --></style>

我们再更改需要以窗口方式出现的Activity的Theme为我们自定义的DialogActivityTheme,再为Activity添加的相应的布局完成对弹出窗体的设置,窗体布局文件如下:

<?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="horizontal"><View android:layout_width="0.5dp"android:layout_height="200dp"/><LinearLayout android:layout_width="300dp"android:layout_height="240dp"android:orientation="vertical"android:background="@drawable/simple_windows"><View android:layout_width="300dp"android:layout_height="0.5dp"/><LinearLayout android:layout_width="300dp"android:layout_height="50dp"android:gravity="center"android:layout_marginTop="10dp"android:orientation="horizontal"><ImageView android:layout_width="30dp"android:layout_height="30dp"android:layout_marginLeft="15dp"android:src="@drawable/simple_icon_dialog"android:layout_gravity="left|center_vertical"/><LinearLayout android:gravity="center"android:layout_weight="1"android:layout_width="0dp"android:layout_height="50dp"android:orientation="vertical"><TextView android:id="@+id/TextView_From"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="20sp"android:text="10086"android:textColor="@color/windowsColor"/><TextView android:id="@+id/TextView_Date"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="12sp"android:text="2015-05-01 00:00:00"android:textColor="@color/windowsColor"/></LinearLayout><Button android:id="@+id/Button_Close"android:layout_width="30dp"android:layout_height="30dp"android:layout_marginRight="15dp"android:layout_gravity="right|center_vertical"android:background="@drawable/simple_button_close"/></LinearLayout><View android:layout_width="match_parent"android:layout_height="1dp"android:background="@color/windowsColor"/><ScrollView android:background="@drawable/simple_content"android:layout_width="match_parent"android:layout_marginRight="10dp"android:layout_marginLeft="10dp"android:layout_marginTop="10dp"android:layout_height="0dp"android:layout_weight="1"android:scrollbars="@null"><TextView android:id="@+id/TextView_Content"android:layout_width="match_parent"android:layout_height="wrap_content"android:padding="8dp"android:textSize="20sp"android:singleLine="false"android:text="这里是短信的正文内容"android:autoLink="phone|email"android:textColor="#CC000000"/></ScrollView><LinearLayout android:layout_width="match_parent"android:layout_marginRight="8dp"android:layout_marginLeft="8dp"android:layout_marginTop="10dp"android:layout_height="45dp"android:paddingBottom="10dp"android:gravity="center"android:orientation="horizontal"><EditText android:id="@+id/EditText_Content"android:layout_width="0dp"android:layout_weight="1"android:paddingLeft="5dp"android:paddingRight="5dp"android:paddingTop="2dp"android:paddingBottom="2dp"android:hint="请输入回复内容"android:background="@drawable/simple_edittext"android:layout_height="30dp"/><View android:layout_width="10dp"android:layout_height="match_parent"/><Button android:id="@+id/Button_Send"android:layout_width="45dp"android:layout_height="30dp"android:text="回复"android:background="@drawable/simple_button"/></LinearLayout></LinearLayout></LinearLayout>

再就是Activity代码的实现,时期获取并显示Receiver收到并发送给Activity的短信的相关信息:

package com.mariostudio.messagewatcher;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;public class DialogActivity extends Activity implements OnClickListener{private TextView textView_From,textView_Date,textView_Content;private Button button_Close,button_Send;private EditText editText_Content;private String MSM_Content,MSM_Date,MSM_From;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.activity_dialog);textView_Content=(TextView) findViewById(R.id.TextView_Content);textView_From=(TextView) findViewById(R.id.TextView_From);textView_Date=(TextView) findViewById(R.id.TextView_Date);button_Close=(Button) findViewById(R.id.Button_Close);button_Send=(Button) findViewById(R.id.Button_Send);button_Close.setOnClickListener(this);button_Send.setOnClickListener(this);Intent intent = getIntent();Bundle bundle = intent.getBundleExtra("Message");MSM_Content = bundle.getString("content");textView_Content.setText(MSM_Content);MSM_From = bundle.getString("from");//在这里可以写一个方法把发信人号码与本地电话薄对比查找联系人姓名if(MSM_From.equals("10086")){//打一个简单的比方,比如发信人是10086我们就可以将发信人姓名显示为“移动公司”MSM_From = "移动公司";     }textView_From.setText(MSM_From);MSM_Date = bundle.getString("date");textView_Date.setText(MSM_Date);}@Overridepublic void onClick(View view) {switch (view.getId()) {case R.id.Button_Close:finish();break;case R.id.Button_Send:String content = editText_Content.getText().toString();//editText_Content。getText()发送短信break;default:break;}}
}

通过以上几个步骤我们就能实现短信的弹窗显示啦,这个时候我们可以打开DDMS给虚拟机发送一条短信试验一下,会有意外的收获哦!

工程代码下载地址:http://download.csdn.net/download/mario_0824/8656101

Android-短信弹窗提示相关推荐

  1. android 手机短信恢复,Android短信如何恢复

    Android短信如何恢复?虽然当下微信使用非常普及,但不少重要事项还是会使用短信进行沟通的,比如快递密码箱ID提醒.信用卡还款提醒.验证码等.其目的是确保一定收到,不会因断网.未登陆等消息消失.所以 ...

  2. Android短信拦截机制适配的坑(下)--4.4以上系统,主要是6.0

    前一篇文章,Android短信拦截机制适配的坑(上)--4.4以下系统 介绍了广播接收的顺序,但是我明确说明在4.4以下系统,那么4.4及以上系统会遇到说明问题呢? 首先我们要来了解4.4系统短信的机 ...

  3. 未越狱iphone与 Android短信备份迁移攻略

    本攻略的原理简单,对小白们操作麻烦,对自己会越狱的同学,又是鸡肋.为了昨天熬夜到2点的辛苦,还是抽出时间写写.        转载自己搬,无需通知本人.        现在许多软件都能实现通讯录和短信 ...

  4. android5.1 rom互刷,红米Note移动版刷机包 乐蛙OS5.1通刷版 短信弹窗 整体美化 快速流畅稳定省电...

    卡刷方法 1.刷机前,请提前做好资料的备份(推荐钛备份.GO备份.或者中文Recovery自带的备份功能) 1.将下载好的ZIP格式卡刷包复制到SD卡/内置卡根目录: 2.手机完全关机状态,按组合键手 ...

  5. 三星S7短信不能提示的处理方法

    三星S7 edge(包括绝大多数的三星手机)的短信的提示方式有两种: 1.弹出窗口:显示在窗口的最顶上(不是任务栏): 2.浮动窗口:通过在窗口上显示一个图标,然后又以弹窗形式显示(在高级设置里面). ...

  6. Android短信的发送和广播接收者实现短信的监听

    Android短信的发送和广播接收者实现短信的监听  要注意Android清单中权限的设置以及广播的注册监听实现 以下就是 Android清单的XML AndroidManifest.xml < ...

  7. android短信功能裁剪,Android短信发送功能实现技巧分享

    如现在启动一模拟器id 号为5554,运行cmd telnet localhost 5554 输入help 可以看到很多用于模拟器中的功能命令 gsm call 134343434   // 便是呼叫 ...

  8. Android短信操作(通过内容提供者)

    2019独角兽企业重金招聘Python工程师标准>>> 1.Android短信数据库表结构 URI主要有: content://sms/               所有短信 con ...

  9. Android短信发送流程之多收件人发送(原)

    前面的< Android短信发送流程之长短信发送 >中介绍了长短信对于普通短信的区别,而对于多收件人的情况,在SmsMessageSender的queueMessage()方法中我们了解到 ...

最新文章

  1. 第三周-第08章节-Python3.5-文件修改详解
  2. html 多项选择,选项标签中的HTML多字段选择
  3. 求1-100之间的所有素数
  4. 在哪里能收到python实例代码-Python找出最小的K个数实例代码
  5. 【DONE】dbeaver不会用,请教!!!
  6. 腾讯与Github的魔幻会面背后的故事…
  7. java 把string转为keyevent_盘点现在Java强大第三方库(字符串操作),程序员都该知道!...
  8. JS正则表达式大全(整理详细且实用)
  9. Android系统(117)---Activity启动过程
  10. java实现icmp攻击,利用java实现ICMP协议在linux环境配置
  11. ROS学习(十七)安装ARDUINO IDE使用rosserial
  12. python分支结构基础实训_python-002基础——分支结构
  13. 通过python程序调取摄像头画面
  14. 网易评论真搞笑~~~呵呵
  15. no zuo no die _0_
  16. 论文解读-CenterNet:Keypoint Triplets for Object Detection
  17. fx5u模拟量如何读取_三菱FX5U PLC内置模拟量输入为电流怎么设置?
  18. word中如何编辑不同页眉的问题
  19. Firefox默认英文修改中文
  20. 数据分析——R语言中ggplot2用法(1)

热门文章

  1. 用 Python 发一个高逼格的朋友圈
  2. Linux【基础篇】—— linux操作系统目录结构、运行级别介绍
  3. PhxPaxos源码分析——网络
  4. c语言caesar密码编程,C语言程序设计#凯撒Caesar密码
  5. PMP计算题笔记(进度网络图、挣值分析、预测技术)
  6. pandas drop 方法
  7. windows 的发展历史(下)(了解一下,增加IT素养)
  8. Java中文处理学习笔记--Hello Unicode
  9. 首件检测在SMT贴片加工中有什么意义?
  10. 企业与企业之间保密协议