android-如何取消计时器并更新相同的计时器?

我正在创建一个每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)

谁能帮我这个编码.我哪里做错了?

解决方法:

我最近运行了此代码,并且工作正常.这可以使用广播Receiver来实现.您必须实现单独的CustomTimer任务来扩展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()”方法的类中实现BroadCast Receive ::
可以说,就我而言(例如)是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()(在您的情况下,在实现Broadcast Receiver的地方,取消注册BroadcastReceiver.)

上一篇:我可以在c#中使用Console.Beep播放mp3文件吗?


下一篇:单片机之振动传感器