2

我正在开发一个 android 应用程序,我的课程在其中扩展了内容观察者。我试图在按下音量减小按钮时触发计时器。按下 down 方法后,我在计时器方法中强制关闭。我需要实现计时器方法。

下面是我的日志:

07-01 12:14:08.929: E/Service(10564): <!>com.ssn.testingaudiomanager.Serviceforshake 19<!> Service
07-01 12:14:14.079: E/changed(10564): <!>com.ssn.testingaudiomanager.VolumeChecker 47<!> Volume6
07-01 12:14:14.089: E/delta(10564): <!>com.ssn.testingaudiomanager.VolumeChecker 51<!> delta value 1
07-01 12:14:14.089: E/delta down & up(10564): <!>com.ssn.testingaudiomanager.VolumeChecker 55<!> delta > 0 1
07-01 12:14:14.089: E/inside(10564): <!>com.ssn.testingaudiomanager.VolumeChecker 78<!> timeemwthod
07-01 12:14:14.099: E/inside(10564): <!>com.ssn.testingaudiomanager.VolumeChecker$1 89<!> time run , time is0.0
07-01 12:14:14.099: E/inside(10564): <!>com.ssn.testingaudiomanager.VolumeChecker$1 94<!> time less than 2
07-01 12:14:14.149: E/changed(10564): <!>com.ssn.testingaudiomanager.VolumeChecker 47<!> Volume6
07-01 12:14:14.149: E/delta(10564): <!>com.ssn.testingaudiomanager.VolumeChecker 51<!> delta value 1
07-01 12:14:14.149: E/delta down & up(10564): <!>com.ssn.testingaudiomanager.VolumeChecker 55<!> delta > 0 1
07-01 12:14:14.159: E/inside(10564): <!>com.ssn.testingaudiomanager.VolumeChecker 78<!> timeemwthod
07-01 12:14:14.159: E/AndroidRuntime(10564): FATAL EXCEPTION: main
07-01 12:14:14.159: E/AndroidRuntime(10564): java.lang.IllegalStateException: Timer was canceled
07-01 12:14:14.159: E/AndroidRuntime(10564):    at java.util.Timer.scheduleImpl(Timer.java:563)
07-01 12:14:14.159: E/AndroidRuntime(10564):    at java.util.Timer.scheduleAtFixedRate(Timer.java:530)
07-01 12:14:14.159: E/AndroidRuntime(10564):    at com.ssn.testingaudiomanager.VolumeChecker.timermethod(VolumeChecker.java:80)
07-01 12:14:14.159: E/AndroidRuntime(10564):    at com.ssn.testingaudiomanager.VolumeChecker.onChange(VolumeChecker.java:57)
07-01 12:14:14.159: E/AndroidRuntime(10564):    at android.database.ContentObserver$NotificationRunnable.run(ContentObserver.java:43)
07-01 12:14:14.159: E/AndroidRuntime(10564):    at android.os.Handler.handleCallback(Handler.java:587)
07-01 12:14:14.159: E/AndroidRuntime(10564):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-01 12:14:14.159: E/AndroidRuntime(10564):    at android.os.Looper.loop(Looper.java:123)
07-01 12:14:14.159: E/AndroidRuntime(10564):    at android.app.ActivityThread.main(ActivityThread.java:3729)
07-01 12:14:14.159: E/AndroidRuntime(10564):    at java.lang.reflect.Method.invokeNative(Native Method)
07-01 12:14:14.159: E/AndroidRuntime(10564):    at java.lang.reflect.Method.invoke(Method.java:507)
07-01 12:14:14.159: E/AndroidRuntime(10564):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:874)
07-01 12:14:14.159: E/AndroidRuntime(10564):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:632)
07-01 12:14:14.159: E/AndroidRuntime(10564):    at dalvik.system.NativeStart.main(Native Method)

请看下面我的代码。

 public class VolumeChecker extends ContentObserver 
   {
  int initialVolume;
  Context context;
  Timer timer;
  float time = 0;

  String downpress = "no";

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

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

    timer = new Timer();

}

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

@Override
public void onChange(boolean selfChange) 
{
    super.onChange(selfChange);

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

    Log.e("changed", "Volume"+currentVolume);

    int delta=initialVolume-currentVolume;

    Log.e("delta","delta value "+delta);

    if(delta>0)
    {
        Log.e("delta down & up","delta > 0 "+delta);

        timermethod();

    }

    else if(delta<0)
    {
        Log.e("delta up & down","delta < 0 "+delta);
    }

    else if(delta == 0)
    {
        Log.e("inisde","delta is 0 , shake it!!");

    }
}



public void timermethod()
{

    Log.e("inside","timeemwthod");

    timer.scheduleAtFixedRate(new TimerTask() 
    {

        @Override
        public void run() 
        {
            time = time++;


            Log.e("inside","time run , time is" + time);

            if(time <= 2)
            {

                Log.e("inside","time less than 2");

                timer.purge();
                timer.cancel();
            }

            if(time > 2)
            {

                Log.e("inside","time greater than 2");

                timer.purge();
                timer.cancel();
            }

        }



    }, 0, 1000);
}

} 
4

0 回答 0