Alarm Manager
-
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