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.
  • 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