ToneGenerator+MessageHandler用法

[code]
package android.demo.dialtone;

import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.ToneGenerator;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class DialTone extends Activity implements OnClickListener
{
/** Called when the activity is first created. */
private final String TAG="DialTone";
private static final int PLAY_TONE=0x01;
private static final int DTMF_DURATION_MS = 120;
private Object mToneGeneratorLock = new Object();
private ToneGenerator mToneGenerator;
private static boolean mDTMFToneEnabled;
private static final HashMap<String,Integer> mToneMap = new HashMap<String,Integer>();


static
{
mToneMap.put("1", ToneGenerator.TONE_DTMF_1);
mToneMap.put("2", ToneGenerator.TONE_DTMF_2);
mToneMap.put("3", ToneGenerator.TONE_DTMF_3);
mToneMap.put("4", ToneGenerator.TONE_DTMF_4);
mToneMap.put("5", ToneGenerator.TONE_DTMF_5);
mToneMap.put("6", ToneGenerator.TONE_DTMF_6);
mToneMap.put("7", ToneGenerator.TONE_DTMF_7);
mToneMap.put("8", ToneGenerator.TONE_DTMF_8);
mToneMap.put("9", ToneGenerator.TONE_DTMF_9);
mToneMap.put("0", ToneGenerator.TONE_DTMF_0);
mToneMap.put("#", ToneGenerator.TONE_DTMF_P);
mToneMap.put("*", ToneGenerator.TONE_DTMF_S);

}


@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d(TAG,"onCreate");
try
{
//
mDTMFToneEnabled = Settings.System.getInt(getContentResolver(),Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
synchronized (mToneGeneratorLock)
{
if (mDTMFToneEnabled && mToneGenerator == null)
{
mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF, 80); //設定聲音大小
setVolumeControlStream(AudioManager.STREAM_DTMF);
}
}
} catch (Exception e)
{
Log.d(TAG, e.getMessage());
mDTMFToneEnabled = false;
mToneGenerator = null;
}
Button button01 = (Button)findViewById(R.id.Button01);
Button button02 = (Button)findViewById(R.id.Button02);
Button button03 = (Button)findViewById(R.id.Button03);
Button button04 = (Button)findViewById(R.id.Button04);
Button button05 = (Button)findViewById(R.id.Button05);
Button button06 = (Button)findViewById(R.id.Button06);
Button button07 = (Button)findViewById(R.id.Button07);
Button button08 = (Button)findViewById(R.id.Button08);
Button button09 = (Button)findViewById(R.id.Button09);


button01.setOnClickListener(this);
button02.setOnClickListener(this);
button03.setOnClickListener(this);
button04.setOnClickListener(this);
button05.setOnClickListener(this);
button06.setOnClickListener(this);
button07.setOnClickListener(this);
button08.setOnClickListener(this);
button09.setOnClickListener(this);



}


@Override
public void onClick(View v)
{
// TODO Auto-generated method stub

int id=v.getId();
Integer currentTone=null; //GetCurrent Tone
switch(id)
{
case R.id.Button01:
Log.d(TAG,"onClick Button01");
currentTone=mToneMap.get("1");
break;
case R.id.Button02:
Log.d(TAG,"onClick Button02");
currentTone=(Integer)mToneMap.get("2");
break;
case R.id.Button03:
Log.d(TAG,"onClick Button03");
currentTone=(Integer)mToneMap.get("3");
break;
case R.id.Button04:
Log.d(TAG,"onClick Button04");
currentTone=(Integer)mToneMap.get("4");
break;
case R.id.Button05:
Log.d(TAG,"onClick Button05");
currentTone=(Integer)mToneMap.get("5");
break;
case R.id.Button06:
Log.d(TAG,"onClick Button06");
currentTone=(Integer)mToneMap.get("6");
break;
case R.id.Button07:
Log.d(TAG,"onClick Button07");
currentTone=(Integer)mToneMap.get("7");
break;
case R.id.Button08:
Log.d(TAG,"onClick Button08");
currentTone=(Integer)mToneMap.get("8");
break;
case R.id.Button09:
Log.d(TAG,"onClick Button09");
currentTone=(Integer)mToneMap.get("9");
break;
}

if (null != currentTone)
{
//
Log.d(TAG,"currentTone="+currentTone.intValue());
Message msg = mHandler.obtainMessage(PLAY_TONE, currentTone.intValue());
mHandler.sendMessage(msg);
//
//playTone(currentTone);
}else
{
Log.d(TAG,"!!!!!!");
}
}

private Handler mHandler= new Handler()
{
public void handleMessage(Message msg)
{
try
{
switch(msg.what)
{
case PLAY_TONE:
Integer tone_id=(Integer)msg.obj;
if(tone_id!=null)
{
PlayTone(tone_id.intValue());
}
break;
}
}catch(Exception e)
{

}
}
};
private void PlayTone(int tone)
{
if(!mDTMFToneEnabled) return;
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int ringerMode = audioManager.getRingerMode();
if (ringerMode == AudioManager.RINGER_MODE_SILENT||
ringerMode == AudioManager.RINGER_MODE_VIBRATE)
{ //靜音或是震動則不發出聲音
return;
}
synchronized(mToneGeneratorLock)
{
if(mToneGenerator==null)
{
return;
}
mToneGenerator.startTone(tone,DTMF_DURATION_MS);
}
}
}

//=================================================================
<?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"
/>
<TableLayout android:id="@+id/TableLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TableRow android:id="@+id/TableRow01" android:layout_width="wrap_content" android:layout_height="wrap_content">
<Button android:text="Digit1" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="Digit2" android:id="@+id/Button02" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="Digit3" android:id="@+id/Button03" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>

</TableRow>
<TableRow android:id="@+id/TableRow02" android:layout_width="wrap_content" android:layout_height="wrap_content">
<Button android:text="Digit4" android:id="@+id/Button04" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="Digit5" android:id="@+id/Button05" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="Digit6" android:id="@+id/Button06" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</TableRow>
<TableRow android:id="@+id/TableRow03" android:layout_width="wrap_content" android:layout_height="wrap_content">
<Button android:text="Digit7" android:id="@+id/Button07" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="Digit8" android:id="@+id/Button08" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="Digit9" android:id="@+id/Button09" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</TableRow>
</TableLayout>
</LinearLayout>
[/code]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值