2011年4月1日星期五

檢查鈴聲模式

要檢查鈴聲模式: 正常,振動,靜音; 可以使用 AudioManager.

調用 getSystemService(AUDIO_SERVICE) 方法, 獲取 audioManager 對象, 再調用 audioManager 對象的 getRingerMode() 方法便可檢查目前的鈴聲模式.



package com.AndroidRinger;

import android.app.Activity;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class AndroidRinger extends Activity {

private AudioManager audioManager;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonRinger = (Button)findViewById(R.id.buttonringer);
final TextView textRingerState = (TextView)findViewById(R.id.textringerstate);

audioManager = (AudioManager)getSystemService(AUDIO_SERVICE);

buttonRinger.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
int ringerMode = audioManager.getRingerMode();
if(ringerMode == AudioManager.RINGER_MODE_SILENT){
textRingerState.setText("Ringer is Silent");
}else if (ringerMode == AudioManager.RINGER_MODE_VIBRATE){
textRingerState.setText("Ringer is Vibrate");
}else{
textRingerState.setText("Ringer is Normal");
}
}});

}
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/buttonringer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Check Ringer Mode"
/>
<TextView
android:id="@+id/textringerstate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>


相關文章:
- 更改鈴聲模式, AudioManager.setRingerMode()

沒有留言:

發佈留言