Alarm Manager
-
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();
}
} -
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();
}
} -
Probably because
listFiles
will always be non-null. See File | Android Developers[^]. -
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();
}
}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 !~
-
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 !~
-
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 !~
-
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 !~
-
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 !~
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(); } }
}
-
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(); } }
}
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 !~
-
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 !~
-
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 !~
-
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();
}
}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
-
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
-
"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
-
"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
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) { } }
}
-
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) { } }
}
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 callinglistFiles()
once beforestartWatching()
. I would think that you'd want to calllistFiles()
only after aCREATE
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
-
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 callinglistFiles()
once beforestartWatching()
. I would think that you'd want to calllistFiles()
only after aCREATE
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
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) { } }
}
-
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) { } }
}
Did you set a breakpoint in the event handler? Using the debugger, what is the value of
event
,contents
, andconn_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