Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2208788
  • 博文数量: 436
  • 博客积分: 9833
  • 博客等级: 中将
  • 技术积分: 5558
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-29 10:27
文章存档

2013年(47)

2012年(79)

2011年(192)

2010年(118)

分类:

2010-11-04 19:25:42

组长:刘宇 组员:吕超 马思雨 赵朝 刘晓楠

本周我们组总结了google android平台开发的代码,收获很大。以下是具体内容:

“情景模式”界面初始化(吕超)

 

 

这个情景模式包括了3个大的选项,考虑使用TabHost来实现主界面的布局,然后在Tabhost中使用不同的布局,完成实际功能。

 

1.在普通模式下,可以设置一组单选项来切换不同的情境模式;

2.定时情景模式在普通模式下还需要加上一个TimePicker来显示并设置时间;

3.定义情景模式则需要自己定义声音的大小。

 

TabHost,是一个用来存放多个Tab标签的容器,每个Tab都可以对应自己的布局。要使用TabHost,首先需要通过getTabHost方法来获取TabHost的对象,然后通过addTab方法来向TabHost中添加Tab。每个Tab在切换时都会产生一个事件,要捕捉这个事件需要设置TabActivity的事件监听setOnTabChangedListener。)

 

TimePickerAndroid平台实现时间功能。用Calendar类获得系统时间,改变时间需要设置setOnTimeChangeListener监听来设置时间。)

 

情景模式中的调解声音大小,静音,振动等需要使用AudioManager来控制和访问铃声。

 

AudioManagerAndroid多媒体接口,为上层应用提供了声音设置管理接口。)

 

实现代码如下:

 

package com.zz.android.RingProfile;

 

import java.util.Date;

 

import com.yarin.android.RingProfile.R;

 

import android.app.AlarmManager;

import android.app.PendingIntent;

import android.app.TabActivity;

import android.content.Context;

import android.content.Intent;

import android.media.AudioManager;

import android.os.Bundle;

import android.util.Log;

import android.view.MotionEvent;

import android.view.View;

import android.view.View.OnTouchListener;

import android.widget.Button;

import android.widget.ImageButton;

import android.widget.ImageView;

import android.widget.ProgressBar;

import android.widget.RadioButton;

import android.widget.RadioGroup;

import android.widget.TabHost;

import android.widget.TimePicker;

import android.widget.RadioGroup.OnCheckedChangeListener;

import android.widget.TabHost.OnTabChangeListener;

 

public class RingProfile extends TabActivity

{

         private static final String TAG="RingToggle";

 

         /*声明TabHost对象*/

         private TabHost mTabHost;

 

         protected boolean isChange;

 

         private AlarmManager mAlarmManager;

 

         private TimePicker mTimePicker ;

 

         private int mTab;

 

         private ImageView  myImage;

         private ImageButton       downButton;

         private ImageButton       upButton;

         private ImageButton       normalButton;

         private ImageButton       muteButton;

         private ImageButton       vibrateButton;

         private ProgressBar         myProgress;

         private AudioManager    audioMa;

         private int                  volume     = 0;

 

         @Override

         public void onCreate(Bundle savedInstanceState)

         {

             super.onCreate(savedInstanceState);

             setContentView(R.layout.main);

 

             /*取得TabHost对象*/

              mTabHost = getTabHost();

 

             /* TabHost添加标签 */

             /*新建一个newTabSpec(newTabSpec)*/

             /*设置其标签和图标(setIndicator)*/

             /*设置内容(setContent)*/

 

             mTabHost.addTab(mTabHost.newTabSpec("tab_test1")

                          .setIndicator("普通情景模式",getResources().getDrawable(R.drawable.icon))

                          .setContent(R.id.RadioGroup01));

             mTabHost.addTab(mTabHost.newTabSpec("tab_test2")

                          .setIndicator("定时情景模式",getResources().getDrawable(R.drawable.timeprofile))

                          .setContent(R.id.RelativeLayout01));

             mTabHost.addTab(mTabHost.newTabSpec("tab_test3")

                          .setIndicator("自定义情景模式",getResources().getDrawable(R.drawable.addprofile))

                          .setContent(R.id.AbsoluteLayout03));

 

             /*设置TabHost的背景图片资源*/

             mTabHost.setBackgroundResource(R.drawable.bg);

 

             /*设置当前显示哪一个标签*/

             mTabHost.setCurrentTab(0);

             mTab = 0;

             updateRadioGroup();

 

             /*初始化,取得AudioManager*/

             audioMa = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

             myImage = (ImageView)findViewById(R.id.myImage);

             myProgress = (ProgressBar)findViewById(R.id.myProgress);

             downButton = (ImageButton)findViewById(R.id.downButton);

             upButton = (ImageButton)findViewById(R.id.upButton);

             normalButton = (ImageButton)findViewById(R.id.normalButton);

             muteButton = (ImageButton)findViewById(R.id.muteButton);

             vibrateButton = (ImageButton)findViewById(R.id.vibrateButton);

个人总结:

 

这周的工作在理解代码的基础上,进行代码开发。首先在代码的理解上还不够透彻,对语言的掌握还不够熟练,因此在执行上花了一点时间。在界面初始化这一部分上,总体来说还是有章法可循的,整体的使用和C语言很像,但因平台的不同,在功能的提供上,有时还是转不过来,不过这不太影响进度。就个人而言,比起代码,个人更关心设计思想,如:界面的交互是不是够人性化,功能操作是不是能够简单易行,但在这次实践中并没有充分体现出来。

 

 

标签事件处理(马思雨)

 

 

table通过多个标签切换显示不同的内容,要实现之一效果,我首先了解了TabHost,它是一个用来存放多个Tab标签的容器,每一个Tab都可以对应自己的布局。

要使用TabHost,就要通过getTabHost方法来获取TabHost的对象,每个Tab在切换时都会产生一个事件,要捕捉这个时间需要设置TabActivity的时间监听setOnTabChangedListener

 //标签切换事件处理,setOnTabChangedListener 

mTabHost.setOnTabChangedListener(new OnTabChangeListener()

             {

            public void onTabChanged(String tabId)

            {

                     if ( tabId.equals("tab_test1") )

                                     {

                               mTab = 0;

 

                    mAlarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);

 

                    mTimePicker = (TimePicker)findViewById(R.id.timePkr);

                    mTimePicker.setIs24HourView(true);

 

 

                               updateRadioGroup();

                                     }

                     else if ( tabId.equals("tab_test2") )

                                     {

                               mTab = 1;

                    mAlarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);

 

                    mTimePicker = (TimePicker)findViewById(R.id.timePkr);

                    mTimePicker.setIs24HourView(true);

 

                               updateRadioGroup();

                                     }

            }

        });

接下来需要处理ui中定义的按钮,选项的事件

        /***************************************************************/

        RadioGroup group1 = (RadioGroup) findViewById(R.id.RadioGroup01);

        group1.setOnCheckedChangeListener(new OnCheckedChangeListener()

        {

                 public void onCheckedChanged(RadioGroup group, int checkedId)

                 {

                     if (isChange)

                               return;

                     switch (checkedId)

                     {

                               case R.id.ring_and_vibrate01: ringAndVibrate(); break;

                               case R.id.ring01: ring(); break;

                               case R.id.vibrate01: vibrate(); break;

                               case R.id.silent01: silent(); break;

                     }

                     RadioButton radio = (RadioButton) findViewById(checkedId);

                     if (radio != null)

                               radio.setTextSize(30);

                 }

        });

        //RadioButton添加监听器

        for (int i = 0, l = group1.getChildCount(); i < l; i++)

        {

                 RadioButton radio = (RadioButton) group1.getChildAt(i);

            radio.setOnTouchListener(new OnTouchListener()

            {

                     public boolean onTouch(View v, MotionEvent event)

                     {

                               RadioButton radio = (RadioButton) v;

                               if (!radio.isChecked())

                                        return false;

                               radio.setTextSize(30);

                               return false;

                     }

            });

        }

        /***************************************************************/

 

        //添加onChangeListener

        RadioGroup group2 = (RadioGroup) findViewById(R.id.RadioGroup02);

        group2.setOnCheckedChangeListener(new OnCheckedChangeListener()

        {

                 public void onCheckedChanged(RadioGroup group, int checkedId)

                 {

                     if (isChange)

                               return;

                     switch (checkedId)

                     {

                               case R.id.ring_and_vibrate02: ringAndVibrate(); break;

                               case R.id.ring02: ring(); break;

                               case R.id.vibrate02: vibrate(); break;

                               case R.id.silent02: silent(); break;

                     }

                     RadioButton radio = (RadioButton) findViewById(checkedId);

                     if (radio != null)

                               radio.setTextSize(30);

                 }

        });

        //RadioButton添加监听器

        for (int i = 0, l = group2.getChildCount(); i < l; i++)

        {

                 RadioButton radio = (RadioButton) group2.getChildAt(i);

            radio.setOnTouchListener(new OnTouchListener()

            {

                     public boolean onTouch(View v, MotionEvent event)

                     {

                               RadioButton radio = (RadioButton) v;

                               if (!radio.isChecked())

                                        return false;

                               radio.setTextSize(30);

                              return false;

                     }

            });

        }

 

        //取得手机的初始音量,并初始化进度条

        volume=audioMa.getStreamVolume(AudioManager.STREAM_RING);

        myProgress.setProgress(volume);

        //取得初始模式,并分别设置图标

        int mode=audioMa.getRingerMode();

        if(mode==AudioManager.RINGER_MODE_NORMAL)

        {

          myImage.setImageDrawable(getResources()

                                   .getDrawable(R.drawable.icon));

        }

        else if(mode==AudioManager.RINGER_MODE_SILENT)

        {

          myImage.setImageDrawable(getResources()

                                   .getDrawable(R.drawable.mute));

        }

        else if(mode==AudioManager.RINGER_MODE_VIBRATE)

        {

          myImage.setImageDrawable(getResources()

                                   .getDrawable(R.drawable.vibrate));

        }

个人总结:

 

这次的任务是对代码进行理解和开发,分给我的任务是标签事件处理和UI事件处理方面的内容,为此借来了参考书,首先找到了转换卡一专栏,了解到了TabWidget类和TabHost类的一些内容,对标签处理进行了解,然后又设置了添加了2UI界面中的按钮,选项事件并对这些进行监听,使程序得以运行,从中我运用了之前java中学到的监听方面的内容,通过jdk查询了一些以前没见过的类,进行了了解,总算了解到了android平台一些初步的内容,受益匪浅。

 

 

“情景模式”具体操作1(刘晓楠)

 

 

因为涉及到情景模式的具体操作,所以需要调节声音的大小、静音、震动等。这就需要使用AudioManager,它主要用来控制和访问铃声。

 

主要类别分为三部分:

1.调节声音的大小的操作。即upButton.setOnClickListener相关部分代码。

2.改变其状态的操作。由正常状态normalButton.setOnClickListener,静音状态muteButton.setOnClickListener,震动状态 vibrateButton.setOnClickListener三种状态组成。

3.对于情景模式的更新和读取当前模式,更新情景模式updateRadioGroup与取得当前情景模式 currentMode    

 

这段代码主要就是对于其音频设置的操作平台,AudioManager提供了给我们较多的操作铃音的属性,再加上自己想法和各方面的完善而完成的操作代码。

 

 

实现代码如下:

 

        //降低音量按键

        upButton.setOnClickListener(new Button.OnClickListener()

        {

          @Override

          public void onClick(View arg0)

          {

                   //AudioManager.ADJUST_RAISE提高音量

            audioMa.adjustVolume(AudioManager.ADJUST_RAISE, 0);

            volume=audioMa.getStreamVolume(AudioManager.STREAM_RING);

            myProgress.setProgress(volume);

            int mode=audioMa.getRingerMode();

            if(mode==AudioManager.RINGER_MODE_NORMAL)

            {

              myImage.setImageDrawable(getResources()

                                       .getDrawable(R.drawable.icon));

            }

            else if(mode==AudioManager.RINGER_MODE_SILENT)

            {

              myImage.setImageDrawable(getResources()

                                       .getDrawable(R.drawable.mute));

            }

            else if(mode==AudioManager.RINGER_MODE_VIBRATE)

            {

              myImage.setImageDrawable(getResources()

                                      .getDrawable(R.drawable.vibrate));

            }

          }

        });

        //正常状态

        normalButton.setOnClickListener(new Button.OnClickListener()

        {

          @Override

          public void onClick(View arg0)

          {

            audioMa.setRingerMode(AudioManager.RINGER_MODE_NORMAL);

            volume=audioMa.getStreamVolume(AudioManager.STREAM_RING);

            myProgress.setProgress(volume);

            myImage.setImageDrawable(getResources()

                                     .getDrawable(R.drawable.icon));

          }

        });

        //静音状态

        muteButton.setOnClickListener(new Button.OnClickListener()

        {

          @Override

          public void onClick(View arg0)

          {

            audioMa.setRingerMode(AudioManager.RINGER_MODE_SILENT);

            volume=audioMa.getStreamVolume(AudioManager.STREAM_RING);

            myProgress.setProgress(volume);

            myImage.setImageDrawable(getResources()

                                     .getDrawable(R.drawable.mute));

          }

        });

        //振动状态

        vibrateButton.setOnClickListener(new Button.OnClickListener()

        {

          @Override

          public void onClick(View arg0)

          {

            audioMa.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);

            volume=audioMa.getStreamVolume(AudioManager.STREAM_RING);

            myProgress.setProgress(volume);

            myImage.setImageDrawable(getResources()

                                     .getDrawable(R.drawable.vibrate));

          }

        });

         }

 

         // 更新情景模式

         protected void updateRadioGroup()

         {

                   int checkedId = currentMode();

                   RadioButton checked = (RadioButton) findViewById(checkedId);

                   isChange = true;

                   checked.setChecked(true);

                   isChange = false;

         }

 

 

         // 取得当前情景模式

         protected int currentMode()

         {

                   AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

 

                   switch (audio.getRingerMode())

                   {

                            case AudioManager.RINGER_MODE_SILENT:

                                     if ( mTab == 0 )

                                     {

                                               return R.id.silent01;

                                     }

                                     else

                                     {

                                               return R.id.silent02;

                                     }

 

                            case AudioManager.RINGER_MODE_VIBRATE:

                                     if ( mTab == 0 )

                                     {

                                               return R.id.vibrate01;

                                     }

                                     else

                                     {

                                               return R.id.vibrate02;

                                     }

                   }

 

                   if (audio.shouldVibrate(AudioManager.VIBRATE_TYPE_RINGER))

                   {

                            if ( mTab == 0 )

                            {

                                     return R.id.ring_and_vibrate01;

                            }

                            else

                            {

                                     return R.id.ring_and_vibrate02;

                            }

                   }

                   if ( mTab == 0 )

                   {

                            return R.id.ring01;

                   }

                   else

                   {

                            return R.id.ring02;

                   }

         }

         /***************************************************************/

个人总结:

 

   本周的工作重心主要在编程代码上边。编程是我的较弱项,而且还是用以前未接触过的Android应用开发,着实是一项挑战。对于代码的理解和这项语言的掌握确实还差很多,所以花了不少时间。尤其在我负责的调控音频操作这部分,不仅要通过模仿书中的实例来编写,还要想着尽可能完善漏洞,注意出现没想到的情况,不过在书本与同学的帮助下,还好没有影响进度。但是所编写的操作代码的功能还是比较简陋,而且也少了不少实用的功能,只是把比较简单基础的完成了。还需要更加努力的学习、实践和理解。

当应用程序所设置的时间到了后,系统中的AlarmManagerService就会从系统底层获取一个闹铃,并从自己维护的队列中取出与其匹配的信息,然后通过其应用注册的PendingIntent把该闹铃事件发会给应用.当应用收到该Intent后就会对事件作出相应的处理,比如,更改情景模式为振动,铃声等.因此,需要在应用程序中创建一个接受广播的类RingBroadcastReceive

来处理这些事件。

 

 

“情景模式”具体操作2(刘宇)

 

 

         // 铃声和震动

         protected void ringAndVibrate()

         {

                   Intent intent = new Intent(RingBroadcastReceiver.RV_CHANGED);

                   if ( mTab == 0 )

                   {

                            intent.putExtra("checkedId", R.id.ring_and_vibrate01);

                   }

                   else

                   {

                            intent.putExtra("checkedId", R.id.ring_and_vibrate02);

                   }

                   PendingIntent alarmIntent = PendingIntent.getBroadcast(this, RingBroadcastReceiver.REQUEST_CODE, intent, 0);

                   Log.e(TAG, "" + intent);

                   mAlarmManager.set(AlarmManager.RTC_WAKEUP, getTime(), alarmIntent);

         }

 

 

         // 铃声

         protected void ring()

         {

                   Intent intent = new Intent(RingBroadcastReceiver.RING_CHANGED);

                   if ( mTab == 0 )

                   {

                            intent.putExtra("checkedId", R.id.ring01);

                   }

                   else

                   {

                            intent.putExtra("checkedId", R.id.ring02);

                   }

                   PendingIntent alarmIntent = PendingIntent.getBroadcast(this, RingBroadcastReceiver.REQUEST_CODE, intent, 0);

                   Log.e(TAG, "" + intent);

                   mAlarmManager.set(AlarmManager.RTC_WAKEUP, getTime(), alarmIntent);

         }

 

 

         // 震动

         protected void vibrate()

         {

                   Intent intent = new Intent(RingBroadcastReceiver.VIBRATE_CHANGED);

                   if ( mTab == 0 )

                   {

                            intent.putExtra("checkedId", R.id.vibrate01);

                   }

                   else

                   {

                            intent.putExtra("checkedId", R.id.vibrate02);

                   }

                   PendingIntent alarmIntent = PendingIntent.getBroadcast(this, RingBroadcastReceiver.REQUEST_CODE, intent, 0);

                   Log.e(TAG, "" + intent);

                   mAlarmManager.set(AlarmManager.RTC_WAKEUP, getTime(), alarmIntent);

         }

 

 

         // 静音

         protected void silent()

         {

                   Intent intent = new Intent(RingBroadcastReceiver.SILENT_CHANGED);

                   if ( mTab == 0 )

                   {

                            intent.putExtra("checkedId", R.id.silent01);

                   }

                   else

                   {

                            intent.putExtra("checkedId", R.id.silent02);

                   }

                   PendingIntent alarmIntent = PendingIntent.getBroadcast(this, RingBroadcastReceiver.REQUEST_CODE, intent, 0);

                   Log.e(TAG, "" + intent);

                   mAlarmManager.set(AlarmManager.RTC_WAKEUP, getTime(), alarmIntent);

         }

 

         // 计算切换时间

         private long getTime()

         {

                   Date dateNow = new Date();

                   long hour = mTimePicker.getCurrentHour() - dateNow.getHours();

                   long min = mTimePicker.getCurrentMinute() - dateNow.getMinutes();

                   long second = dateNow.getSeconds();

                   return dateNow.getTime() + (hour * 60 + min) * 60 * 1000 - second * 1000;

         }

 

         /***********************************************************************************/

}

个人总结:

 

这周得工作重点在于仿照书中实例进行代码开发。对基于google android平台的开发,我们还比较陌生,尤其是android

sdk的使用。android sdk是一款强大开发工具包,通过本周的学习,我们已初步掌握Android编程的基本模式,理解Android编程的关键技术,能够完成功能较全面的Android程序,收获很大。

阅读(1521) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~