Android-短信弹窗提示

来源:互联网 发布:运营商网络制式 编辑:程序博客网 时间:2024/05/16 09:09

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

              

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

关于权限一块,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:

        <receiver            android: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





0 0
原创粉丝点击