I am making an app in Android Studio in Java and one of the codes is when volume up button triggered(action key up) device should vibrate.
It works if I hold volume button for like 500 milliseconds and then release it, but on quick press on volume button it doesn't work.
I use volume up trigger. if I hold the volume button down for like 500 millisecond and then release it vibration works, but If I just quickly push volume button other things works but vibration doesn't.
the code:
switch (keycode) {
case `your text`KeyEvent.KEYCODE_VOLUME_UP:
if `your text`(KeyEvent.ACTION_UP == action) {
vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
count++;
String num = String.valueOf(count);
Number.setText(num);
{
if (count == 1) {
vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
final long[] pattern1 = {0, 200, 0,};
vibrator.vibrate(pattern1, -1);
Changing action up to action down causes another problem which if I hold the button the code continuously runs.
I tried changing action up to action down but caused another problem. I don't want the code to constantly run if volume key is hold down.
Related
I've just started learning Android. I use my MIUI 10 device for debugging.
I've learned the very basic App Lifecycle Program. The problem is, I'm using toasts to show when each of the methods are being called. For eg: When I press the back button, onPause, onStop and onDestroy are 3 SEPERATE toasts that will be displayed.
The problem I'm facing is that MIUI will automatically cancel the current toast if another one is called. So I end up with only onDestroy toast displaying.
Is there a way to ensure I have the toast on screen for a set amount of time before the next one comes? This doesn't have to only apply to this scenario. I need a general solution that will help in the future as well.
If MIUI's version of Android has altered the default behaviour where Toasts are displayed sequentially, you can always post them with a delay yourself. The standard delay period for a long toast is 3500ms, and a short toast is 2000ms. With that in mind, you could try something along these lines (untested):
final Handler uiHandler = new Handler(Looper.getMainLooper());
void scheduleToasts(String... messages) {
final List<String> list = Arrays.asList(messages);
Collections.reverse(list);
final AtomicInteger countdown = new AtomicInteger(list.size());
final ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
service.scheduleWithFixedDelay(() -> {
final int count = countdown.getAndDecrement();
if(count == 0) {
service.shutdown();
return;
}
uiHandler.post(() -> Toast.makeText(getContext(), list.get(count), Toast.LENGTH_LONG).show());
}, 0, 3500, TimeUnit.MILLISECONDS);
}
With usage:
scheduleToasts("message1", "message2", "message3");
In my code I want to begin doing something when the user hits "start" and pause if the user hits "pause" and start up again once the user hits "play"
The pause button changes text to read play.
I'm having issues with stopping the program once it starts. The following is an extremely simplified version of my code, assuming all imports have been done:
boolean play = true;
Button pausebtn = new Button("Pause");
pausebtn.setOnAction( e -> {
if(play == true)
{
pausebtn.setText("Play");
play = false;
//pause the printing that the "Start" button began
}
else
{
pausebtn.setText("Pause");
play = true;
}
});
Button startbtn = new Button("Start");
startbtn.setOnAction( e -> {
//start printing from 1 to integer max
});
I know that the order of this code may need to be changed. But before I do that I want to just know exactly how I should go about doing this.
My actual code of course is much more complicated but this is basically what I want to happen: have the program start one execution when the start button is pressed, and pause it once the pause button is pressed, and start again once the play button is pressed.
Any help would be appreciated.
My audio isn't playing...I've been stuck on this for around 7 hours (ALL DAY!), and have cannot figure out the issue. I am trying to have the volume mute when user clicks mute and unmute when user clicks it again.
I want to be able to have the user unmute the app to the same volume that the user was originally on. userVolumeOnStart.userVolume is where the users original volume is stored in. I use this value in the unMute, to set their volume to what it used to be.
For example:
If your volume was 7 and you used my mute button:
Open app->Mute (Volume=0)->Unmute(Volume = 7 again) etc.
Here is the listener to the mute button:
public void mute(View view) {
mutebutton = (ImageButton) findViewById(R.id.mutebutton);
if ((variableForMute.x % 2) != 0) { //If it's odd UNMUTE
Log.v(TAG, "Use this volume to unmute " +userVolumeOnStart.userVolume +"");
Toast.makeText(Main_Menu.this, "UNMUTED", Toast.LENGTH_SHORT).show();
mAudioManager.setStreamVolume(AudioManager.STREAM_RING, userVolumeOnStart.userVolume, 0);
variableForMute.x++;
} else { //If its even MUTE
userVolumeOnStart.userVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_RING);
Log.v(TAG, "Right Before Mute " +userVolumeOnStart.userVolume +"");
mAudioManager.setStreamVolume(AudioManager.STREAM_RING, 0, 0);
Toast.makeText(Main_Menu.this, "MUTED", Toast.LENGTH_SHORT).show();
variableForMute.x++;
}
}
userVolumeOnStart.userVolume is a variable that I defined in a seperate class, to use it globally. Here is where the audio actually runs. I can assure you the issue is not here (as I have not messed with the code in this class today) , but I will still provide the code anyway:
cdt = new CountDownTimer(20000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
Log.v(TAG, millisUntilFinished + "left");
millisUntilFinishedRounded = ((500 + millisUntilFinished) / 1000) * 1000;
if (millisUntilFinishedRounded == 10000) { //TEN SECONDS
ten = MediaPlayer.create(TwentySeconds.this, R.raw.tenmoreseconds);
ten.start();
}
if (millisUntilFinishedRounded == 3000) {
three = MediaPlayer.create(TwentySeconds.this, R.raw.threetwoone);
three.start();
}
}
This is a service that runs in the background when the app starts.
Here is my log after changing ringer volume on my phone while app is running and pressing the mute button. It shows that userVolumeOnStart.userVolume is doing what it's supposed to be doing:
I am desperate now, so I would really appreciate any feedback (negative or positive!)
Thanks so much,
{Rich}
By the way, my question is not a duplicate of The audio volume is zero?. I have changed the audio stream since that question, and have figured out the problem there. There, there was an issue with AudioManager.STREAM_MUSIC where userVolumeOnStart.userVolume would always be 0. I have changed STRAM_MUSIC to STREAM_RING. That is not the issue now, as userVolumeOnStart.userVolume successfully changes based on user volume. The problem now is that the Media Players are not effectively working for an unknown reason.
I found the answer after 2 days!
MediaPlayer mp = MediaPlayer.create(this, R.raw.MYAUDIO);
I shouldn't break it up into declaration and instantiation:
Mp.create will call the static method which is an instance that won't get stored. When I call start() I am starting the original MediaPlayer, not the instance! So basically, I am satrting something that I never even stored, and that's why the audio wasn't playing!
I'm struggling with a kind of feedback given progress bar.
I have an app that controls a measurement device that gives back signal data as numbers and on a run there is tens of signals. In the app I have a progress dialog that shows the progress of a run and the signals as plain text. I'd like to add a kind of a progress bar that would have increment steps with different colors depending of the signal. For example the higher the signal the redder the increment step.
Is it possible to use progress dialog/bar to do that?
Solved by creating a progress bar of my own from blank LinearLayout
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
for(int k=0; k<bar+1; k++) {
View v = new View(this);
v.setBackgroundColor(Color.GRAY);
params.weight = 1;
v.setLayoutParams(params);
progress_bar.addView(v, k);
}
Which can then be updated with
progress_bar.getChildAt(check).setBackgroundColor(Color.rgb(r,g,b));
where check is first 0 and is increased by 1 every time
You could try this:
pBar.getIndeterminateDrawable().setColorFilter(0xFFFF0000,PorterDuff.Mode.Multiply);
You can also try:
pBar.getProgressDrawable().setColorFilter(0xFFFF0000,PorterDuff.Mode.Multiply);
Source: Change progressbar color through CODE ONLY in Android
I have a MapActivity which has a touch event.
In the touch event:
I get the current geocodes
Put an overlay image on that location.
and set it as the center of the mapview.
This seems to work fine and as expected for a while. But then the application force closes. I think the touch event is being fired like crazy even when nothing's being touched. I know that because I had put a toast set up that shows the longitude and lattitude of the touched position. and it keeps showing up even when nothing's touched.
I cant seem to accurately position the problem, let alone fixing it. Here's the touch event for my mapActivity
private void LocationStuffs(double latitude, double longitude){
itemizedOverlay.clear();
itemizedOverlay.addOverlayItem((int)latitude, (int)longitude, "place");
mapView.invalidate();
mapView.getOverlays().add(itemizedOverlay);
MapController mc = mapView.getController();
mc.setCenter(new GeoPoint((int)latitude, (int)longitude));
mc.setZoom(20);
}
public boolean onTouchEvent(MotionEvent event, MapView mapView){
//---when user lifts his finger---
if (event.getAction() == 1) {
GeoPoint p = mapView.getProjection().fromPixels(
(int) event.getX(),
(int) event.getY());
Toast.makeText(getBaseContext(),
p.getLatitudeE6() / 1E6 + "," +
p.getLongitudeE6() /1E6 ,
Toast.LENGTH_SHORT).show();
latitude = p.getLatitudeE6() / 1E6;
longitude = p.getLongitudeE6() / 1E6;
LocationStuffs(latitude*1E6, longitude*1E6);
}
return false;
}
Change your return statement to return true:
return true;
Basically, what that does is that it tells the system that you have handled the touchevent. Let me know if that solves it.
I believe you have few errors in your code:
1- You need a "return true" statement before you close the "if" statement in the "onTouchEvent" method. This tells the system that the event has been handled and it should be consumed. That's probably the reason for multiple touchs.
2- the "mapView.invalidate();" should be moved to the end of your "LocationStuffs" method, as you want to have you screen redrawn after you have add the new location and not before.
3- You are adding repeatedly the same itemizedOverlay to the mapview. After calling your "LocationStuffs" method 10 times, you will have the same overlay added 10 times to the overlays list. You should add the Itemizedoverlay only once, outside this method (on the onCreate or onResume for example)
good luck