主要是贴monitor的代码,才能判断,大哥。

解决方案 »

  1.   

    mainActivity.monitor(Xservice.this); 和monitor(Xservice.this); 都不是同一个方法吧?
    在哪里开启的线程?
      

  2.   


    public void monitor(final Context context){    
        
         getRunningProcessInfo(context);
        
         Looper.prepare();
         final Handler handler = new Handler();  
            Runnable runnable = new Runnable(){
             private String M;
             SimpleDateFormat formatter =new SimpleDateFormat ("yyyy-MM-dd  HH:mm:ss");    
             Date curDate;           
             String date;   
            
            
                @Override  
                public void run() {  
                    // TODO Auto-generated method stub              
                    //获取当前时间                          
                    curDate = new Date(System.currentTimeMillis());
                    date = formatter.format(curDate) + "\n";
                    outInfo("\n" + date);                               
                    
                    for(int i=1; UID[i]>0; i++){
                     newFlow[i] = TrafficStats.getUidRxBytes(UID[i])/1024;
                     usedFlow[i] = 0;
                     
                     if((newFlow[i]>oldFlow[i])){
                     usedFlow[i] = newFlow[i] - oldFlow[i];
                     oldFlow[i] = newFlow[i];
                     
                     M = String.valueOf(usedFlow[i])+"KB     ";
                     outInfo(M);
                     outInfo(log[i]);
                     }
                    }
                    
                    getRunningProcessInfo(context);
                    
                    for(int j=1; j<packageName.length-1; j++){
                     if(packageName[j]!=""){
                       if(isTopActivity(context, packageName[j])){
                       outInfo("foregroundTask=" + packageName[j] + "\n");                
                       }
                     }
                    }
                    
                    handler.postDelayed(this, time*1000);// 10000是延时时长
                }   
            };   
            handler.postDelayed(runnable, time*1000);// 打开定时器,执行操作  
            //handler.removeCallbacks(this);// 关闭定时器处理      
        }
    这里就是monitor里的代码,求解啊
      

  3.   


    这两个是同样的代码   一个是从activity里实例化的函数   一个就是在service里的函数。
    新的线程是在service里开启的
      

  4.   

    你这为什么要写一个新的线程类呢,我看你的monitor(final Context context)这个方法里面又使用了runnable接口的。报的错就是你在main线程里面做的操作导致堵塞了
      

  5.   

    你service和main线程关于getRunningProcessInfo是不是都在操作UI啊
      

  6.   

    了解下main线程的looper机制,你的looper和主线程冲突了,请看帖子http://blog.csdn.net/singwhatiwanna/article/details/17361775,希望对你有帮助
      

  7.   

    意思就是runnable接口是用的main线程么
    我是新手,不是很懂,我想的是service后台运行,用一个新的线程,这样清晰一些
      

  8.   

    getRunningProcessInfo只是或许一下当前进程的一些信息,没有操作UI呀
      

  9.   

    本来service就是在后台,还有为啥service和main都去执行monitor这个方法,是不是有问题