-->

How to cancel the timer and renew the same timer?

2019-08-01 06:28发布

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

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

在我的应用我使用TimerTask这个实施

这是利用振动和蜂鸣代码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); 
}

这是我使用的注销部分代码

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

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

我的震动工作注销之前罚款并重新登录我歌厅错误

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)

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

Answer 1:

我最近运行这段代码并工作正常。 这可以通过使用广播Receiver.You已经实现扩展TimerTask的独立CustomTimer任务来实现:

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:

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

}

现在,你可以启动或执行这行停振:要启动振动:

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

停止:

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

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



Answer 2:

计时器实例设置为null,当你注销并对其进行初始化每次用户登录的应用程序。 这将解决“定时器被取消”相关的问题。



Answer 3:

为什么你需要一个静态TimerTask.You可以给这样这对我来说工作正常。

timer.schedule(new TimerTask() {
        @Override
        public void run() {
        //your code
        }
        }, 0, 30000);

虽然注销使用, timer.cancel(). 在这里,你可以简单地取消timer.No需要取消TimerTask的。



文章来源: How to cancel the timer and renew the same timer?