Android : android: thread(time) in service

on Monday, August 11, 2014


I want to create a thread(time) run in service. Here's my code in service when service is run:



public int onStartCommand(Intent intent, int flags, int startId) {

Toast.makeText(this, "The Service Started", Toast.LENGTH_LONG).show();

Thread myThread = null;
Runnable myRunnableThread = new CountDownRunner();
myThread= new Thread(myRunnableThread);
myThread.start();
return START_STICKY;
}


in CountDownRunner():



class CountDownRunner implements Runnable{
// @Override
public void run() {
while(!Thread.currentThread().isInterrupted()){
try {
doWork();
Thread.sleep(1000); // Pause of 1 Second

} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}catch(Exception e){
}
}
}
}


in doWork:



public void doWork() {
runOnUiThread(new Runnable() { //error
public void run() {
try{

Date dt = new Date();
int minutes = dt.getMinutes();

if (minutes>30){

Intent i = new Intent();
i.setClass(this, serv.class); //error
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}else{
//if serv activity is show up, close the activities
}

}catch (Exception e) {}
}
});
}


I have some error, runOnUiThread and setClass how to fix that?. I want to when minutes>30, serv activities will show up . .


0 comments:

Post a Comment