本文主要是介绍Android Hander 通信,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在某个Activity的onCreate()方法中添加如下方法
setVolumeControlStream(AudioManager.STREAM_MUSIC);
可以达到提示静音
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
(1)public AudioService(Context context) {......
createAudioSystemThread();
......
}
(2)private void createAudioSystemThread() {
mAudioSystemThread = new AudioSystemThread();
mAudioSystemThread.start();
waitForAudioHandlerCreation();
}
/** Waits for the volume handler to be created by the other thread. */
(3)private void waitForAudioHandlerCreation() {
synchronized(this) {
while (mAudioHandler == null) {
try {
// Wait for mAudioHandler to be set by the other thread
wait();
} catch (InterruptedException e) {
Log.e(TAG, "Interrupted while waiting on volume handler.");
}
}
}
}
(4)/** Thread that handles native AudioSystem control. */
private class AudioSystemThread extends Thread {
AudioSystemThread() {
super("AudioService");
}
@Override
public void run() {
// Set this thread up so the handler will work on it
Looper.prepare();
synchronized(AudioService.this) {
mAudioHandler = new AudioHandler();
// Notify that the handler has been created
AudioService.this.notify();
}
// Listen for volume change requests that are set by VolumePanel
Looper.loop();
}
(5) /** Handles internal volume messages in separate volume thread. */
private class myHandler extends Handler {
private void userDefinedfuntionTest1() {
........
// Post a persist msg
sendMsg();
}
private void userDefinedfuntionTest2() {
.......
// Post a persist msg
sendMsg();
}
private void userDefinedfuntionTest1() {
........
}
private void userDefinedfuntionTest2() {
.......
}
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_ONE:
break;
case MSG_TWO:
break;
case MSG_THREE:
break;
case MSG_FOURCE:
break;
case MSG_SIX:
break;
}
}
}
(6)private static void sendMsg(Handler handler, int msg, int existingMsgPolicy, int arg1, int arg2, Object obj, int delay) {
if (existingMsgPolicy == SENDMSG_REPLACE) {
handler.removeMessages(msg);
} else if (existingMsgPolicy == SENDMSG_NOOP && handler.hasMessages(msg)) {
Log.d(TAG, "sendMsg: Msg " + msg + " existed!");
return;
}
handler.sendMessageDelayed(handler.obtainMessage(msg, arg1, arg2, obj), delay);
}
(7)Handler的sendMessageDelayed (Message msg, long delayMillis) 说明:
public final boolean sendMessageDelayed (Message msg, long delayMillis)
Added in API level 1
Enqueue a message into the message queue after all pending messages before (current time + delayMillis).
You will receive it in handleMessage(Message), in the thread attached to this handler.
Returns
Returns true if the message was successfully placed in to the message queue.
Returns false on failure, usually because the looper processing the message queue is exiting.
Note that a result of true does not mean the message will be processed --
if the looper is quit before the delivery time of the message occurs then the message will be dropped.
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
JNI调用方法:
(1)在AudioSystem.java中添加
/**
* @hide
*/
public static native int setMasterVolume(int vol);
(2)在android_media_AudioSystem.cpp中添加如下信息:
a)
static int
android_media_AudioSystem_setMasterVolume(JNIEnv *env, jobject thiz, jint vol)
{
return check_AudioSystem_Command(AudioSystem::setMasterVolume((float)vol / 100));
}
b) static JNINativeMethod gMethods[] = {
{"setParameters", "(Ljava/lang/String;)I", (void *)android_media_AudioSystem_setParameters},
{"getParameters", "(Ljava/lang/String;)Ljava/lang/String;", (void *)android_media_AudioSystem_getParameters},
......
{"setMasterVolume", "(I)I", (void*)android_media_AudioSystem_setMasterVolume},
};
(3) 在AudioSystem.h和AudioSystem.cpp中声明和实现
static status_t setMasterVolume(float value);
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
这篇关于Android Hander 通信的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!