- 浏览: 351658 次
- 性别:
- 来自: 北京
最新评论
-
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 2172使用android自动化测试工具monkeyrunner启动 ... -
设置默认应用
2014-05-15 11:46 1721setDefaultLauncher(); ... -
Android之APK文件签名——keytool和jarsigner
2012-10-10 14:06 1854一、生成密钥库将位置定位在jdk的bin文件中,输入以下命名行 ... -
Android中应用程序如何获得系统签名权限
2012-08-21 14:18 0有些库的使用条件比较苛刻,要求同一签名的程序才可以获得访问权。 ... -
Android中应用程序如何获得系统签名权限
2012-08-21 14:18 0有些库的使用条件比较苛刻,要求同一签名的程序才可以获得访问权。 ... -
Android权限共享UID和签名(转)
2012-08-21 14:17 1757共享UID 安装在设备中的每一个Android包文件(.ap ... -
android小结
2012-04-17 10:21 16281.获取屏幕的分辨率 在 Activity 里使 ... -
TextView实现电话、网址链接
2012-03-22 17:39 1638TextView是android中一个组件,具有autolin ... -
Android Eclipse JNI 调用 .so文件加载
2012-03-01 18:41 5962http://blog.sina.com.cn/s/blo ... -
Gallery加小点效果实现
2012-02-15 16:15 1687大家好: 心雨心今天又跟大家见面了 今天发布的是Gal ... -
如何解决:Android中 Error generating final archive: Debug Certificate expired on 10/09
2011-12-21 13:39 1613问题概述: 在导入一个app后提示如下错误(出现该问题, ... -
Android Google Map API 开发基础知识
2011-12-16 17:25 1770开发基于谷歌地图的应用和普通的android应用差不多都要 ... -
使一段字符串显示不同的颜色
2011-12-12 15:09 2663一种:字体颜色改变 String appna ... -
droid系统中使用TelephonyManager类来获取imei号和其他手机信息
2011-11-22 11:15 14821在AndroidManifest.xml文件中增加& ... -
CategoryAdapter控件
2011-11-17 10:50 1545使用方法: private Cate ... -
(转)Android开发:在EditText中关闭软键盘
2011-11-04 13:45 151821、EditText有焦点(focusable为true)阻止 ... -
获取未安装的APK图标
2011-09-01 10:38 1435网上关于"获取未安装的APK图 ... -
android ListView的美化涉及到的一些属性
2011-08-26 10:44 2369用心的朋友应该会发现,listview中在设置了背景之后。会有 ... -
ScrollView中嵌入ListView办法
2011-08-11 17:13 2267①layout布局代码: <ScrollView an ... -
android 混淆 去除第三方jar
2011-06-10 19:05 12510编译与反编译,一对相辅相成的 ...
相关推荐
react-native-sms, 支持iOS和Android的短信发送方 react-native-sms用户定义的SendSMS使用这个 rn 组件发送带有回调 (completed/cancelled/error). iOS和Android的短信。目前,只支持用户发送短信。 this react-na
android-support-v4-v7-v13-v14-v17(官方最新完整版),官方最新版的,压缩包内包含android-support-v4、android-support-v7-appcompat,android-support-v7-cardview,android-support-v7-gridlayout,android-support-...
内容包含:android-support-v4_1.6.0_26_20120316.jar android-support-v4_1.6.0_26_20120623.jar android-support-v4_1.6.0_26_20120730.jar android-support-v4_1.6.0_26_20121109.jar android-support-v4_1.6.0_26...
Android项目实战--手机卫士19--短信的恢复
asmack-android-8-4.0.7jar 2015-02-20更新,现在asmack已经托管到github上,费了很长时间才找到,在这里分享给大家
Android项目实战--手机卫士18--读取用户的短信内容以及短信备份
ksoap2-android-assembly-2.6.5-jar-with-dependencies.jar 要是需要最新的,下载地址: http://code.google.com/p/ksoap2-android/
Android项目实战--手机卫士10--GPS定位与发送短信指令
Android获取手机短信的源代码,测试可用。
将android-17.zip解压到 Android\android-sdk\platforms 文件夹下。
基于android平台的手机软件开发-基于android平台的手机-星座-查询软件设计-本科论文.doc
课程表--android 手机开发 java 课程表--android 手机开发 java 课程表--android 手机开发 java 课程表--android 手机开发 java 课程表--android 手机开发 java 课程表--android 手机开发 java
安卓手机定时发送短信多条短信同时发布.zip
android-sdk\platforms\android-21
android-ndk-r16b-windows-x86_64包里toolchains目录下的mips64el-linux-android-4.9和mipsel-linux-android-4.9,可以解决NDK17版本以上编译出现的No toolchains found in the NDK
Twitter手机端安装包--Android,twitter-8-56-0-release-00.apk
Android手机后台发送短信,测试可用
Android x86 即运行于 x86 PC上的Android操作系统,目前已经支持大部分安卓程序。Android X86平台是由Beyounn和Cwhuang主持设计的。项目的主要目的在于为X86平台...资源来源:https://www.fosshub.com/Android-x86.html
将android-18解压到Android\android-sdk\platforms下。