首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

发一条短信,找出你的手机-android平台

2012-09-19 
发一条短信,找到你的手机---android平台?The layout for this dialog, main.xml, is shown below:?In the

发一条短信,找到你的手机---android平台

?

The layout for this dialog, main.xml, is shown below:

?

    1. 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:

      ?

      1. 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:

        ?

        1. Now the OS will know what receiver to call for that action, so lets create the IntentReceiver called FinderReceiver:

          ?

          1. 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:

            ?

            发一条短信,找出你的手机-android平台

            ?

            And then after you send it you should see a notification like this:

            ?

            发一条短信,找出你的手机-android平台

            ?

            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 -->

热点排行