android:线程(时间)在服务中

时间:2014-08-11 07:00:53

标签: android service

我想创建一个在服务中运行的线程(时间)。这是运行服务时我的服务代码:

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;            
}
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){
                }
            }
    }
}
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) {}
        }
    });
}

我有一些错误,runOnUiThreadsetClass如何解决这个问题?我想在分钟> 30时,serv活动会出现。

0 个答案:

没有答案