Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Mobile Development
  3. Android
  4. Alarm Manager

Alarm Manager

Scheduled Pinned Locked Moved Android
question
19 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Lost User

    Probably because listFiles will always be non-null. See File | Android Developers[^].

    P Offline
    P Offline
    Pavlex4
    wrote on last edited by
    #3

    Directory "/sys/bus/usb/devices" is empty when otg cable is not attached !!!!

    L 1 Reply Last reply
    0
    • P Pavlex4

      Directory "/sys/bus/usb/devices" is empty when otg cable is not attached !!!!

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #4

      Then use your debugger to find out what is happening. As we keep telling you.

      A 1 Reply Last reply
      0
      • P Pavlex4

        I want to check every second if /sys/bus/usb/devices/ directory is empty or it contain files because I want to make text inside app that says otg not connected if directory is empty or to say otg connected if directory contain files and I want to check that outside the app,while it runs in background! I need to check every second if directory is empty or it contain files? I have crated detection using alarm manager but it doesn't work.It's just repeating "otg connected" MainActivity.class

        public class MainActivity extends AppCompatActivity
        {
        private Process suProcess;
        private PendingIntent pendingIntent;

        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity\_main);
        
            getRoot();
            startAlarm();  
        }
        
        private void getRoot()
        {
            try
            {
                suProcess = Runtime.getRuntime().exec("su");
            }
            catch (IOException e)
            {
        
            }
        }
        
        private void startAlarm()
        {
            Intent alarmIntent = new Intent(MainActivity.this, AlarmReceiver.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
        
            AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM\_SERVICE);
            int interval = 1000;
            manager.setInexactRepeating(AlarmManager.RTC\_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
        }
        

        }

        Alarm Receiver.class

        public class AlarmReceiver extends BroadcastReceiver
        {
        @Override
        public void onReceive(Context context,Intent intent)
        {
        File[] listFiles = new File("/sys/bus/usb/devices").listFiles();
        if (listFiles == null) Toast.makeText(context,"otg not connected",Toast.LENGTH_SHORT).show();
        if (listFiles != null) Toast.makeText(context,"otg connected",Toast.LENGTH_SHORT).show();
        }
        }

        A Offline
        A Offline
        Afzaal Ahmad Zeeshan
        wrote on last edited by
        #5

        First of all, my recommendation is to consider using the [FileObserver](https://developer.android.com/reference/android/os/FileObserver.html) in Android, instead of triggering that each second to check if there are files or not. That is a good approach towards going through what you want to do. Secondly, as Richard mentioned, the reason is that the list object is not null, that doesn't mean its count or length or size is also zero. Just to show you what I really mean, here is a sample code,

        File directory = new File("/path/to/folder");
        File[] contents = directory.listFiles();

        if(contents == null) {
        // directory not found
        } else if(contents.length == 0) {
        // directory exists, but no files
        } else {
        // has further content, down the tree.
        }

        You can update your code, to meet this structure and then show the Toast notifications in Android. So kindly implement these two things, first of all that service will help you not throttle CPU every second. For an example of FileObserver please see: [java - How do you implement a FileObserver from an Android Service - Stack Overflow](http://stackoverflow.com/questions/7265906/how-do-you-implement-a-fileobserver-from-an-android-service). [file io - How to check if a directory is empty in Java - Stack Overflow](http://stackoverflow.com/questions/5930087/how-to-check-if-a-directory-is-empty-in-java)

        The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~

        P 1 Reply Last reply
        0
        • L Lost User

          Then use your debugger to find out what is happening. As we keep telling you.

          A Offline
          A Offline
          Afzaal Ahmad Zeeshan
          wrote on last edited by
          #6

          What happens is that his directory is indeed there, but he does not check up against files or other stuff inside the directory. I tried to explain the process to him in the post below, as well as another suggestion that will make his application not throttle CPU every 1 second to check if the directory was modified or not.

          The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~

          L 1 Reply Last reply
          0
          • A Afzaal Ahmad Zeeshan

            What happens is that his directory is indeed there, but he does not check up against files or other stuff inside the directory. I tried to explain the process to him in the post below, as well as another suggestion that will make his application not throttle CPU every 1 second to check if the directory was modified or not.

            The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #7

            Afzaal Ahmad Zeeshan wrote:

            I tried to explain

            Largely a waste of time it seems.

            A 1 Reply Last reply
            0
            • L Lost User

              Afzaal Ahmad Zeeshan wrote:

              I tried to explain

              Largely a waste of time it seems.

              A Offline
              A Offline
              Afzaal Ahmad Zeeshan
              wrote on last edited by
              #8

              Richard MacCutchan wrote:

              Largely a waste of time it seems.

              By the number of his interactions in the forum, indeed.

              The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~

              1 Reply Last reply
              0
              • A Afzaal Ahmad Zeeshan

                First of all, my recommendation is to consider using the [FileObserver](https://developer.android.com/reference/android/os/FileObserver.html) in Android, instead of triggering that each second to check if there are files or not. That is a good approach towards going through what you want to do. Secondly, as Richard mentioned, the reason is that the list object is not null, that doesn't mean its count or length or size is also zero. Just to show you what I really mean, here is a sample code,

                File directory = new File("/path/to/folder");
                File[] contents = directory.listFiles();

                if(contents == null) {
                // directory not found
                } else if(contents.length == 0) {
                // directory exists, but no files
                } else {
                // has further content, down the tree.
                }

                You can update your code, to meet this structure and then show the Toast notifications in Android. So kindly implement these two things, first of all that service will help you not throttle CPU every second. For an example of FileObserver please see: [java - How do you implement a FileObserver from an Android Service - Stack Overflow](http://stackoverflow.com/questions/7265906/how-do-you-implement-a-fileobserver-from-an-android-service). [file io - How to check if a directory is empty in Java - Stack Overflow](http://stackoverflow.com/questions/5930087/how-to-check-if-a-directory-is-empty-in-java)

                The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~

                P Offline
                P Offline
                Pavlex4
                wrote on last edited by
                #9

                Now when I launch it shows otg not connected and it's repeating it many times and when I plug in otg cable it's still repeating "otg not connected" and after some time it shows "otg connected" !!!! When I start app it shouldn't show anything, it should only show when I plug in or when I plug out otg cable.And it shouldn't repeat same message many times !!!!

                public class AlarmReceiver extends BroadcastReceiver
                {
                @Override
                public void onReceive(Context context,Intent intent)
                {

                    File directory = new File("/sys/bus/usb/devices");
                    File\[\] contents = directory.listFiles();
                
                    if(contents.length == 0)
                    {
                        Toast.makeText(context,"otg not connected",Toast.LENGTH\_SHORT).show();
                    }
                    else
                    {
                        Toast.makeText(context,"otg connected",Toast.LENGTH\_SHORT).show();
                    }
                }
                

                }

                A 1 Reply Last reply
                0
                • P Pavlex4

                  Now when I launch it shows otg not connected and it's repeating it many times and when I plug in otg cable it's still repeating "otg not connected" and after some time it shows "otg connected" !!!! When I start app it shouldn't show anything, it should only show when I plug in or when I plug out otg cable.And it shouldn't repeat same message many times !!!!

                  public class AlarmReceiver extends BroadcastReceiver
                  {
                  @Override
                  public void onReceive(Context context,Intent intent)
                  {

                      File directory = new File("/sys/bus/usb/devices");
                      File\[\] contents = directory.listFiles();
                  
                      if(contents.length == 0)
                      {
                          Toast.makeText(context,"otg not connected",Toast.LENGTH\_SHORT).show();
                      }
                      else
                      {
                          Toast.makeText(context,"otg connected",Toast.LENGTH\_SHORT).show();
                      }
                  }
                  

                  }

                  A Offline
                  A Offline
                  Afzaal Ahmad Zeeshan
                  wrote on last edited by
                  #10

                  That is because there are a few Toasts already in the queue, that need to be rendered. That is one of the backdrops of this timer trigger.

                  The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~

                  P 2 Replies Last reply
                  0
                  • A Afzaal Ahmad Zeeshan

                    That is because there are a few Toasts already in the queue, that need to be rendered. That is one of the backdrops of this timer trigger.

                    The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~

                    P Offline
                    P Offline
                    Pavlex4
                    wrote on last edited by
                    #11

                    I don't understand!What should I do?

                    1 Reply Last reply
                    0
                    • A Afzaal Ahmad Zeeshan

                      That is because there are a few Toasts already in the queue, that need to be rendered. That is one of the backdrops of this timer trigger.

                      The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~

                      P Offline
                      P Offline
                      Pavlex4
                      wrote on last edited by
                      #12

                      What to do?

                      1 Reply Last reply
                      0
                      • P Pavlex4

                        I want to check every second if /sys/bus/usb/devices/ directory is empty or it contain files because I want to make text inside app that says otg not connected if directory is empty or to say otg connected if directory contain files and I want to check that outside the app,while it runs in background! I need to check every second if directory is empty or it contain files? I have crated detection using alarm manager but it doesn't work.It's just repeating "otg connected" MainActivity.class

                        public class MainActivity extends AppCompatActivity
                        {
                        private Process suProcess;
                        private PendingIntent pendingIntent;

                        @Override
                        protected void onCreate(Bundle savedInstanceState)
                        {
                            super.onCreate(savedInstanceState);
                            setContentView(R.layout.activity\_main);
                        
                            getRoot();
                            startAlarm();  
                        }
                        
                        private void getRoot()
                        {
                            try
                            {
                                suProcess = Runtime.getRuntime().exec("su");
                            }
                            catch (IOException e)
                            {
                        
                            }
                        }
                        
                        private void startAlarm()
                        {
                            Intent alarmIntent = new Intent(MainActivity.this, AlarmReceiver.class);
                            PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
                        
                            AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM\_SERVICE);
                            int interval = 1000;
                            manager.setInexactRepeating(AlarmManager.RTC\_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
                        }
                        

                        }

                        Alarm Receiver.class

                        public class AlarmReceiver extends BroadcastReceiver
                        {
                        @Override
                        public void onReceive(Context context,Intent intent)
                        {
                        File[] listFiles = new File("/sys/bus/usb/devices").listFiles();
                        if (listFiles == null) Toast.makeText(context,"otg not connected",Toast.LENGTH_SHORT).show();
                        if (listFiles != null) Toast.makeText(context,"otg connected",Toast.LENGTH_SHORT).show();
                        }
                        }

                        D Offline
                        D Offline
                        David Crow
                        wrote on last edited by
                        #13

                        Pavlex4 wrote:

                        I want to check every second if /sys/bus/usb/devices/ directory is empty or it contain files...I need to check every second if directory is empty or it contain files?

                        Bad idea. All that does is needlessly tie up the CPU.

                        "One man's wage rise is another man's price increase." - Harold Wilson

                        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                        "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                        P 1 Reply Last reply
                        0
                        • D David Crow

                          Pavlex4 wrote:

                          I want to check every second if /sys/bus/usb/devices/ directory is empty or it contain files...I need to check every second if directory is empty or it contain files?

                          Bad idea. All that does is needlessly tie up the CPU.

                          "One man's wage rise is another man's price increase." - Harold Wilson

                          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                          "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                          P Offline
                          P Offline
                          Pavlex4
                          wrote on last edited by
                          #14

                          Than what's the better way to do this?

                          D 1 Reply Last reply
                          0
                          • P Pavlex4

                            Than what's the better way to do this?

                            D Offline
                            D Offline
                            David Crow
                            wrote on last edited by
                            #15

                            FileObserver[^]

                            "One man's wage rise is another man's price increase." - Harold Wilson

                            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                            "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                            P 1 Reply Last reply
                            0
                            • D David Crow

                              FileObserver[^]

                              "One man's wage rise is another man's price increase." - Harold Wilson

                              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                              "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                              P Offline
                              P Offline
                              Pavlex4
                              wrote on last edited by
                              #16

                              Why it's not working?

                              public class MainActivity extends AppCompatActivity
                              {
                              private Process suProcess;
                              private static int conn_length = -1;
                              File directory = new File("/sys/bus/usb/devices");
                              File[] contents = directory.listFiles();

                              @Override
                              protected void onCreate(Bundle savedInstanceState)
                              {
                                  super.onCreate(savedInstanceState);
                                  setContentView(R.layout.activity\_main);
                              
                                  getRoot();
                              
                              
                              
                                  FileObserver observer = new FileObserver("/sys/bus/usb/devices")
                                  {
                                      @Override
                                      public void onEvent(int event, String file)
                                      {
                                          if(contents.length == conn\_length){
                                              return;
                                          }
                                          else{
                                              conn\_length = contents.length;
                                          }
                              
                                          if(conn\_length == 0)
                                          {
                              
                                              Toast.makeText(MainActivity.this,"otg not connected",Toast.LENGTH\_SHORT).show();
                                          }
                                          else
                                          {
                                              Toast.makeText(MainActivity.this,"otg connected",Toast.LENGTH\_SHORT).show();
                                          }
                                      }
                                  };
                                  observer.startWatching();
                              }
                              
                              private void getRoot()
                              {
                                  try
                                  {
                                      suProcess = Runtime.getRuntime().exec("su");
                                  }
                                  catch (IOException e)
                                  {
                              
                                  }
                              }
                              

                              }

                              D 1 Reply Last reply
                              0
                              • P Pavlex4

                                Why it's not working?

                                public class MainActivity extends AppCompatActivity
                                {
                                private Process suProcess;
                                private static int conn_length = -1;
                                File directory = new File("/sys/bus/usb/devices");
                                File[] contents = directory.listFiles();

                                @Override
                                protected void onCreate(Bundle savedInstanceState)
                                {
                                    super.onCreate(savedInstanceState);
                                    setContentView(R.layout.activity\_main);
                                
                                    getRoot();
                                
                                
                                
                                    FileObserver observer = new FileObserver("/sys/bus/usb/devices")
                                    {
                                        @Override
                                        public void onEvent(int event, String file)
                                        {
                                            if(contents.length == conn\_length){
                                                return;
                                            }
                                            else{
                                                conn\_length = contents.length;
                                            }
                                
                                            if(conn\_length == 0)
                                            {
                                
                                                Toast.makeText(MainActivity.this,"otg not connected",Toast.LENGTH\_SHORT).show();
                                            }
                                            else
                                            {
                                                Toast.makeText(MainActivity.this,"otg connected",Toast.LENGTH\_SHORT).show();
                                            }
                                        }
                                    };
                                    observer.startWatching();
                                }
                                
                                private void getRoot()
                                {
                                    try
                                    {
                                        suProcess = Runtime.getRuntime().exec("su");
                                    }
                                    catch (IOException e)
                                    {
                                
                                    }
                                }
                                

                                }

                                D Offline
                                D Offline
                                David Crow
                                wrote on last edited by
                                #17

                                Define "not working." Since you asked for ALL_EVENTS, your event handler is potentially going to get called for a lot of events, yet you are not checking for any of them. Is that intentional? You are calling listFiles() once before startWatching(). I would think that you'd want to call listFiles() only after a CREATE event.

                                "One man's wage rise is another man's price increase." - Harold Wilson

                                "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                                "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                                P 1 Reply Last reply
                                0
                                • D David Crow

                                  Define "not working." Since you asked for ALL_EVENTS, your event handler is potentially going to get called for a lot of events, yet you are not checking for any of them. Is that intentional? You are calling listFiles() once before startWatching(). I would think that you'd want to call listFiles() only after a CREATE event.

                                  "One man's wage rise is another man's price increase." - Harold Wilson

                                  "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                                  "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                                  P Offline
                                  P Offline
                                  Pavlex4
                                  wrote on last edited by
                                  #18

                                  I have changed code to this but it still won't show message when cable is connected or disconnected !!!!

                                  public class MainActivity extends AppCompatActivity
                                  {
                                  private Process suProcess;
                                  private static int conn_length = -1;
                                  File directory = new File("/sys/bus/usb/devices");
                                  File[] contents;

                                  @Override
                                  protected void onCreate(Bundle savedInstanceState)
                                  {
                                      super.onCreate(savedInstanceState);
                                      setContentView(R.layout.activity\_main);
                                  
                                      getRoot();
                                  
                                  
                                  
                                      FileObserver observer = new FileObserver("/sys/bus/usb/devices")
                                      {
                                          @Override
                                          public void onEvent(int event, String file)
                                          {
                                              if(event == FileObserver.CREATE)
                                              {
                                                  contents = directory.listFiles();
                                  
                                                  if (contents.length == conn\_length)
                                                  {
                                                      return;
                                                  }
                                                  else
                                                  {
                                                      conn\_length = contents.length;
                                                  }
                                  
                                                  if (conn\_length == 0)
                                                  {
                                  
                                                      Toast.makeText(MainActivity.this, "otg disconnected", Toast.LENGTH\_SHORT).show();
                                                  }
                                                  else
                                                  {
                                                      Toast.makeText(MainActivity.this, "otg connected", Toast.LENGTH\_SHORT).show();
                                                  }
                                              }
                                          }
                                      };
                                      observer.startWatching();
                                  }
                                  
                                  private void getRoot()
                                  {
                                      try
                                      {
                                          suProcess = Runtime.getRuntime().exec("su");
                                      }
                                      catch (IOException e)
                                      {
                                  
                                      }
                                  }
                                  

                                  }

                                  D 1 Reply Last reply
                                  0
                                  • P Pavlex4

                                    I have changed code to this but it still won't show message when cable is connected or disconnected !!!!

                                    public class MainActivity extends AppCompatActivity
                                    {
                                    private Process suProcess;
                                    private static int conn_length = -1;
                                    File directory = new File("/sys/bus/usb/devices");
                                    File[] contents;

                                    @Override
                                    protected void onCreate(Bundle savedInstanceState)
                                    {
                                        super.onCreate(savedInstanceState);
                                        setContentView(R.layout.activity\_main);
                                    
                                        getRoot();
                                    
                                    
                                    
                                        FileObserver observer = new FileObserver("/sys/bus/usb/devices")
                                        {
                                            @Override
                                            public void onEvent(int event, String file)
                                            {
                                                if(event == FileObserver.CREATE)
                                                {
                                                    contents = directory.listFiles();
                                    
                                                    if (contents.length == conn\_length)
                                                    {
                                                        return;
                                                    }
                                                    else
                                                    {
                                                        conn\_length = contents.length;
                                                    }
                                    
                                                    if (conn\_length == 0)
                                                    {
                                    
                                                        Toast.makeText(MainActivity.this, "otg disconnected", Toast.LENGTH\_SHORT).show();
                                                    }
                                                    else
                                                    {
                                                        Toast.makeText(MainActivity.this, "otg connected", Toast.LENGTH\_SHORT).show();
                                                    }
                                                }
                                            }
                                        };
                                        observer.startWatching();
                                    }
                                    
                                    private void getRoot()
                                    {
                                        try
                                        {
                                            suProcess = Runtime.getRuntime().exec("su");
                                        }
                                        catch (IOException e)
                                        {
                                    
                                        }
                                    }
                                    

                                    }

                                    D Offline
                                    D Offline
                                    David Crow
                                    wrote on last edited by
                                    #19

                                    Did you set a breakpoint in the event handler? Using the debugger, what is the value of event, contents, and conn_length?

                                    "One man's wage rise is another man's price increase." - Harold Wilson

                                    "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                                    "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                                    1 Reply Last reply
                                    0
                                    Reply
                                    • Reply as topic
                                    Log in to reply
                                    • Oldest to Newest
                                    • Newest to Oldest
                                    • Most Votes


                                    • Login

                                    • Don't have an account? Register

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • World
                                    • Users
                                    • Groups