Android basic integration project-holiday group Assistant (3), android Group
Basic Android Integration Project (1)Holiday group assistant part 3
-- Reprinted with the source: coder-pig
This section introduces:
In the previous two chapters, we have completed reading contacts for group assistants to access the database. Use
SimpleCursorAdapter is used to bind a database to a ListView; select all or none of listview;
The id of the phone number to be called is also transmitted to the third interface through Intent in the form of a list set.
Today, we will complete the development of the third interface. The work is as follows:
1) Complete the layout of the third Activity
2) parse the List set sent by the second Activity through Intent
3) read the holiday greetings from the festival table in the data table and display them on the page.
4) Complete the toggle switch function
5) Complete the function of sending unified greetings
6) Complete the function of sending different greetings
7) Use AlertDialog with a list and a confirmation button
8) Use SmsManager to send SMS messages
Body:
1. Complete the layout of the third Activity:
The Code is as follows:
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: id = "@ + id/LinearLayout1" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical" tools: context = "com. jay. example. festivalsmshelper. mainActivity "> <LinearLayout android: layout_width =" match_parent "android: layout_height =" wrap_content "android: orientation =" horizontal "> <EditText android: layout_width =" wrap_content "android: layout_height = "wrap_content" android: hint = "dear" android: id = "@ + id/editappellation"/> <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "XXX, I am"/> <EditText android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: hint = "" android: id = "@ + id/editme"/> </LinearLayout> <TextView android: layout_width = "match_parent" android: layout_height = "150dp" android: id = "@ + id/textwish"/> <LinearLayout android: layout_width = "match_parent" android: layout_height = "wrap_content" android: orientation = "horizontal"> <Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "" android: id = "@ + id/btnchange"/> <Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "send different greetings" android: id = "@ + id/btnsendunlike"/> </LinearLayout> <LinearLayout android: layout_width = "match_parent" android: layout_height = "wrap_content" android: orientation = "horizontal"> <Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Send unified greetings" android: id = "@ + id/btnsendlike"/> <Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "return" android: id = "@ + id/btnback"/> </LinearLayout>
2. parse the List set sent by the second Activity:
You can store the data directly using the list, and the Enhancement for loop can be removed. It is only used to confirm whether the passed set has data,
This prevents null pointers.
Intent it = getIntent (); final List <Integer> list = it. getIntegerArrayListExtra ("ids"); int I = 1; // The following statement is used to check whether the value is passed. What value is passed, usually log .? For (Integer j: list) {System. out. println (j );}
3. Read and display the delimiter of the festival table in the database table
A simple query statement:
sql = "select detail from festival where sentence_id = " + num+"";textwish.setText(getWish(sql));
In addition, because many of our operations need to query the database, we can directly write it to a method getWish ()
private String getWish(String sql){String wish = null;GetContactsService gcs = new GetContactsService(ThridActivity.this);Cursor curosr =gcs.query(sql, null);curosr.moveToFirst();wish = curosr.getString(0);curosr.close();return wish;}
Ps: num is an integer ranging from 1 to 10. It depends on the number of records in the database table.
4. Complete the toggle function:
In fact, this is just a simple change to the num number above, auto-incrementing ++
Then, set num to 1 when the value is 10.
btnchange.setOnClickListener(new OnClickListener() {public void onClick(View v) {if(num == 10)num = 1;else ++num;sql = "select detail from festival where sentence_id = " + num+"";textwish.setText(getWish(sql));}});
5) Complete the function of sending unified greetings:
Send the same text message to each contact. Here, read the passed set to get the id.
Then, according to the idRead the contact name and phone number corresponding to the id in the Contacts table;
Combine these two items as a resource array of the list items in the dialog box.
You also need to create a dialog box with a list and add a confirmation button.
Code:
Btnsendlike. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {String ids = ""; // retrieves data from the list set. The result may be:, 9, such as for (Integer j: list) {ids = ids + j + "," ;}// remove the tail "," remove the last bit to ids = ids. substring (0, ids. length ()-1); System. out. println (ids); // you need to retrieve the contact information in the Contacts table, used to display and send text messages in the following dialog box: SQL = "select * from contacts where _ id in (" + ids + ")"; System. out. println (SQL); List <Map <String, String> lc = getContacts (SQL ); // extract the elements from the set and put them in the String data. // judge whether the length of the array to be created is int length = 0; for (Map <String, String> mp: lc) {length ++;} final int lg = length; String name [] = new String [length]; String number [] = new String [length]; string show [] = new String [length]; final String finumber [] = number; final String finame [] = name; int I = 0; for (Map <String, string> mp: lc) {name [I] = mp. get ("name"); number [I] = mp. get ("number"); Log. I ("name [I]", name [I]); Log. I ("number [I]", number [I]); I ++ ;}for (int o = 0; o <I; o ++) {show [o] = name [o] + ":" + number [o]; Log. I ("show [o]", show [o]) ;}// the confirmation dialog box is displayed: builder = new AlertDialog. builder (ThridActivity. this); builder. setTitle ("Uniform sending mode \ n please confirm the sender:"); builder. setIcon (R. drawable. ic_launcher); builder. setItems (show, null); builder. setPositiveButton ("Confirm sending", new DialogInterface. onClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {for (int p = 0; p <lg; p ++) {String num = finumber [p]; string wish = editappellation. getText (). toString () + finame [p] + "I am" + editme. getText (). toString () + textwish. getText (). toString (); sendMessage (num, wish) ;}}); alert = builder. create (); alert. show ();}});
6) Create an SMS sending method:
Private void sendMessage (String number, String message) {SmsManager. getDefault (). sendTextMessage (number, null, message, null, null); Toast. makeText (getApplicationContext (), "sent successfully", Toast. LENGTH_SHORT ). show (); // in the simulator environment, we need to check the sent text message content or do not waste text message money when debugging the real machine. // log is used. I can view the sent text message content to Log. I ("sendMessage", number + message );}
In addition, if you use the above method to send text messages, your mobile phone will not keep the message records;
It is actually sending text messages in the background!
7) Well, after completing step 5 and step 6, the program is basically formed:
After completing the first six steps, the program can complete the basic functions:
The effect is as follows:
Next, check our Logcat to see the sent information:
8) Finally, complete the random speech part.
It means to randomly send different text messages to different people:
In fact, this part is similar to the one above, only when you modify the text message
You only need to add the following code:
1) retrieve all records of the detail field in the Festival table and store them in a string array.
// Store all greeting text messages to the String array String SQL = "select detail from festival"; String [] showmsg = new String [10]; getContactsService gcService = new GetContactsService (ThridActivity. this); Cursor curosr = gcService. query (SQL, null); int s = 0; curosr. moveToFirst (); while (curosr. moveToNext () {showmsg [s] = curosr. getString (0); s ++;} final String [] fishowmsg = showmsg;
2) random numbers are used to randomly retrieve slang
For (int p = 0; p <lg; p ++) {String num = finumber [p]; // you only need to modify the speaker content of the output to int randow = (int) (Math. random () * 10); String wish = editappellation. getText (). toString () + finame [p] + "I am" + editme. getText (). toString () + fishowmsg [randow]; sendMessage (num, wish );}
Run:
Project Code as of now:
Code download
Knowledge Point summary:
1) parse the list set stored in Intent:
Intent it = getIntent ();
Final List <Integer> list = it. getIntegerArrayListExtra ("ids ");
2) related operations of the SQLite database and the use of cursor
Do not call cursor. moveToFirst () When Using cursor. Otherwise, an error will be reported!
In addition, you can call moveToNext () to move the cursor behind! You can use getXxx to obtain different types of data.
3) Use SmsManager. getDefault (). sendTextMessage (number, null, message, null, null );
Send SMS
4) generate 1 ~ Random integer in 10: int randow = (int) (Math. random () * 10 );
Now, the program development of the holiday group assistant is here. The app is just a prototype, and there is no UI or code.
Optimization, there is a certain degree of redundancy and bugs, you can modify or optimize your own writing process
The original intention of this article is to help beginners consolidate related knowledge points! Similar knowledge point integration will be launched in the future
Project, so stay tuned. Thank you
Of course, there is time to modify this app. As Daniel said, changing the UI may be a good app.
How can I crack the limit on sending SMS messages to Android systems? I want to send over 100 text messages to my group on a holiday, so I have to order to continue to be speechless on my cell phone.
Provides SMS sending platform, MMS sending platform software, and wap platform software. 3g integrated messaging platform (SMS message wap)
Provides SMS sending platform, MMS sending platform software, and wap platform software. 3g integrated messaging platform (SMS message wap)
How can I send a group of text messages to android 236 mobile phones? After you press the menu, the key that does not appear in the group to say that you have installed the software group. You can renew it.
You can install a third-party sms software, such as go sms.