提示接收到信息的程序
2012-08-20 15:42
162 查看
上篇文章写了发送信息,所以接下来写一篇小小文章关于接收到信息,并且弹出一个小小的toast(toast是可以自定义的,如果你觉得系统的不好看,你可以自己定义一个,但是我为了方便就使用系统提供的了)
1、首界面的xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:textSize="20dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/myString"
android:gravity="center_horizontal"
/>
<Button
android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dip"
android:text="@string/ok"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
图示:
2、该界面的java代码:
package com.receive;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ReceiveMSNActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button ok = (Button) findViewById(R.id.ok);
ok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
System.exit(0); //退出该程序
}
});
}
}
4、开发一个广播类,用来监听收到信息的广播:
package com.receive;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
Bundle bundle = intent.getExtras();
if(bundle != null){ //当数据不为空时
Object[] myObject = (Object[]) bundle.get("pdus"); // 分析数据
SmsMessage[] messages = new SmsMessage[myObject.length];
for(int i=0; i<myObject.length; i++){
messages[i] = SmsMessage.createFromPdu((byte[]) myObject[i]);
}
StringBuilder sb = new StringBuilder();
for(SmsMessage tempSmsMessage : messages){ //循环信息数组
sb.append("收到来自:\n");
sb.append(tempSmsMessage.getDisplayOriginatingAddress() + "\n");
sb.append("内容为:\n");
sb.append(tempSmsMessage.getDisplayMessageBody());
}
Toast.makeText(context, sb.toString(), Toast.LENGTH_LONG).show();
}
}
}
}
5、效果如下图所示:
a、在5556模拟器上发送信息
b、在5554模拟器上接收信息:
6、该简单程序就到这先,如以后又改进再补充上去。
1、首界面的xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:textSize="20dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/myString"
android:gravity="center_horizontal"
/>
<Button
android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dip"
android:text="@string/ok"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
图示:
2、该界面的java代码:
package com.receive;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ReceiveMSNActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button ok = (Button) findViewById(R.id.ok);
ok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
System.exit(0); //退出该程序
}
});
}
}
4、开发一个广播类,用来监听收到信息的广播:
package com.receive;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
Bundle bundle = intent.getExtras();
if(bundle != null){ //当数据不为空时
Object[] myObject = (Object[]) bundle.get("pdus"); // 分析数据
SmsMessage[] messages = new SmsMessage[myObject.length];
for(int i=0; i<myObject.length; i++){
messages[i] = SmsMessage.createFromPdu((byte[]) myObject[i]);
}
StringBuilder sb = new StringBuilder();
for(SmsMessage tempSmsMessage : messages){ //循环信息数组
sb.append("收到来自:\n");
sb.append(tempSmsMessage.getDisplayOriginatingAddress() + "\n");
sb.append("内容为:\n");
sb.append(tempSmsMessage.getDisplayMessageBody());
}
Toast.makeText(context, sb.toString(), Toast.LENGTH_LONG).show();
}
}
}
}
5、效果如下图所示:
a、在5556模拟器上发送信息
b、在5554模拟器上接收信息:
6、该简单程序就到这先,如以后又改进再补充上去。
相关文章推荐
- MFC之对话框程序界面控件提示信息操作CToolTipCtrl
- 实现winfrom进度条及进度信息提示,winfrom程序假死处理
- 运行HBase应用开发程序产生异常,提示信息包含org.apache.hadoop.hbase.ipc.controller.ServerRpcControllerFactory的解决办法
- XP安装SQL Server,错误提示“安装程序无法获取ASPNET账户的系统账户信息 ”
- 小程序无网络状态时不刷新提示信息
- 锁屏状态下点亮屏幕,并弹出闹钟提示信息,可以在锁屏界面上取消闹钟;使用广播接收闹钟定时:
- 程序运行时提示输入一个整数并利用Scanner类接收数据,输出该数值是几位数。(要求:利用String 类中的方法)
- 升级早期ASP.NET的Beta版程序到Beta 2时的错误信息提示,兼容性考虑和版本转换
- 编写程序,查询所有学生选修课程的期末成绩和对应等级,如学生未选修任何课程 --则输出提示信息。用case选择结果
- CentOS提示"另一个目前运行的程序正在访问软件信息"
- 编写程序,用if else语句根据姓名查询teaching数据库中学生的基本信息和选课信息,学生姓名通过变量输入。不存在则输出提示信息
- 如何去掉某些程序的广告提示信息
- linux下基于tcp的简单的接收与发送程序,开发板与本机接收与发送信息
- 有五个学生,每个学生有3门课(语文、数学、英语)的成绩,写一个程序接收从键盘输入学生的信息,输入格式为:name,30,30,30(姓名,三门课成绩), 然后把输入的学生信息按总分从高到低的顺序写入
- CentOS提示"另一个目前运行的程序正在访问软件信息"
- 在javascript中使用提示信息来熟悉当前的程序流程
- Android Studio不能运行程序,提示信息:Error: Failed to resolve: com.android.support:appcompat-v7:27.+ <a hr
- 如何拷贝Wins下运行程序弹出错误提示框中的错误信息
- 2.4要不要加油?设计程序,用于赛车上的计算机监视系统。该程序在赛车油量偏低时(少于1/4),警示车手应该注意;在油箱接近满载(不低于3/4)时要告诉车手绕过维修站。在其他情况下,不提示任何信息,以免
- 编写程序实现比萨制作。需求说明编写程序,接收用户输入的信息,选择需要制作的比萨。可供选择的比萨有:培根比萨和海鲜比萨。