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);

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值