ANR在服务中与广播接收器有关

时间:2012-01-20 23:08:53

标签: android

我的目标是启动一项服务,侦听设备屏幕状态的更改(打开或关闭)并对这些更改进行操作。我知道这并不理想,但是,这正是我想要实现的目标。

出于某种原因,我的广播接收器似乎只在屏幕亮起时触发,而不是在屏幕亮起时触发。此外,logcat显示了大量的ANR,似乎服务被重复杀死并重新启动。

我按照教程发现:here

以下是我的相关代码:

ScreenReceiver.java

    public class ScreenReceiver extends BroadcastReceiver {

        private boolean screenOff;

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
                screenOff = true;
            } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
                screenOff = false;
            }
            Intent i = new Intent(context, UpdateService.class);
            i.putExtra("screen_state", screenOff);
            context.startService(i);
        }

    }

UpdateService.java(根据建议更新,现在导致强制关闭)

    public class UpdateService extends IntentService {

        public UpdateService(String name) {
        super(name);
        // TODO Auto-generated constructor stub
    }

        @Override
        public void onCreate() {
            super.onCreate();
            // register receiver that handles screen on and screen off logic
            IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
            filter.addAction(Intent.ACTION_SCREEN_OFF);
            BroadcastReceiver mReceiver = new ScreenReceiver();
            registerReceiver(mReceiver, filter);
        }

        @Override
        protected void onHandleIntent(Intent intent) {
            boolean screenOn = intent.getBooleanExtra("screen_state", false);
            if (!screenOn) {



                String command8 = "*******";
                String command9 = "*********";
                    int timeout = 5;

   try {
   RootTools.Result result = new RootTools.Result() {
   @Override
   public void process(String line) throws Exception {
   // Do something with current line;
   // Maybe store it using setData()
   }

   @Override
   public void onFailure(Exception ex) {
   // Do something if we failed while trying to run a command or read its output

   setError(1);
   }

   @Override
   public void onComplete(int diag) {


   }

   };

   RootTools.sendShell(
   new String[] {

   command8,
   command9},
   timeout,
   result
   );
   if(0 != result.getError())
   return;
   //Do something with getData() if needed.
   } catch (IOException e) {
   //Handle exception
   } catch (InterruptedException e) {
   //Handle exception
   } catch (RootToolsException e) {
   //TODO Auto-generated catch block
   e.printStackTrace();
   }

            } else {

             String command8 = "*******";
                String command9 = "*******";
                 String command10 = "********";
                    String command11 = "********";
                    int timeout = 5;

   try {
   RootTools.Result result = new RootTools.Result() {
   @Override
   public void process(String line) throws Exception {
   // Do something with current line;
   // Maybe store it using setData()
   }

   @Override
   public void onFailure(Exception ex) {
   // Do something if we failed while trying to run a command or read its output

   setError(1);
   }

   @Override
   public void onComplete(int diag) {
   //TODO

   }

   };

   RootTools.sendShell(
   new String[] {

   command8,
   command9,
   command10,
   command11},
   timeout,
   result
   );
   if(0 != result.getError())
   return;
   //Do something with getData() if needed.
   } catch (IOException e) {
   //Handle exception
   } catch (InterruptedException e) {
   //Handle exception
   } catch (RootToolsException e) {
   //TODO Auto-generated catch block
   e.printStackTrace();
   }
            }
        }

        @Override
        public IBinder onBind(Intent arg0) {
            // TODO Auto-generated method stub
            return null;
        }

}

按下按钮启动广播接收器,使用以下代码:

 IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    BroadcastReceiver mReceiver = new ScreenReceiver();
    registerReceiver(mReceiver, filter);

1 个答案:

答案 0 :(得分:3)

onStart()不仅被弃用,而且在主应用程序线程上调用。 ANR的定义是您在主应用程序线程上花费了太多时间。请将onStart()逻辑移到后台线程中,可能是通过继承IntentService并将逻辑放在onHandleIntent()中。