package com.examples.android;
import java.util.Date;
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 {
/** Called when the activity is first created. */
private static final String TAG = "RingToggle";
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);
//标签切换事件处理,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();
}
}
});
RadioGroup group1 = (RadioGroup) findViewById(R.id.RadioGroup01);
group1.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
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 (null != radio)
{
radio.setTextSize(30);
}
}
});
//RadioButton添加监听器
for (int i = 0, l = group1.getChildCount(); i < l; i++)
{
RadioButton radio = (RadioButton) group1.getChildAt(i);
radio.setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
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 (null != radio)
{
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));
}
//降低音量按键
downButton.setOnClickListener(new Button.OnClickListener()
{
public void onClick (View arg0)
{
//adjustVolume可以增加和降低音量
audioMa.adjustVolume(AudioManager.ADJUST_LOWER, 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));
}
}
});
//提高音量
upButton.setOnClickListener(new Button.OnClickListener()
{
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()
{
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()
{
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()
{
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;
}
}
// 铃声和震动
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.RV_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.RV_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.RV_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;
}
}
package com.examples.android;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.util.Log;
public class RingBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "RingBroadcastReceiver";
public static final String VIBRATE_CHANGED = "com.examples.android.VIBRATE_CHANGED";
public static final String SILENT_CHANGED = "com.examples.android.SILENT_CHANGED";
public static final String RV_CHANGED = "com.examples.android.RV_CHANGED";
public static final String RING_CHANGED = "com.examples.android.RING_CHANGED";
public static final int REQUEST_CODE = 0;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
int checkedId = intent.getIntExtra("checkedId", 0);
Log.e(TAG, checkedId + intent.getAction());
// 切换情景模式
switch (checkedId)
{
case R.id.ring_and_vibrate01:
case R.id.ring_and_vibrate02:
ringAndVibrate(audio);
break;
case R.id.vibrate01:
case R.id.vibrate02:
vibrate(audio);
break;
case R.id.silent01:
case R.id.silent02:
silent(audio);
break;
default:
ring(audio);
break;
}
}
// 铃声和震动
protected void ringAndVibrate(AudioManager audio)
{
audio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
audio.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_ON);
audio.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION, AudioManager.VIBRATE_SETTING_ON);
}
// 铃声
protected void ring(AudioManager audio)
{
audio.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
audio.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_OFF);
audio.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION, AudioManager.VIBRATE_SETTING_ON);
}
// 震动
protected void vibrate(AudioManager audio)
{
audio.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
audio.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_ON);
audio.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION, AudioManager.VIBRATE_SETTING_ON);
}
// 静音
protected void silent(AudioManager audio)
{
audio.setRingerMode(AudioManager.RINGER_MODE_SILENT);
audio.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_OFF);
audio.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION, AudioManager.VIBRATE_SETTING_OFF);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RadioGroup
android:id="@+id/RadioGroup01"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<RadioButton
android:text="@string/ring_and_vibrate"
android:id="@+id/ring_and_vibrate01"
android:layout_width="fill_parent"
android:textSize="24sp"
android:paddingLeft="50sp"/>
<RadioButton
android:text="@string/ring"
android:id="@+id/ring01"
android:layout_width="fill_parent"
android:textSize="24sp"
android:paddingLeft="50sp"
android:paddingRight="50sp"/>
<RadioButton
android:text="@string/vibrate"
android:id="@+id/vibrate01"
android:layout_width="fill_parent"
android:textSize="24sp"
android:paddingLeft="50sp"
android:paddingRight="50sp"/>
<RadioButton
android:text="@string/silent"
android:id="@+id/silent01"
android:layout_width="fill_parent"
android:textSize="24sp"
android:paddingLeft="50sp"
android:paddingRight="50sp"/>
</RadioGroup>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout01"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="12sp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/help"
android:textSize="20px"
android:textStyle="bold"
android:id="@+id/help"/>
<TimePicker
android:id="@+id/timePkr"
android:layout_below="@id/help"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioGroup
android:orientation="vertical"
android:id="@+id/RadioGroup02"
android:layout_below="@id/timePkr"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerInParent="true">
<RadioButton
android:text="@string/ring_and_vibrate"
android:id="@+id/ring_and_vibrate02"
android:layout_width="fill_parent"
android:textSize="24sp"
android:paddingLeft="50sp"/>
<RadioButton
android:text="@string/ring"
android:id="@+id/ring02"
android:layout_width="fill_parent"
android:textSize="24sp"
android:paddingLeft="50sp"
android:paddingRight="50sp"/>
<RadioButton
android:text="@string/vibrate"
android:id="@+id/vibrate02"
android:layout_width="fill_parent"
android:textSize="24sp"
android:paddingLeft="50sp"
android:paddingRight="50sp"/>
<RadioButton
android:text="@string/silent"
android:id="@+id/silent02"
android:layout_width="fill_parent"
android:textSize="24sp"
android:paddingLeft="50sp"
android:paddingRight="50sp"/>
</RadioGroup>
</RelativeLayout>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/AbsoluteLayout03"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/myText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="情景模式:"
android:textSize="16sp"
android:layout_x="20px"
android:layout_y="42px"/>
<ImageView
android:id="@+id/myImage"
android:layout_width="48px"
android:layout_height="48px"
android:layout_x="110px"
android:layout_y="32px"/>
<TextView
android:id="@+id/myText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="声音音量:"
android:textSize="16sp"
android:layout_x="20px"
android:layout_y="102px"/>
<ProgressBar
android:id="@+id/myProgress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="160dip"
android:layout_height="wrap_content"
android:max="7"
android:progress="5"
android:layout_x="110px"
android:layout_y="102px"/>
<ImageButton
android:id="@+id/downButton"
android:layout_width="100px"
android:layout_height="100px"
android:layout_x="50px"
android:layout_y="162px"
android:src="@drawable/down"/>
<ImageButton
android:id="@+id/upButton"
android:layout_width="100px"
android:layout_height="100px"
android:layout_x="150px"
android:layout_y="162px"
android:src="@drawable/up"/>
<ImageButton
android:id="@+id/normalButton"
android:layout_width="60px"
android:layout_height="60px"
android:layout_x="50px"
android:layout_y="272px"
android:src="@drawable/icon"/>
<ImageButton
android:id="@+id/muteButton"
android:layout_width="60px"
android:layout_height="60px"
android:layout_x="120px"
android:layout_y="272px"
android:src="@drawable/mute"/>
<ImageButton
android:id="@+id/vibrateButton"
android:layout_width="60px"
android:layout_height="60px"
android:layout_x="190px"
android:layout_y="272px"
android:src="@drawable/vibrate"/>
</AbsoluteLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, RingProfile!</string>
<string name="app_name">《情景模式》</string>
<string name="ring_and_vibrate">铃声和振动</string>
<string name="ring">铃声</string>
<string name="vibrate">振动</string>
<string name="silent">静音</string>
<string name="help">设置时间:</string>
</resources>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.examples.android"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".RingProfile"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="RingBroadcastReceiver">
<intent-filter>
<action android:name="com.examples.android.RV_CHANGED" />
<action android:name="com.examples.android.RING_CHANGED" />
<action android:name="com.examples.android.VIBRATE_CHANGED" />
<action android:name="com.examples.android.SILENT_CHANGED" />
</intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="5" />
</manifest>