如何取消计时器并更新同一个定时器? [英] How to cancel the timer and renew the same timer?

查看:388
本文介绍了如何取消计时器并更新同一个定时器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个应用程序,振动并发出蜂鸣声,每30秒,当我退出的振动并发出蜂鸣声必须取消,当我登录的振动和蜂鸣应该恢复。

I am creating an app that vibrate and beep every 30 sec and when I log out the vibrate and beep must be cancelled and when I log in the vibrate and beep should resume.

注:必须振动并发出蜂鸣声,每30秒,直到我退出

NOTE: it must vibrate and beep for every 30 sec until I log out

在我的应用程序,我使用的TimerTask 此实施

In my app I am using TimerTask for this implementation

这是在code为振动并发出蜂鸣声使用的TimerTask

this is the code for vibrate and beep using TimerTask

static TimerTask Task;
final static Handler handler = new Handler();
static Timer t = new Timer();

public static void vib() {

    Task = new TimerTask() {
        public void run() {
            handler.post(new Runnable() {
                public void run() {
                    Vibrator vibrator = (Vibrator) ApplicationUtils.getContext().getSystemService(Context.VIBRATOR_SERVICE);
                    vibrator.vibrate(3000);
                    playSound();
                    Log.d("TIMER", "Timer set on");
                }
            });
        }
    };
    t.schedule(Task, 0, 30000); 
}

这是注销部分code我用

This is the code I'm using in logout section

public void stopvib() {
    if (Task != null) {
    //  Log.d("TIMER", "timer canceled");
        t.cancel();
        Task.cancel();
    }
}

注:我也删除了 Task.cancel(); 但还是我收到同样的错误

Note: I also removed the Task.cancel(); but still I am getting same error

我的震动做工精细注销前重新登录我歌厅错误

My vibrate working fine before logout and again login I am geting error

java.lang.IllegalStateException: Timer was cancelled
    at java.util.Timer.scheduleImpl(Timer.java:562)
    at java.util.Timer.schedule(Timer.java:481)
    at com.vib(AlertListActivity.java:724)

任何一个可以帮我这个编码。我哪里做错了?

can any one help me with this coding. Where did I go wrong?

推荐答案

我最近运行此code和工作正常。这可以通过使用广播Receiver.You实现必须实行独立的CustomTimer任务,扩展TimerTask的:

i have recently run this code and is working fine. This can be achieved using broadcast Receiver.You have to implement separate CustomTimer task that extend TimerTask:

Activity mActivity=null;
public MyCustomTimer(Activity mActivity) {
    this.mActivity=mActivity;
}
    @Override
    public void run() {
        this.mActivity.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                Toast.makeText(mActivity, "Write you code here",Toast.LENGTH_LONG).show();
                Log.d("MyCustomTimer","Call");
            }
        });

    }

在此,你必须实现广播要实现VIB()方法的类接收: 让说,在我的情况(只是举例)是MainActivity:

After this you have to implement BroadCast Receive in that class where you want to implement " vib() " method.: Let say, in my case (just for example ) is MainActivity:

public class MainActivity extends Activity {
    private MyCustomTimer myCustomTimer = null;
    BroadcastReceiver mBr_Start = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals("START_VIBRATION")) {
                System.out.println("onreceive :START_VIBRATION");
                vib();
            }
        }
    };
    BroadcastReceiver mBr_Stop = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals("STOP_VIBRATION")) {
                stopVibration();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        IntentFilter mIntentFilter = new IntentFilter();
        mIntentFilter.addAction("START_VIBRATION");
        registerReceiver(mBr_Start, mIntentFilter);
        IntentFilter mIntentFilter2 = new IntentFilter();
        mIntentFilter2.addAction("STOP_VIBRATION");
        registerReceiver(mBr_Stop, mIntentFilter2);

        Button b1 = (Button) findViewById(R.id.button1);
        b1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this, MySecondActivity.class)
                        .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(i);

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    private void vib() {
        myCustomTimer = new MyCustomTimer(MainActivity.this);
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(myCustomTimer, 0, 30000);
    }

    private void stopVibration() {
        Log.d("MainActivity", "Before Cancel");
        if (null != myCustomTimer)
            myCustomTimer.cancel();
        Log.d("MainActivity", "After Cancel");
    }

}

现在,你可以启动或通过实施这些行停振: 要启动振动:

Now,you can start Or stop vibration by implementing these lines: To start vibration:

Intent i=new Intent("START_VIBRATION");
                mActivity.sendBroadcast(i);

要停止:

Intent i=new Intent("STOP_VIBRATION");
                mActivity.sendBroadcast(i);

请注意: 的onDestroy()MainActivity的(在你的情况,你在哪里实现广播接收器,注销的BroadcastReceiver。)

Note: onDestroy() of MainActivity (in your case,Where you implement Broadcast Receiver,unregister BroadcastReceiver.)

这篇关于如何取消计时器并更新同一个定时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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