Reputation: 1407
using my Broadcast receiver, I'm setting a shared preference (BT_Device_connected), whenever a Bluetooth device is connected.
now, the app i'm writing turns on Bluetooth automatically when you plug in the phone (yes, i know it's not recommended. focus! ;).
what i'm trying to do is wait 2 minutes after the phone has been plugged in, and check to see if during those two minutes the phone has connected to a Bluetooth Device (if BT_device_connected is true, it has, if false, it hasn't).
so, i'm trying to launch a timer/handler from within one service, and wait 2 minutes, and then either launch a new service, or a method that will check if the device is connected.
and... i have to do all this in the Background, since none of this actually affects ui.
the glorious code i have, which doesn't work, is as follows:
public void onDeviceConnectTimer(){
TimerTask wait;
final Handler handler = new Handler();
Timer t = new Timer();
wait = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
Intent serviceIntent = new Intent(CheckIfDeviceConnectService.class.getName());
getApplicationContext().startService(serviceIntent);
Log.d("TIMER", "Timer set off");
}
});
}};
t.schedule(wait, initializeDate());
}
What am I doing wrong, and is there a better way to do this?
Upvotes: 0
Views: 132
Reputation: 6588
Try using handler
like this:
final Handler handler = new Handler();
Runnable myRunnable=new Runnable()
{
public void run()
{
//Start sevice here.
}
};
Now start runnable by:
handler.postDelayed(myRunnable, 120000);
This will start your service after 2 minutes.
Upvotes: 2