您的位置:首页 > 移动开发 > Android开发

Android基础整合项目之节日群发助手(三)

2016-01-07 00:16 651 查看
Android基础整合项目(一) [b]之节日群发助手part 3[/b]

——转载请注明出处: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的布局:



代码如下:

[html] view
plaincopyprint?

<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循环可以去掉,只是用来确认传过来的集合是否有数据,

从而避免空指针问题的出现

[java] view
plaincopyprint?

Intent it = getIntent();  

        final List<Integer> list = it.getIntegerArrayListExtra("ids");  

        int i = 1;  

        //下面这个语句用于查看是否有传值过来,传了什么值,通常用log.?来跟踪错误  

        for(Integer j:list)  

        {  

            System.out.println(j);  

        }  

3.读取数据库表中的festival表的祝福语并显示

简单的一条查询语句即可:

[java] view
plaincopyprint?

sql = "select detail from festival where sentence_id = " + num+"";  

        textwish.setText(getWish(sql));  

另外因为我们的操作很多都要查询数据库,就直接写到一个方法中getWish()

[java] view
plaincopyprint?

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

[java] view
plaincopyprint?

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对应的联系人名称,电话号码;

将这两个组合到一起,作为对话框的列表项的资源数组

还需要创建一个带列表的对话框,添加一个确定按钮

代码:

[java] view
plaincopyprint?

btnsendlike.setOnClickListener(new OnClickListener() {  

            @Override  

            public 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()  

                {  

                    @Override  

                    public 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)创建发送短信的方法:

[java] view
plaincopyprint?

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字段的所有记录,存储到字符串数组中

[java] view
plaincopyprint?

//将所有的祝福语短信存储到字符串数组中  

                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)使用随机数,随机的取出祝福语

[java] view
plaincopyprint?

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: