Android listen volume up button pressed 3 times
I am new to Android development and I want some function to call whenever user presses the Volume Up button 3 times (in Java). I am making an SOS application and want to implement this functionality.
do you know?
how many words do you know
See also questions close to this topic
-
Read each name in Array list to create seperate object for
I have a file that has student names, age, and an id number. I have a student class that holds the everything above for each student object. I stored all the names, id numbers. and age separately in an array list. Now im trying to assign the info to create a student object.
public class Student { private String lName; private int idNumber; private int age; public Student() { lName = ""; idNumber = 0; age = 0; } public Student(String l, int i, int a) { lName = l; idNumber = i; age = a; } public void setName(String last) { lName = last; } public String getName() { return lName; } public void setIdNum(int num) { idNumber = num; } public int getIdNum() { return idNumber; } public void setAge(int a) { age = a; } public int getAge() { return age; } }
My Text File looks something like this: This info is stored in parallel array lists. I don't quite get how to implement this into an object to pass it into my second contractor method.
Josh 2134 19 Smith 5256 21 Rogers 9248 19 Andrew 7742 20
Here's what I've tried;
public static void main(String[] args) { String file = "studentData.txt"; Scanner reader = new Scanner(file); ArrayList<String> lastNames = lNames(file); ArrayList<Integer> idNumbers = idNum(file); ArrayList<Integer> ageList = ages(file); Scanner input = new Scanner(System.in); Student s1 = new Student(); // confused about how to implement this constructor with the textile info for (int i = 0; i<idNumbers.size(); i++) { Student user = new Student(lastNames.get(i), idNumbers.get(i), ageList.get(i)); } //user enters idNumber to display age System.out.println("Enter ID Number"); //exception handling to be added int idNum = input.nextInt(); for (int i = 0; i<idNumbers.size(); i++) { if (idNum == idNumbers.get(i)) { s1.setAge(ageList.get(i)); System.out.println(s1.getAge()); } } }
-
Using EdittextPreference for Goto search
sorry for my poor English. I want to use EditTextPreference in my bottom nav like the pic below, ![screenshot][1]
I have recycleview xml in my project with in many sub cardview layouts(which is scrollable) and I want to create item in the bottom nav which is called "Goto". When the "Goto" item is clicked i want it to pop-up like the screenshot. And when user enters a number(according to the cardviews i.e if the number of cardview is 40 user must enter 1-40) I want to search the cardview by its ID. Thank you and I hope u got it, If u have any questions let me know [1]: https://i.stack.imgur.com/grK8P.jpg
My xml format look like this. As you see in the blow since the cardviews are huge in number it is not cool to scroll all the way down that is why i need Goto item in the bottom nav to search it by its ID when the user click number in the EditTextPreference as u see in the screenshot. i.e The screenshot is not from my app
<LinearLayout> <LinearLayout> <androidx.cardview.widget.CardView> <RealtiveLayout> <Textview/> <RealtiveLayout> </androidx.cardview.widget.CardView> </LinearLayout> <LinearLayout> <androidx.cardview.widget.CardView> <RealtiveLayout> <Textview/> <RealtiveLayout> </androidx.cardview.widget.CardView> </LinearLayout> <LinearLayout> <androidx.cardview.widget.CardView> <RealtiveLayout> <Textview/> <RealtiveLayout> </androidx.cardview.widget.CardView> </LinearLayout> <LinearLayout> <androidx.cardview.widget.CardView> <RealtiveLayout> <Textview/> <RealtiveLayout> </androidx.cardview.widget.CardView> </LinearLayout> .. .. .. .. many more..
-
How to get remaining time of the day in java?
I would like to calculate the time remaining for next day 00:00:00 from the current date time.
For e.g. time difference between 2
022-05-07T05:49:41.883807900Z
and2022-05-08T00:00:00Z
Expected answer:
18:10:19
or 65419 (in seconds).How can I achieve this with efficiently using java 8?
-
Updating a Single Column In Room Database
That's the function I'm using for updat
private fun updateSettingsDatabase(settingsDao: SettingsDao) { lifecycleScope.launch { settingsDao.update(SettingsEntity( 1, nightMode=nightModeResult, )) } } @Query("SELECT * FROM `settings-table`") fun fetchCurrentSettings(): Flow<List<SettingsEntity>>
I specified
nightMode=
because I thought that this way I'm only updating this colummn, but it turns out that it resets every column, how do I update a single column, while keeping the values the rest of the columns? -
IOS Launcher in android studio
I'm trying to change the whole Android OS installed app icons into IOS icons, please help me with the proper code or library for android kotlin
-
Android activity onKeyDown() not triggered for Power button, and Intent.ACTION_SCREEN_OFF is not broadcasted either
I have an
Activity
(Activity_RingAlarm
) that is supposed to be launched as a full-screen intent when an alarm rings. The activity is launched fine, no issue there. I want to give the user an option to dismiss the alarm by pressing the power button. To this effect, I had programmed theActivity
to listen to Power button press, as well as theIntent.ACTION_SCREEN_OFF
broadcast. The relevant portion of code is shown below:public class Activity_RingAlarm extends AppCompatActivity implements View.OnClickListener { private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if (Objects.equals(intent.getAction(), Intent.ACTION_SCREEN_OFF)) { DisplayManager displayManager = (DisplayManager) context.getSystemService(DISPLAY_SERVICE); for (Display display : displayManager.getDisplays()) { if (display.getState() == Display.STATE_OFF) { onPowerButtonPressed(); } } } } }; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) { setTurnScreenOn(true); setShowWhenLocked(true); } super.onCreate(savedInstanceState); setContentView(R.layout.activity_ringalarm); // other codes here IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(Intent.ACTION_SCREEN_OFF); intentFilter.addAction(Intent.ACTION_SCREEN_ON); registerReceiver(broadcastReceiver, intentFilter); } @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(broadcastReceiver); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { Log.e(getClass().getSimpleName(), "keycode = " + keyCode); if (keyCode == KeyEvent.KEYCODE_POWER) { onPowerButtonPressed(); return true; } return super.onKeyDown(keyCode, event); } }
The issue is, while
onKeyDown(int, KeyEvent)
responds to volume up and down buttons, it does not respond to the power key. Neither in the emulator, nor in my Samsung Galaxy M31 (both running Android 12 API 31).I tried putting the above code in the main activity of the app, but even there, the issue is the same.
According to answers posted to this question, it is indeed not possible to capture key events related to the power key, but the
ACTION_SCREEN_OFF
broadcast should still work. But my app doesn't even get that broadcast. The documentation of the above intent action says that the broadcast has nothing to do with the screen turning off, and is broadcasted when the device becomes non-interactive.In another question (can't re-locate it at the moment), it was said that
dispatchKeyEvent(KeyEvent)
should work. I tried that too, but it doesn't.What can I do in this situation?
-
on:keydown event with Enter Key in svelte
I am using svelte with a on:click event on a button. when this button is clicked, i dispatch some information to a higher component. What I would like to do is hit the enter key instead, but on:keydown doesn't seem to work? How can I get this to fire when hitting enter?
<button on:click={() => dispatch('search', { searchword: item })} >ClickMe</button>
<button on:keydown={() => dispatch('search', { searchword: item })} >PressEnter</button>