我正在创建一个应用程序,每隔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)
任何人都可以帮我这个编码。我哪里做错了?
答案 0 :(得分:3)
我最近运行此代码并且工作正常。这可以使用广播Receiver来实现。您必须实现扩展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()”方法的类中实现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()(在您的情况下,您实现广播接收器,取消注册BroadcastReceiver。)
答案 1 :(得分:0)
当您注销时将计时器实例设置为null,然后在每次用户登录应用程序时将其初始化。这将解决“计时器被取消”相关问题。
答案 2 :(得分:0)
为什么你需要一个静态的TimerTask.You可以给出这样的效果,对我来说很好。
timer.schedule(new TimerTask() {
@Override
public void run() {
//your code
}
}, 0, 30000);
使用退出时,timer.cancel().
在这里你可以简单地取消timer.No需要取消TimerTask。