編輯:中級開發
// 鈴聲
456. protected void ring()
457. {
458. Intent intent = new Intent(RingBroadcastReceiver.RV_CHANGED);
459.
460. if (mTab == 0)
461. {
462. intent.putExtra("checkedId", R.id.ring01);
463. }
464. else
465. {
466. intent.putExtra("checkedId", R.id.ring02);
467. }
468.
469. PendingIntent alarmIntent = PendingIntent.getBroadcast(this, RingBroadcastReceiver.REQUEST_CODE, intent, 0);
470.
471. Log.e(TAG, " " + intent);
472.
473. mAlarmManager.set(AlarmManager.RTC_WAKEUP, getTime(), alarmIntent);
474. }
475.
476. // 震動
477. protected void vibrate()
478. {
479. Intent intent = new Intent(RingBroadcastReceiver.RV_CHANGED);
480.
481. if (mTab == 0)
482. {
483. intent.putExtra("checkedId", R.id.vibrate01);
484. }
485. else
486. {
487. intent.putExtra("checkedId", R.id.vibrate02);
488. }
489.
490. PendingIntent alarmIntent = PendingIntent.getBroadcast(this, RingBroadcastReceiver.REQUEST_CODE, intent, 0);
491.
492. Log.e(TAG, " " + intent);
493.
494. mAlarmManager.set(AlarmManager.RTC_WAKEUP, getTime(), alarmIntent);
495. }
496.
497. // 靜音
498. protected void silent()
499. {
500. Intent intent = new Intent(RingBroadcastReceiver.RV_CHANGED);
501.
502. if (mTab == 0)
503. {
504. intent.putExtra("checkedId", R.id.silent01);
505. }
506. else
507. {
508. intent.putExtra("checkedId", R.id.silent02);
509. }
510.
511. PendingIntent alarmIntent = PendingIntent.getBroadcast(this, RingBroadcastReceiver.REQUEST_CODE, intent, 0);
512.
513. Log.e(TAG, " " + intent);
514.
515. mAlarmManager.set(AlarmManager.RTC_WAKEUP, getTime(), alarmIntent);
516. }
517.
518. // 計算切換時間
519. private long getTime()
520. {
521. Date dateNow = new Date();
522. long hour = mTimePicker.getCurrentHour() - dateNow.getHours();
523. long min = mTimePicker.getCurrentMinute() - dateNow.getMinutes();
524. long second = dateNow.getSeconds();
525.
526. return dateNow.getTime() + (hour * 60 + min) * 60 * 1000 - second * 1000;
527. }
528.}
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);
}
本文節選於機械工業出版社推出的《Android應用開發揭秘》一書,作者為楊豐盛。本書內容全面,詳細講解了Android框架、android組件、用戶界面開發、游戲開發、
簡介: 在這個由五個部分所組成的系列的第一部分中,您將接觸到移動 Web 應用程序中最流行的新技術:地理定位。高端智能手機都內置 GPS,現在您將了解 Web
簡介: Java™ 語言是 Android 開發人員所選的工具。android 運行時使用自己的虛擬機 Dalvik,這並不是多數程序開發人員使用
簡介: HTML 5 針對移動 Web 應用程序引入了大量新特性,其中包括一些可視化特性,它們通常會帶來強烈的視覺沖擊。Canvas 是最引人注目的新 UI