int值不在的onchange ContentObserver的得到更新 [英] int value not getting updated in onchange of ContentObserver

查看:165
本文介绍了int值不在的onchange ContentObserver的得到更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个Android应用程序,在我的类扩展ContentObserver.I我注册 我的课堂观察中VOLUME_RING的变化。

我的课的OnChange方法得到只有在音量按钮将变为调用。

现在的问题是,这是在类的构造函数得到更新的全局int变量是没有得到中的onchange方法的更新。

在code以下是我已经尽力了,

 公共类VolumeChecker扩展ContentObserver
    {
         上下文语境;
         处理程序处理程序;

         INT initialVolume;


公共VolumeChecker(上下文C,处理程序处理)
{
    超(处理);
    上下文= C;
    this.handler =处理程序;

    AudioManager音频=(AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
    initialVolume = audio.getStreamVolume(AudioManager.STREAM_RING);

    Log.e(inisde,volvhevker  -  intitvol+ initialVolume);

}

@覆盖
公共布尔deliverSelfNotifications()
{
    返回super.deliverSelfNotifications();
}

@覆盖
公共无效的onChange(布尔selfChange)
{

    super.onChange(selfChange);

    Log.e(平变化,initialVolume+ initialVolume);
    刷新();
}


公共无效刷新()
{
    新VolumeChecker(背景下,处理程序);
}

}
 

在initialVolume变量值,这是越来越在构造函数中刷新时更新, 是没有得到体现在Onchange方法。

请help.thanks!

解决方案

什么是创建一个新的 VolumeChecker 刷新的目的()?你可以简单地再像这样更新变量:

 公共无效刷新(){
    AudioManager音频=(AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
    initialVolume = audio.getStreamVolume(AudioManager.STREAM_RING);
}
 

和可能做一些重构,使音频的全局变量,这样你就不必每次都重新创建。你可以很可能重构刷新()方法直接进入的onChange()的方法为好。

i am developing an android app, where my class extends ContentObserver.I am registering my class for observing the changes in VOLUME_RING.

the onchange method of my class gets called only upon volume button changes.

The problem is, an global int variable which is getting updated in the constructor of the class is not getting updates in onchange method.

The code below is what i have tried,

   public class VolumeChecker extends ContentObserver 
    {
         Context context;
         Handler handler;

         int initialVolume;


public VolumeChecker(Context c, Handler handler)
{
    super(handler);
    context=c;
    this.handler = handler;

    AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    initialVolume = audio.getStreamVolume(AudioManager.STREAM_RING);

    Log.e("inisde","volvhevker - intitvol " + initialVolume);

}

@Override
public boolean deliverSelfNotifications()
{
    return super.deliverSelfNotifications();
}

@Override
public void onChange(boolean selfChange) 
{

    super.onChange(selfChange);

    Log.e("onchange","initialVolume" + initialVolume);
    refresh();
}


public void refresh()
{
    new VolumeChecker(context,handler);
}

}

The initialVolume variable value, which is getting updated in the constructor upon refresh, is not getting reflected in the onchange method.

Please help.thanks!

解决方案

What is the purpose of creating a new VolumeChecker within refresh()? You can simply update the variable again like so:

public void refresh() {
    AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    initialVolume = audio.getStreamVolume(AudioManager.STREAM_RING);
}

and perhaps do some refactoring to make audio a global variable so that you do not have to recreate it each time. You can likely refactor the contents of the refresh() method directly into the onChange() method as well.

这篇关于int值不在的onchange ContentObserver的得到更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆