- 浏览: 355786 次
- 性别:
- 来自: 北京
最新评论
-
sxchao2008:
心雨心 写道你说的不好用是?你好,在静默卸载系统级别app的时 ...
静默卸载系统软件和第三方软件 -
zhao1111:
请问楼主哪里提供地震的xml文件啊
android解析xml文件 Android DOM解析XML之全球实时地震信息列表 -
yuanmouren1hao:
java字节流(读写文件) -
haiyangzhy:
很好。
ScrollView中嵌入ListView办法 -
gisdaniel:
[/url][url][fla ...
Android中的XML解析-DOM的使用与开发技巧
Introduction
In this tutorial we will create an application called PhoneFinder. This application will illustrate how to deal with sending and receiving SMS messages. The idea of the application is that when your phone is lost or stolen you will be able to use someone else's phone to retrieve the GPS coordinates at your phone's location to help you find it.
This application needs an Activity that will allow the user to enter in the password and an IntentReceiver that will be kicked off on incoming SMS messages.
Click here to download the complete source.
Password Entry
We will use the simple dialog shown below for password entry. Once the password is correctly entered we will save a MD5 sum of the password into the SharedPreferences for the package. The preferences is an easy way to save small amounts of persistent data. It is also only accessible by classes in your package. We will take the extra precaution of saving an MD5 of the password, this way if the data was read somehow it would not reveal the plain text password unless the password is very weak (aka in the dictionary).
The layout for this dialog, main.xml, is shown below:
-
<?xml version="1.0" encoding="utf-8"?>
-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
android:orientation="vertical"
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent"
-
>
-
-
<TextView
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:text="@string/password_label"
-
/>
-
<EditText android:id="@+id/password"
-
android:maxLines="1"
-
android:layout_marginTop="2dip"
-
android:layout_width="wrap_content"
-
android:ems="25"
-
android:layout_height="wrap_content"
-
android:autoText="true"
-
android:scrollHorizontally="true"
-
android:password="true" />
-
<TextView
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:text="@string/password_confirm_label"
-
/>
-
<EditText android:id="@+id/password_confirm"
-
android:maxLines="1"
-
android:layout_marginTop="2dip"
-
android:layout_width="wrap_content"
-
android:ems="25"
-
android:layout_height="wrap_content"
-
android:autoText="true"
-
android:scrollHorizontally="true"
-
android:password="true" />
-
-
<Button android:id="@+id/ok"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:layout_gravity="right"
-
android:text="@string/button_ok" />
-
-
<TextView android:id="@+id/text1"
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
/>
-
</LinearLayout>
As you can see it is a very simple layout, 2 text fields, 2 input fields, a button and another text field at the end to display messages to the user. The strings are defined in the strings.xml for better multilingual support.
The code for this activity is very simple. It's job is to make sure that the password is at least 6 characters, and that the 2 password fields match. Once that is confirmed then all we need to do is save the MD5 sum of the password the user entered into the SharedPreferences.
Here is the PhoneFinder Activity:
-
public class PhoneFinder extends Activity {
-
-
-
private TextView messages;
-
private EditText pass1;
-
private EditText pass2;
-
-
@Override
-
public void onCreate(Bundle icicle) {
-
super.onCreate(icicle);
-
setContentView(R.layout.main);
-
-
messages = (TextView) findViewById(R.id.text1);
-
pass1 = (EditText) findViewById(R.id.password);
-
pass2 = (EditText) findViewById(R.id.password_confirm);
-
-
button.setOnClickListener(clickListener);
-
}
-
-
private OnClickListener clickListener = new OnClickListener() {
-
-
-
if (p1.equals(p2)) {
-
-
if (p1.length() >= 6 || p2.length() >= 6) {
-
-
Editor passwdfile = getSharedPreferences(PhoneFinder.PASSWORD_PREF_KEY, 0).edit();
-
passwdfile.putString(PhoneFinder.PASSWORD_PREF_KEY,
-
md5hash);
-
passwdfile.commit();
-
messages.setText("Password updated!");
-
-
} else
-
messages.setText("Passwords must be at least 6 characters");
-
-
} else {
-
pass1.setText("");
-
pass2.setText("");
-
messages.setText("Passwords do not match");
-
}
-
-
}
-
-
};
-
}
In onCreate() we initialize the various Views that we are using in the layout and then we setup the OnClickListener object for the "ok" button. When the "ok" button is pressed we are taken down into the onClick() function that starts on line 40.
In the onClick() function we confirm that the requirements for password length is met, and we make sure that both of the text boxes match. If that all happens then we get to line 48 where we setup the SharedPreferences.Editor class. This class allows us to edit the shared preferences for this application. It is called "shared" because it is application wide preferences, there are also Activty level preferences available via Activity.getPreferences(int).
Writing to the preferences is easy once you have the Editor object. You just use one of the putX() functions to add key/value pairs and then call the commit() function to save the results.
Then on line 48 you'll see that we call the member function getMd5Hash(String) which returns the MD5 sum as a string, and we store that in the preferences. Here is the getMd5Hash(String) function:
-
try {
-
byte[] messageDigest = md.digest(input.getBytes());
-
-
while (md5.length() < 32)
-
md5 = "0" + md5;
-
-
return md5;
-
Log.e("MD5", e.getMessage());
-
return null;
-
}
-
}
We use a android.security.MessageDigest object passing in "MD5" as the algorithm we want to use. The digest() function is called passing in a Byte array from the String passed in, and it returns a byte array. This byte array can then be saved as a BigInteger and then converted to a hex string using toString(16). When we convert this byte array to a BigInteger leading zeros will be trimmed, so we add leading zeros with the while loop until the MD5 sum reaches 32 characters.
On the next page we will create the IntentReciever that will listen for SMS messages and respond to a relavent SMS message...
<!--pagebreak--><!--pagebreak-->Handling the SMS Message
Now that the user can save the password to the preferences we will need to check all incoming SMS messages and respond to any relavent ones. We are looking for a message in the format:SMSLOCATE:<password>
So if a SMS messages starts with "SMSLOCATE:" and the MD5 sum of the password after the ":" matches that of the one saved earlier then we will send a text message with everything we know about the phones current location. To do this we need to setup an IntentReceiver that will respond to the "android.provider.Telephony.SMS_RECEIVED" action, this will take some additional lines in the AndroidManifest.xml file. Here is the AndroidManifest.xml file:
-
<?xml version="1.0" encoding="utf-8"?>
-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
-
package="com.helloandroid.android.phonefinder">
-
<uses-permission android:name="android.permission.RECEIVE_SMS" />
-
<uses-permission android:name="android.permission.ACCESS_GPS" />
-
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
-
<application android:icon="@drawable/icon">
-
<activity android:name=".PhoneFinder" android:label="@string/app_name">
-
<intent-filter>
-
<action android:name="android.intent.action.MAIN" />
-
<category android:name="android.intent.category.LAUNCHER" />
-
</intent-filter>
-
</activity>
-
-
<receiver android:name=".FinderReceiver">
-
<intent-filter>
-
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
-
</intent-filter>
-
</receiver>
-
-
</application>
-
</manifest>
You'll see starting on line 4 that our application must request permission to receive SMS messages, Access the GPS device, and Access the phone's location. This is requested with the <uses-permission> tag. Then down on line 15 we must specify our receiver as "FinderReceiver" and also the intent-filter that will be checked against all intents that are broadcasted. You can see here we are only concerned with IntentBroadcasts with the action "android.provider.Telephony.SMS_RECEIVED".
Now the OS will know what receiver to call for that action, so lets create the IntentReceiver called FinderReceiver:
-
public class FinderReceiver extends IntentReceiver {
-
-
@Override
-
-
SharedPreferences passwdfile = context.getSharedPreferences(
-
PhoneFinder.PASSWORD_PREF_KEY, 0);
-
-
null);
-
-
if (correctMd5 != null) {
-
-
SmsMessage[] messages = Telephony.Sms.Intents
-
.getMessagesFromIntent(intent);
-
-
for (SmsMessage msg : messages) {
-
if (msg.getMessageBody().contains("SMSLOCATE:")) {
-
if (tokens.length >= 2) {
-
-
if (md5hash.equals(correctMd5)) {
-
LocationManager lm =
-
-
SmsManager sm = SmsManager.getDefault();
-
-
sm.sendTextMessage(to, null, lm.getCurrentLocation("gps").toString(),
-
null, null, null);
-
-
Toast.makeText(context, context.getResources().getString(R.string.notify_text) + to,
-
Toast.LENGTH_SHORT).show();
-
}
-
}
-
}
-
}
-
}
-
}
-
}
We start by getting the correct MD5 sum for the saved password from the SharedPreferences, we do this on lines 18-21. If there is actually a password in there then we now want to loop through all of the SMS messages that were received.
We use Telphony.Sms.Intents.getMessageFromInent(intent) to get an array of SmsMessages. We will now loop through this array and see if the body of the message has "SMSLOCATE:" in it. If a message does, we need to get the password after the ":", take it's MD5 sum and compare it to the one we are looking for.
If the passwords match, then we get into the block starting on line 36. Now all we need is a String with the address to send it to, and a String with the location information. To get the location information we create a new LocationManager and simply use getCurrentLocation("gps").toString(). This will print out everything known about the location using gps as the location provider. We then send the text message using an SmsManager object. We also show a notification (Toast) saying that the message was sent.
Note: It might be a good idea to have an option in the password entry dialog to either show or don't show the notification. If the phone was stolen it would be better to hide the notification or the theif may realize that he's being tracked and will turn off the phone, etc.
Testing this operation
Now, everything is setup. With the new version of the SDK it is very easy to send in phone calls or text messages to the emulator. It is all done using the "Emulator Control" view in Eclipse. You can add this view by going to "Window -> Show View -> Other" and then selecting the "Emulator Control" in the Android section.
So to test first you need to launch the main activity and setup a password. For this example I've entered "123456" for the password. Now you can send a text message with "SMSLOCATE:123456" as the body of the text as shown below:
And then after you send it you should see a notification like this:
There you have it! I think this is a great example of how easy it is to develop useful applications for Android. Two basic objects to handle a very useful task.
<!-- google_ad_section_end -->发表评论
-
查看Android应用包名package和入口activity名称
2014-08-05 16:20 2237使用android自动化测试工具monkeyrunner启动 ... -
设置默认应用
2014-05-15 11:46 1769setDefaultLauncher(); ... -
Android之APK文件签名——keytool和jarsigner
2012-10-10 14:06 1924一、生成密钥库将位置定位在jdk的bin文件中,输入以下命名行 ... -
Android中应用程序如何获得系统签名权限
2012-08-21 14:18 0有些库的使用条件比较苛刻,要求同一签名的程序才可以获得访问权。 ... -
Android中应用程序如何获得系统签名权限
2012-08-21 14:18 0有些库的使用条件比较苛刻,要求同一签名的程序才可以获得访问权。 ... -
Android权限共享UID和签名(转)
2012-08-21 14:17 1815共享UID 安装在设备中的每一个Android包文件(.ap ... -
android小结
2012-04-17 10:21 16811.获取屏幕的分辨率 在 Activity 里使 ... -
TextView实现电话、网址链接
2012-03-22 17:39 1710TextView是android中一个组件,具有autolin ... -
Android Eclipse JNI 调用 .so文件加载
2012-03-01 18:41 6021http://blog.sina.com.cn/s/blo ... -
Gallery加小点效果实现
2012-02-15 16:15 1744大家好: 心雨心今天又跟大家见面了 今天发布的是Gal ... -
如何解决:Android中 Error generating final archive: Debug Certificate expired on 10/09
2011-12-21 13:39 1680问题概述: 在导入一个app后提示如下错误(出现该问题, ... -
Android Google Map API 开发基础知识
2011-12-16 17:25 1851开发基于谷歌地图的应用和普通的android应用差不多都要 ... -
使一段字符串显示不同的颜色
2011-12-12 15:09 2739一种:字体颜色改变 String appna ... -
droid系统中使用TelephonyManager类来获取imei号和其他手机信息
2011-11-22 11:15 14896在AndroidManifest.xml文件中增加& ... -
CategoryAdapter控件
2011-11-17 10:50 1627使用方法: private Cate ... -
(转)Android开发:在EditText中关闭软键盘
2011-11-04 13:45 152221、EditText有焦点(focusable为true)阻止 ... -
获取未安装的APK图标
2011-09-01 10:38 1511网上关于"获取未安装的APK图 ... -
android ListView的美化涉及到的一些属性
2011-08-26 10:44 2412用心的朋友应该会发现,listview中在设置了背景之后。会有 ... -
ScrollView中嵌入ListView办法
2011-08-11 17:13 2319①layout布局代码: <ScrollView an ... -
android 混淆 去除第三方jar
2011-06-10 19:05 12574编译与反编译,一对相辅相成的 ...
相关推荐
当你发送一条短信时,你的设备会将消息传递到这个特定的SMSC,然后SMSC负责将消息路由到正确的目的地。在接收到短信时,SMSC也会将信息转发到你的手机。了解如何在Android应用中获取短信中心号码是开发与短信服务...
通常,长按一条短信后,屏幕顶部会出现“多选”选项,勾选需要删除的短信,最后点击“删除”。 2. **设置自动删除**:进入手机的“设置” -> “应用管理”或“应用信息” -> 选择“信息”应用 -> “存储”选项,部分...
- 进入手机的“短信”应用,打开一条短信,然后点击“MENU”键,选择“通知设置”或“设置”选项。 - 在这里,找到“通知”设置,选择你希望应用于短信的铃声,确保是从“notifications”文件夹中选择的。 - 开启...
本文将深入探讨如何在Android项目中实现短信验证码的自动填充功能,通过分析给出的源码,我们可以了解到这个过程的关键技术点。 首先,我们需要理解短信验证码的生命周期。通常,验证码是由服务器发送到用户的手机...
String message = "这是一条测试短信。"; smsManager.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI); ``` 这里的`sentPI`和`deliveredPI`是`PendingIntent`对象,用于监听短信发送和送达的...
3. **SQLite数据库**:Android手机上的短信存储在SQLite数据库中,每个短信对应一条记录。开发者需要使用SQLiteOpenHelper类来与数据库进行交互,读取短信内容并准备备份。 4. **JSON序列化**:为了将短信数据发送...
当用户从苹果(iOS)系统转向安卓(Android)系统时,如何将iPhone中的短信迁移至新购入的安卓手机,例如从iPhone 11过渡到华为Mate 50 Pro,便成为了一个实际问题。本文将详细解析这一过程,并分享亲测有效的解决...
对于忙碌的用户来说,有时可能无法及时查看并回复每一条短信,这就引出了一个需求——自动回复短信。本资料“电信设备-手机自动回复短信的方法”将详细讲解如何在不同的电信设备上设置手机自动回复短信的功能,帮助...
在`onReceive()`方法中,我们需要从`bundle`中获取短信的`pdus`,然后遍历每条短信,解析其内容。通常验证码会包含在短信的正文中,我们可以通过正则表达式匹配验证码。 ```java for (Object pdu : (Object[]) ...
在Android平台上,备份和还原手机...总的来说,"Android myduanxin.zip"提供了一种实用的手段来管理和保护Android手机上的短信,同时也为开发者提供了一个学习和研究的实例,有助于提升他们在Android平台上的开发技能。
在`android-auto-messaging-demo-master`这个压缩包文件中,我们可以期待找到以下关键组件和文件: 1. `AndroidManifest.xml`: 这是项目的配置文件,声明了应用的基本信息,如应用名称、权限需求(如访问网络、读写...
- **6.1 您有一条短信popup提醒** 实现短信到达时的弹窗提醒功能,展示如何监听短信接收事件。 - **6.2 手机电池计量还剩多少** 介绍如何获取手机电池的状态信息,包括剩余电量等。 - **6.3 群发拜年短信给联系...
- 默认情况下,只会转发最近的一条短信,但可以通过设置选择是否转发整个会话。 - **1.1.10 如何设置短信通知铃声与振动选项** - 进入消息设置,找到“通知”选项,可以选择不同的铃声或振动模式。 - **1.1.11 ...
在Android操作系统中,音量控制是一项关键功能,它允许用户根据个人喜好和环境需求调整不同场景下的音量级别。在Android设备上,音量控制主要分为五个不同的类别:系统音量、通话音量、铃声音量、媒体音量以及提示...
3. **聊天式对话**:界面采用了一对一的聊天形式,每条短信都以气泡的形式显示,使对话更直观,阅读更方便。 4. **联系人管理**:插件集成了联系人列表,可以快速查找并选择收信人,操作流程与iOS类似,简化了用户...
- 一条最长的短信息约占160个字符(即约140字节),超出部分会被分割成多条短信。 #### 34. 修改图片像素值的算法 - 对于给定的一维整型数组,可以通过遍历数组,检查每个像素值是否为纯白色不透明(0xffffffff),...
当用户在手机上编写一条短信时,信息会被编码为7位或16位的字符集,如GSM 7位编码,然后通过网络传输到目标设备。 “delete_sms”这个标签可能指的是短信应用中的删除功能,这是用户管理自己短信收件箱的一个常见...
通过优质的短信通道,每秒可以处理200条短信,这对于需要快速响应的场景(如注册、登录、支付验证)至关重要。 5. **上行功能**:除了能够发送验证码,卡洛思的接口还支持上行功能,即用户可以通过短信回应服务,这...
根据提供的文档内容,我们可以归纳出一系列关于Android 4.0终端操作的知识点,涉及系统设置、应用程序使用等多个方面。下面将对每个操作进行详细的解析。 ### 1. 时间与日期设置 - **24小时制与时区设置**: - ...