我正在创建一个每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.)
以上是内存溢出为你收集整理的android-如何取消计时器并更新相同的计时器?全部内容,希望文章能够帮你解决android-如何取消计时器并更新相同的计时器?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)