Android基礎整合項目之節日群發助手(三),android群發
Android基礎整合項目(一) 之節日群發助手part 3
——轉載請註明出處:coder-pig
本節引言:
在前面兩個章節中我們已經完成了群發助手的讀連絡人,存取資料庫;使用
SimpleCursorAdapter綁定資料庫與ListView;實現listview的全選與全不選;
也把需要撥打到電話號碼的id以list集合的形式通過Intent傳送到了第三個介面
今天我們就來完成第三個介面的開發,工作如下:
1)完成第三個Activity的布局
2)解析第二個Activity通過Intent傳送過來的List集合
3)讀取資料表中的festival表中的節日祝福語,顯示到介面上
4)完成切換祝福語的功能
5)完成發送統一祝福語的功能
6)完成發送不同祝福語的功能
7)使用具有列表和帶確定按鈕的AlertDialog
8)使用SmsManager完成簡訊的發送
本文:
1.完成第三個Activity的布局:
代碼如下:
<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="親愛的" android:id="@+id/editappellation" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="XXX,我是" /> <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="發送不同祝福語" 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="發送統一祝福語" android:id="@+id/btnsendlike" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="返回" android:id="@+id/btnback" /> </LinearLayout></LinearLayout>
2.解析第二個Activity傳送過來的List集合:
直接用list儲存即可,增強for迴圈可以去掉,只是用來確認傳過來的集合是否有資料,
從而避免null 指標問題的出現
Intent it = getIntent();final List<Integer> list = it.getIntegerArrayListExtra("ids");int i = 1;//下面這個語句用於查看是否有傳值過來,傳了什麼值,通常用log.?來跟蹤錯誤for(Integer j:list){System.out.println(j);}
3.讀取資料庫表中的festival表的祝福語並顯示
簡單的一條查詢語句即可:
sql = "select detail from festival where sentence_id = " + num+"";textwish.setText(getWish(sql));
另外因為我們的操作很多都要查詢資料庫,就直接寫到一個方法中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是一個整數1-10都可以,看資料庫表的記錄數決定的
4.完成祝福語的切換功能:
其實這裡就是簡單的改變上面那個num的數字而已,自增++
然後等於十的時候把num重設為1即可
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)完成發送統一祝福語的功能:
就是給每個連絡人發送相同的簡訊,這裡的話,先讀取傳過來的集合擷取id
然後根據id讀取Contacts表中id對應的連絡人名稱,電話號碼;
將這兩個組合到一起,作為對話方塊的清單項目的資源數組
還需要建立一個帶列表的對話方塊,添加一個確定按鈕
代碼:
btnsendlike.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {String ids = "";//將list集合中的資料取出來,結果可能是:7,8,9,這樣的for(Integer j:list){ids = ids + j + ",";}//去掉小尾巴","直接去掉最後一位即可ids = ids.substring(0, ids.length()-1);System.out.println(ids);//需要取出Contacts表中的連絡人資訊,用於等下對話方塊內容顯示以及傳送簡訊時sql = "select * from contacts where _id in("+ids+")";System.out.println(sql);List<Map<String,String>> lc = getContacts(sql);//取出集合中的元素放到字串資料中//判斷需建立的數組的長度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]);}//彈出確認的對話方塊:builder = new AlertDialog.Builder(ThridActivity.this);builder.setTitle("統一發送祝福語模式\n請確認發送人員:");builder.setIcon(R.drawable.ic_launcher);builder.setItems(show,null);builder.setPositiveButton("確定發送", 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] + "我是"+editme.getText().toString() +textwish.getText().toString();sendMessage(num, wish);}}}); alert = builder.create(); alert.show(); }});
6)建立傳送簡訊的方法:
private void sendMessage(String number,String message){SmsManager.getDefault().sendTextMessage(number, null, message, null, null);Toast.makeText(getApplicationContext(), "發送成功", Toast.LENGTH_SHORT).show();//在模擬器環境中我們需要查看發出的簡訊內容或者真機調試時不想浪費簡訊錢//就使用log.i查看發出的簡訊內容即可Log.i("sendMessage", number+message);}
另外,使用上述方法傳送簡訊,你的手機是不會保留髮送的資訊記錄的;
其實就是後台傳送簡訊!
7)好了,完成5,6步後程式基本成型了:
完成前面6步後,程式已經可以完成準系統了:
效果如下:
接著查看我們的Logcat,可以看到發送的資訊內容:
8)最後再完成隨機祝福語部分
就是隨機給不同的人發送不同內容的簡訊:
其實這裡和上面那個部分是大同小異的,僅僅是修改傳送簡訊時
的祝福語內容,使用隨機數決定發送的祝福語而已,僅僅需要添加下述代碼即可:
1)取出Festival表中的detail欄位的所有記錄,儲存到字串數組中
//將所有的祝福語簡訊儲存到字串數組中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)使用隨機數,隨機的取出祝福語
for(int p = 0;p < lg;p++){String num = finumber[p];//只需要修改輸出時的祝福語內容即可int randow = (int) (Math.random() * 10);String wish = editappellation.getText().toString() + finame[p] + "我是"+editme.getText().toString() +fishowmsg[randow];sendMessage(num, wish);}
運行:
截至之當前的項目代碼:
代碼下載
知識點總結:
1)解析Intent中儲存的list集合:
Intent it = getIntent();
final List<Integer> list = it.getIntegerArrayListExtra("ids");
2)SQLite資料庫的相關操作,以及cursor的使用
切忌一點,使用cursor一定要調用cursor.moveToFirst()不然會報錯哦!
另外可以調用moveToNext()可以使遊標後移!可使用getXxx擷取不同類型的資料
3)使用SmsManager.getDefault().sendTextMessage(number, null, message, null, null);
傳送簡訊
4)產生1~10內的隨機整數:int randow = (int) (Math.random() * 10);
好了,節日群發助手這個項目開發就到這裡了,app僅僅是具有雛形,UI和代碼都沒有
最佳化,存在一定的冗餘和bug,各位學員在自己編寫的過程中可以自行的修改或者最佳化
本文的初衷是協助各位初學者鞏固相關的知識點的!後續會推出類似的知識點整合
項目,敬請關注,謝謝
當然有時間也會修改下這個app,正如大牛所說的,改下UI或許會是個好的app
怎破解安卓系統群發簡訊限制?本人節日要群發上千條簡訊一超過100條就要在手機上點繼續十分無語
提供簡訊群發平台,多媒體訊息群發平台軟體,wap平台軟體.3G融合訊息平台(簡訊多媒體訊息wap)
提供簡訊群發平台,多媒體訊息群發平台軟體,wap平台軟體.3G融合訊息平台(簡訊多媒體訊息wap)
android 236版本手機怎群發簡訊?按了菜單之後沒有群發的那個鍵 說安裝軟體群發什的,可以匿了
你可以裝個第三方簡訊軟體,比如說GO簡訊