The addresses of the other four articles on mobile piling for big people (proofread and add): 1. Introduction to Android (2) Just swipe and encode the gesture in Android; (III) Activities and icons in the Android application life cycle; (IV) Overheard Word words and gestures. Add a multiple-choice quiz to your Android mobile app and sign it with a secure digital certificate With web logic, content is king. But with mobile users, interactive rules are king. Static information design for mobile apps is decreasing, and gamification is increasing. This month Andrew Glover decided to introduce Android mobile development by adding a multi-choice quiz feature to a sample app (Overheard Word, introduced in the previous article). After that he will show you how to generate a digital certificate and how to publish and promote your signed app on Google Play or Amazon Appstore. So far in this Mobil for the masses series, we've used Android as an example for learning how to do mobile development, including tutorials on the Android application lifecycle, implementing swipe gestures in your Android apps, and working with third-party libraries to simplify development and enhance app functionality. While I'm not sure about doing Android, I'm interested in exploring other mobile environments and technologies. So this month we're wrapping up our Android-intensive articles by adding a quiz feature to the Overheard Word sample app and preparing to deploy it to two popular Adroid app stores: Google Play and the Amazon Appstore. All of this will be the foundation for the next section: HTML5 invades mobile development! Gamify my app Before we signed Overheard Word and pushed it to the Android marketplace on Google Play and Amazon Appstore to compete with millions of other apps, I wanted to make sure it was the best Overheard Word app (unfamiliar with our Overheard Word app? Review the article that introduced this example). As you know, games are the current driving force of the mobile ecosystem, and a series of apps are expected to have efficient interactions. Mobile apps that ignite curiosity and the desire to win do a great job even when their goal is to provide information value. That's why Overheard Word is not just a list of words on a page; instead, it is designed to incite readers to dig into the vocabulary, and then reward them for sticking with it! (By the way, Gamification is a term for a design technique that is starting to become popular) Overheard Word Inquiry We will start by defining a new layout to make the Overheard Word test view consistent, and then we will define an Activity to display the layout. As mentioned earlier, I am using Eclipse's ADT as my development environment, and I assume you are using it too. We will start by defining a new layout to make the Overheard Word test view consistent, and then we will define an Activity to display the layout. As mentioned earlier, I am using Eclipse's ADT as my development environment, and I assume you are using it too. Figure 1. Creating a new layout in Eclipse We will start by defining a new layout to make the Overheard Word test view consistent, and then we will define an Activity to display the layout. As mentioned earlier, I am using Eclipse's ADT as my development environment, and I assume you are using it too. Next, copy the following XML into your new file. Listing 1. Quiz layout for Overheard Word
Overheard Word's existing layout file defines the UI for the study guide. This layout defines a series of sample text UI elements, so that you can get an idea of what things will look like when your app goes live. The layout for this test includes a TextView that holds the detailed definition and a RadioGroup for the various words that fit that definition. When the user selects a word, submits a new question or gets a chance to try again, the app will immediately notify them with an event and respond accordingly. There are also counters to track the user's progress through this small test. Our next step is to create a new Activity class. This class should extend the Activity class and provide an onCreate method, as shown in Listing 2. (Note that setContentView specifies the new layout file created in Listing 1) Listing 2. New Activity class: OverheardQuiz
Our next step is to instantiate a new Activity, which will ultimately display the Overheard word test layout. Rather than just creating a selectable menu item, let's try out the swipe gesture to launch a test. We'll also add the app menu to the test later. Programming Intents As we discussed earlier, an Android device is extremely constrained by battery life, execution power, and memory. Most personal devices also have many apps for features like calling and texting, so they have multiple active applications running at the same time. The Android platform manages this by giving you, as a developer, certain constraints on how your app should be run at any given time. For example, you cannot force an Activity class to start and display its view. Instead, you start the activity by sending it an intent to the platform. The platform will start the activity when it is able to do so. Think of an Intent as a mechanism for performing tasks asynchronously. From the user's perspective, you're suggesting that the platform should do something and it does it immediately when it can. So instead of just launching our Activity, we need to wrap it in an Intent. Android will then launch the Activity for us. Since we are launching a new test Activity with a gesture, we will plug the intent into a GestureDetector instance. (For more on GestureDetector in this series, see the first two articles.) Instead of just returning false in the isUpSwip condition, we will send a new Intent like this: Listing 3. Adding the new Activity to GestureDetector
Note that an Intent accepts a context and a class that you wish to start, and startActivity is a method of an Activity instance. To try it out, start your simulator instance and swipe up when a word is displayed. You should see a new Activity, as shown in Figure 2. Figure 2. A quiz is born! To try it out, start your simulator instance and swipe up when a word is displayed. You should see a new Activity appear as shown in Figure 2. Slide up, slide down The first thing we need to do is add a gesture detector so that the user can leave the test and return to learning the vocabulary. Since swiping up takes the user to the test, it makes sense that swiping down would leave the test. We call finish to exit the test activity and return to study mode. Listing 4. Calling finish inside a swipe gesture
As mentioned in the previous article, we implemented an action bar in the learning activity in Overheard Word to exit the app. Here we reuse that code to create a new quiz exit function. Listing 5. An action bar to quit the quiz
Note that we don’t want to exit our app, just leave the quiz; so we updated the action bar text to ‘Exit Test’ instead of ‘Exit’. Words and Definitions Next, we set up a definition with one correct answer and two incorrect answers. Since we are using Thingamejig to handle the logic, the only thing we need to do is enter the relevant fields, as shown in Listing 6: Listing 6. Setting up a testable word with definitions
In Listing 5, the onCreate method first builds the base view we provide. Next, the gestures are initialized. Just like the original learning activity, a WordTestEngine (instead of WordStudyEngine) is instantiated and connected to the Overheard Word word store (a JSON file in this case). The possibleAnswersForm method returns a list of three definitions, one of which is correct. In order to make the quiz not always display in the same order, the WordTestEngine instance will shuffle the definitions. Finally, we loop over the input of words that match the given definition into our RadioButtons. If you launch the app instance, you will see a nice form quiz interface with a word definition and three word options. Figure 3. The quiz UI with words and a definition Also notice at the bottom of the screen there is a static Result button and a counter showing the total number of quiz questions. In the screenshot, 1 out of 10 questions has been used. So far, we've quickly created sample logic for the quiz using Thingamejig and some old Activity logic. Next we'll manually handle the radio button portion of the quiz. We'll call back into the radio button group behavior and listen for when an option is selected, without requiring the user to click an additional button (like submit) to indicate the selection. This way the user gets immediate feedback without any additional action. Handling event dispatch I mentioned that I wanted to provide immediate feedback based on their word choices. Furthermore, I also wanted Overheard Word to briefly pause for feedback. If the user gives a correct response, I want him or her to have a few seconds to get a feel for it before moving on to the next question. If the answer is incorrect, I should either let them try again or wait a second or two before showing the correct answer. Either way, the user has a chance to learn how they did on the quiz. But remember, we don't just make Android apps what we like; there's no way to force apps to sleep or start threads at any time. (Starting threads in an Android app is possible, of course, but the factors involved are beyond your control.) Just as we use Intents to tell an Android app how we want it to behave, we use Handlers to indicate that we desire to defer or immediately execute something; how Android handles the request is up to Android. Please Pause In Android, you use Handlers to handle event dispatching and also pass information between threads. In this case, we will always dispatch an event with Handlers, which will be a sealed Runnable instance. In response to the user selecting a word, we will create a Handler instance, pass some logic inside the Runnable (such as showing a new Activity) and set a delay of a few seconds. We implement the onCheckedChangeListener listener attached to the RadioGroup to call back the radio button click, as shown below: Listing 7 Listing 7. setOnCheckedChangeListener
Just like traditional GUI programming with JavaAWT or Swing, you can add event listeners. In this example, we are adding an OnCheckedChangeListener to the RadioGroup. When the user selects a RadioButton, the listener code will be executed. If the user selects correctly, a new instance of OverheardQuizActivity will display a new word. The user also gets a friendly green text message of 'correct' in the TextView. If the user makes an incorrect selection, the information text is red and the Activity UI is reset to allow the user to try again. Also note that the two Handler instances delay starting the thread, one because a new Intent is being started, and the other because the UI is reset. You also need to remove the default text from the quiz layout defined in Listing 1. Simply go back to the XML file and delete the text from the TextView with the ID quiz_result. android:text="Result". Now launch your app and test it! Bundles in Android The next thing we need to do is add some logic to the counter in the bottom right corner of the quiz view (see Figure 3). As the quiz is viewed from the user's perspective, we want to show him or her their progress, such as how many questions are left. In order for our counter to work, we need a way to maintain the state of the counter (for example, it was 2 before, and now it should be 3), which requires us to be able to pass data from one activity to another, and we use Bundles to do this task. If you have been reading this series for a while, then you have already come into contact with Bundles, because a Bundle instance is a long parameter of each Activity's onCreate method. Bundles must be a Map of key-value pairs. You can retrieve the same value type by calling the method on the key value. In addition to Activity instances, Bundles are also included in Intents. In fact, if you put a new Activity key-value pair into the Intent association, you can retrieve the Bundle through the Intent to get the created Activity. These may sound confusing, but you will understand it once you do it yourself. Creating a Counter First, we update the Handler that creates the new OverheardQuizActivity and add an incrementing counter, as shown in Listing 8: Listing 8. Adding an incremented counter
The putExtra method is associated with the key value QUIZ_NUM added to quizNumber, both of which are member variables of the OverheardQuiz class. Next, we update the onCreate method of the OverheardQuiz class to get this value from the Intent that was launched. (If onCreate doesn't find a value, it is assigned 1. The reason is that the QUIZ_NUM value was not set when the OverheardWordActivity was first launched.) If this value is greater than 10, the sequence is reset and we send the user a celebratory message (via a Toast). Finally, we will update the counter's TextView to the new count. The full code is shown in Listing 9. Listing 9. Updating a TextView with the current count
Now let's fire up the final version of Overheard Word and see what it does. Figure 4. Overheard Word is munificent! With that, I think we have successfully built an app: it provides a valuable service (fast internal vocabulary building), has an intuitive interface, and now even has a fun quiz feature that keeps users coming back. The UI could be more interesting (black text on white background is annoying), but now it’s time to sign it and release it to the market. Sign your Android app If you want your app to run on everyone's devices, you need to deploy it to a public app store. While there are many options, the two most popular app stores for Android are Google Play and Amazon Appstore (see ). For security reasons, both stores require that their apps must be signed by the author. When you register an app, you create a private certificate. Then you sign your code with this digital certificate. Signing is a way to distribute a special binary that is signed by you and not by some unknown, potentially malicious party. Android devices will not install unsigned apps, so if you want to be an Android developer, you need to know how to sign your apps. A few articles I showed you before on "How to install a test app" on an Android device, you actually signed it with a test certificate. Test certificates are good for testing, but the public app store doesn't accept them. At this point you need to learn how to generate a real certificate through Eclipse. To sign the app, go to the project menu in Eclipse ADT and select 'Android Tools' -> 'Export Signed Application Package'. The Generate New Certificate Wizard will pop up. Figure 5. The Eclipse ADT certificate wizard This app will guide you through the dialog box to provide your personal information. If you have ever bought an SSL certificate for a website, you will go through a similar process. There is no certificate authority process in Android. Therefore, app signing is easier than SSL certificates, but it is also more likely to be fraudulent; for example, no one can stop me from saying that I am from Bank of America or Coca-Cola. Figure 6. Identifying information for the certificate After you have created the certificate and private key store (to store your private key), you can export an app package with a .apk extension, as shown in Figure 7. Figure 7. OverheardWord.apk Once your app is digitally signed, you are free to distribute it to the world, at least technically. However, signing is not enough to release it to the public app stores. You also need to promote it. Improve your app Public app stores are just platforms that provide a global market, but smart customers in the app market will not download old stuff. Just like you need to know how to package valuable features and fun in building apps, you can entice users to try it. When you upload (or publish) an Android app to Google Play or Amazon Appstore, you can do the following:
Of course, you also need to decide whether your app will be free or paid; and you need to set an appropriate price, market rate. Indeed, you may find that building and signing your app is the easy part, while marketing mobile apps in this booming and competitive market is the hard part. Summarize As you know, native Android development is just one point in the production of mobile apps. The next article in this series will first look at how to build mobile web apps using HTML5. Web-based mobile apps offer some advantages over native apps – like the fact that you can publish them anywhere! But web-based apps can’t be as fancy as native apps and may not perform the same way as native apps. As we delve deeper into HTML5, we’ll discover the joys and constraints of mobile web apps. |
<<: Mobile technology for the masses: words and gestures from the Overheard Word
>>: The relationship between programmer growth and the number of lines of code
x Improve various leg shape training methods Basic...
If you want to do your work well, you must first ...
In July this year, during the 2nd anniversary cel...
The same applies to SEM bidding. Don’t use dilige...
1. Overview The Monte Carlo method is a calculati...
...
Following Jia Yueting's Weibo post last month...
The most effective way to directly control the av...
2015 has just begun, and the WeChat JS SDK was re...
On October 10, Lee Jae-yong, the third-generation...
If "live streaming by all" and "li...
recently Public Security Bureau of Dangyang City,...
Do you know the answers to these questions about ...
In iOS 6 released two years ago, Apple removed the...
Early this morning, Apple pushed the iOS 10 Beta 3...