How to send message to mute sound through activities - volume

As the title, I want to mute sound when hitting X button or unmute when hitting Y button, and I want to keep this status through activities. Here is my code.
When hitting X button:
mSound.setVolume(0, 0);
sound = "SOUND_OFF";
Y button:
mSound.setVolume(1, 1);
sound = "SOUND_ON"
When I finish my first activity:
Intent i = new Intent(getApplicationContext(),com.example.englishtest.Question.class);
i.putExtra("checkSound", sound);
startActivity(i);
overridePendingTransition(R.anim.right_in, R.anim.left_out);
finish();
In my second activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
Bundle extras = getIntent().getExtras();
if(extras != null) {
sound = extras.getString("checkSound");
checkVolume(sound);
}
My checkVolume function:
public void checkVolume(String s){
if(s=="SOUND_ON"){
mSound.setVolume(1, 1);
}
else if(s=="SOUND_OFF"){
mSound.setVolume(0, 0);
}
}
But it doesn't work. Can anyone show me my mistake?

Too late from the moment you post this question. But I think your approaching is bad.
Let create another class with a static field to hold configuration status.
Such as:
public class Configuration {
public static boolean soundOn;
}
Initialize this field in the main Activity:
protected void onCreate(Bundle savedBundle) {
Configuration.soundOn = true;
...
}
Then just checking this field whenever you wanna setVolume and remember to save new status to this field when setting mute/unmute from any activity.

Related

Android dev, What's best practice for timing update data in viewmodel

I want obey the MVVM struct in an android app. Now I have a situaion:
I want repeat get data from net and update the view.
So my code is like this:
public class FooVm() {
private MutalbeLiveData<Data> data = new MutableLiveData();
private Timer timer;
public LiveData<Data> getData() {
if (data == null) {
data = new MutableLivedata();
repeatRefresh()
}
}
private void repeatRefersh() {
timer = new Timer();
// schedule at fixed rate get data from net.
}
#override
public void onClear() {
if (timer == null) {
timer.cancel();
timer = null;
}
}
}
Now question is: if I want pause refresh when then view(for example, an Activity) onStop and resume refresh when view onStart. What's the right way to do thi?
Create another methods in viewModel , stopTimer() and resumeTimer()
override onStop() and onResume() in the activity
call viewModel.stopTimer() and viewModel.resumeTimer() before super method.
override fun onStop() {
viewModel.stopTimer()
super.onStop()
}
override fun onResume(){
viewModel.resumeTimer()
super.onResume()
}
I write it in kotlin . but you get the idea.

How to wait for user's action in JavaFX (in the same stage)

Do you know how to wait for the user's input in a for loop? I don't mean the showAndWait() method, because I am not opening a new dialogue stage for the user. So for example, each round of the for loop should be waiting for the user to push a button before going ahead with the next round.
How is it possible? Many thanks!
UPDATE:
Now it came to my mind, that it would work with a while(buttonNotPressed){} but is it a good solution? I mean the while loop is running in this case as crazy until the user won't push the button. Or doest it work somehow similarly with wait methods?
Imagine it as a session:
User starts session with handleStart() You give the user 5 questions, one after one. In every iteration, the user can answer the upcoming question and he can save or submit the answer by handleSaveButton() You process the answer as you want, and go ahead with the next iteration. The point is, that the iteration must stop, until the save button hasn't been pressed.
Don't do it like that. The FX toolkit, like any event-driven GUI toolkit, already implements a loop for the purposes of rendering the scene graph and processing user input each iteration.
Just register a listener with the button, and do whatever you need to do when the button is pressed:
button.setOnAction(event -> {
// your code here...
});
If you want the action to change, just change the state of some variable each time the action is performed:
private int round = 0 ;
// ...
button.setOnAction(event -> {
if (round < 5) {
System.out.println("Round "+round);
System.out.println("User's input: "+textArea.getText());
round++ ;
}
});
I recently ran into a similar problem where I wanted something to be executed with an interval (if that's what you mean), until the user fired an event. I found 3 ways to do this:
UPDATE
You should use the stop/cancel method for the custom runnable and timer or else the thread will still be running when you exit the application. Timeline seems do it by itself.
Using a Timer:
Timer timer = new Timer();
TimerTask task = new TimerTask() {
#Override
public void run() {
System.out.println("Printed every second.");
}
};
timer.scheduleAtFixedRate(task, 0, 1000);
//timer.cancel();
With a TimeLine:
Timeline tl = new Timeline(new KeyFrame(Duration.millis(1000), e -> {
System.out.println("Timeline");
}));
tl.setCycleCount(Timeline.INDEFINITE);
tl.play();
//tl.stop();
Or making your own runnable class:
public class Runner implements Runnable {
private final Thread thread = new Thread(this);
private boolean run;
#Override
public void run() {
while(run) {
try {
System.out.println("Printed from loop");
Thread.sleep(1000);
} catch (InterruptedException e) {
run = false;
}
}
}
public void start() {
run = true;
thread.start();
}
public void stop() {
if(run) {
thread.interrupt();
System.out.print("Thread has stopped.");
}
}
}
And then when a person clicks fx. a button the event would stop using the example James_D posted:
Button btn = new Button("Button");
btn.setOnAction(e -> {
timer.cancel();
tl.stop();
runner.stop();
});
In my case, for inside for, had to create 2 index in class, use:
//start method
Timer timer = new Timer();
TimerTask task = new TimerTask()
{
public void run()
{
Platform.runLater(()->{
//... code to run after time, calling the same mehtod, with condition to stop
});
}
};
timer.schedule(task, time);
//end method
Had to use recursive method, incrementing the index with conditions, cause the tasks were been schedule all at the same time, without wait time.
I do not know if it is rigth, but was the solution that i found.
Hope it helps.
ALTERNATIVE SOLUTION W/O PAUSING:
I'm creating a game where I want the user to pick the game difficulty before the game starts. Instead of trying to pause the program midway through, I just put the next step of the code in a separate method which you call once a button is clicked:
private static difficulty;
public static void main(String[] args) {
try {
Application.launch(args);
} catch (UnsupportedOperationException e) {
}
}
public void start(Stage startStage) {
HBox buttons = new HBox();
Button easyButton = new Button("Easy");
Button mediumButton = new Button("Medium");
Button hardButton = new Button("Hard");
buttons.getChildren().addAll(easyButton, mediumButton, hardButton);
buttons.setAlignment(Pos.CENTER);
hbox.getChildren().addAll(buttons);
Scene startScene = new Scene(buttons, 200, 200);
startStage.setScene(startScene);
startStage.show(); // MENU
EventHandler<ActionEvent> playEasy = new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent actionEvent) {
difficulty = 1; // SET DIFFICULTY
startStage.close(); // CLOSE MENU
play(); // RUN GAME ON EASY
}
};
EventHandler<ActionEvent> playMedium = new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent actionEvent) {
difficulty = 2; // SET DIFFICULTY
startStage.close(); // CLOSE MENU
play(); // RUN GAME ON MEDIUM
}
};
EventHandler<ActionEvent> playHard = new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent actionEvent) {
difficulty = 3; // SET DIFFICULTY
startStage.close(); // CLOSE MENU
play(); // RUN GAME ON HARD
}
};
easyButton.setOnAction(playEasy);
mediumButton.setOnAction(playMedium);
hardButton.setOnAction(playHard);
}
public void play() {
// WRITE GAME CODE HERE
}
To solve your specific problem, you could probably pass the startStage into the play method and then just update the scene there...but regardless I do hope this helps someone whos having trouble on how to use buttons! :)

Update fragment from activity

I have a activity which have two fragments.
Activity receives broadcast events for the two fragments.
One fragment has a image button and text view. When the image button is clicked an event is send to the server and server responds back with live broadcast event.
We receive the response in activity and I need to update the UI of the fragment(the image button needs to be changed with another image)
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.single_window_lock, container, false);
updateUI(view);
return view;
}
public void updateUI(View view){
String lockName;
final String lockState;
final boolean state;
final ImageButton singleLockImage = (ImageButton)view.findViewById(R.id.single_lock_image);
final TextView lockNameText = (TextView)view.findViewById(R.id.single_lock_name);
final TextView lockStateText = (TextView)view.findViewById(R.id.single_lock_state);
final ProgressBar progress = (ProgressBar)view.findViewById(R.id.singleLockProgress);
doorLock = LockState.getValue();
lockName = doorLock.getName();
if (doorLock.isLocked()) {
lockState = getActivity().getString(R.string.door_locks_locked);
singleLockImage.setImageResource(R.drawable.doorlocks_single_locked);
state = true;
} else {
lockState = getActivity().getString(R.string.door_locks_unlocked);
singleLockImage.setImageResource(R.drawable.doorlocks_single_unlocked);
state = false;
}
lockNameText.setText(lockName);
lockStateText.setText(lockState);
singleLockImage.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
getActivity().changeState(state);
}
}
);
}
I thought to call updateUI, which will get the new state from the cache saved after the broadcast event received in Activity, but I am not sure how to pass (view)
Use FragmentActivity instead.
in FragmentActivity :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news_articles);
// Create an instance of ExampleFragment
TestFragment0 firstFragment = new TestFragment0();
// In case this activity was started with special instructions from an Intent,
// pass the Intent's extras to the fragment as arguments
firstFragment.setArguments(getIntent().getExtras());
// Add the fragment to the 'fragment_container' FrameLayout
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, firstFragment).commit();
}
in FragmentActivity for fragments :
TestFragment0 firstFragment0 = new TestFragment0();
firstFragment0.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,firstFragment0).commit();

Using two Buttons on one Activity

I'm still new to android. I have a kids book that l have done with two buttons "READ TO ME" and "READ TO MYSELF".
Read to me onclick will play a recording as it reads the book but it goes to SoundOne Activity.
Read to myself will display text and it leads to PageOne Activity.
How do l create some sort of an "if statement" such that when l click read to me it will play the recording but lead to PageOne Activity. And when l click read to myself it will display the text but still leads to PageOne Activity
This might help reduce the number of classes l have created so far and avoid ANR l assume. Some source code or a tutorial will help Thanks in advance my code is below:
package com.inerds.donkiejoukie;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.content.Context;
public class Fbone extends MainActivity {
MediaPlayer one;
MediaPlayer mb;
MediaPlayer mp;
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.fbone);
one = MediaPlayer.create(this, R.raw.pageone);
mb = MediaPlayer.create(this, R.raw.menubar);
mp = MediaPlayer.create(this, R.raw.pageflip);
ImageButton imageButton1 = (ImageButton) findViewById(R.id.imageButton1);
imageButton1.setOnClickListener (new OnClickListener() {
public void onClick(View vone) {
mb.pause();
mb.stop();
mp.start();
startActivity(new Intent(getApplicationContext(), PageOne.class));
finish();
}
});
ImageButton readtome = (ImageButton) findViewById(R.id.readtome);
readtome.setOnClickListener (new OnClickListener() {
public void onClick(View v) {
mb.pause();
mb.stop();
one.start();
startActivity(new Intent(getApplicationContext(), SoundOne.class));
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 100, 0);
finish();
}
});
}
}
The Intent mechanism is used to communicate from one Activity to another. In the Intent, as well as a class to start, you can specify the name of an action to perform, as well as other options. In this case, you might want to use an extra to tell the following class whether to play sound or not. When you start the intent:
Intent nextActivity = new Intent(getApplicationContext(), SoundOne.class);
// Put true or false in the next line according to which click handler you're in.
nextActivity.putBooleanExtra(SoundOne.extra_enableSound, true);
startActivity(nextActivity);
and then in your SoundOne activity, you need a constant field for the extra name:
static final String extra_enableSound = "enableSound";
and you can find the value of this extra from your onCreate, or wherever you want to start the sound:
if (getIntent().getBooleanExtra(extra_enableSound, false)) {
// start the sound
}
Now your PageOne activity is unused and can be deleted. The documentation for Intent tells you what other information you can stash in there.
I'm not a professional programmer so this might not be the best way to do this, but it works well for me with minimal code.
Here's what I'm doing.
Set an "onClick" for the buttons in my XML
<Button
android:id="#+id/button2"
android:layout_width="144dp"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/button1"
android:onClick="sendMessage1"
android:text="#string/cancel"
/>
the in the activity i set up my buttons
Button button1;
Button button2;
then use a sendmessage instead of onButtonclick
public void sendMessage(View view)
{
Intent intent = new Intent(this, ThirdActivity.class);
startActivity(intent);
}
public void sendMessage1(View view)
{
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
package com.inerds.donkiejoukie;
public class Fbone extends MainActivity {
MediaPlayer one;
MediaPlayer mb;
MediaPlayer mp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fbone);
one = MediaPlayer.create(this, R.raw.pageone);
mb = MediaPlayer.create(this, R.raw.menubar);
mp = MediaPlayer.create(this, R.raw.pageflip);
ImageButton imageButton1 = (ImageButton) findViewById(R.id.imageButton1);
ImageButton readtome = (ImageButton) findViewById(R.id.readtome);
imageButton1.setOnClickListener (handleOnClickListener);
readtome.setOnClickListener (handleOnClickListener);
}
private OnClickListener handleOnClickListener = new OnClickListener() {
public void onClick(View v) {
switch(v.getId()){
case R.id.imageButton1:
mb.pause();
mb.stop();
mp.start();
startActivity(new Intent(getApplicationContext(), PageOne.class));
finish();
break;
case R.id.readtome:
mb.pause();
mb.stop();
one.start();
startActivity(new Intent(getApplicationContext(), SoundOne.class));
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 100, 0);
finish();
break;
}
}
};
}

AspectJ capture button clicked

I want to know whether how to capture the button clicked with AspectJ and get its parameter (eg. button name). I think for having more generalized capturing with AspectJ, it shoudl be used MouseListener so it can capture other UI elements in general!
Example:
In a GUI example I have defined 2 buttons that take some actions
public JButton btn1 = new JButton("Test1");
public JButton btn2 = new JButton("Test2");
btn1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//take some actions
}
}
btn2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//take some actions
}
}
How to capture these buttons with AspectJ, and get their parameters (eg. name)?
It is possible. I have provided two examples. The first that prints out for every JButton that has an ActionListener. The other example only prints out if a specific buttons is clicked.
Prints the text for every JButton clicked with an ActionListener:
#Pointcut("execution(* *.actionPerformed(*)) && args(actionEvent)")
public void buttonPointcut(ActionEvent actionEvent) {}
#Before("buttonPointcut(actionEvent)")
public void beforeButtonPointcut(ActionEvent actionEvent) {
if (actionEvent.getSource() instanceof JButton) {
JButton clickedButton = (JButton) actionEvent.getSource();
System.out.println("Button name: " + clickedButton.getText());
}
}
Prints the text for a specific JButton:
public static JButton j1;
#Pointcut("execution(* *.actionPerformed(*)) && args(actionEvent) && if()")
public static boolean button1Pointcut(ActionEvent actionEvent) {
return (actionEvent.getSource() == j1);
}
#Before("button1Pointcut(actionEvent)")
public void beforeButton1Pointcut(ActionEvent actionEvent) {
// logic before the actionPerformed() method is executed for the j1 button..
}
UPDATED:
You can do this in many different ways. For example add your buttons to the aspect directly. But I prefere to use a enum object between (ButtonManager in this case), so the code does not know about the aspect. And since the ButtonManager is an enum object, it is easy for the aspect to retrieve values from it.
I just tested it with a Swing button class from Oracle and it works. In the Swing class:
b1 = new JButton("Disable middle button", leftButtonIcon);
ButtonManager.addJButton(b1);
AspectJ is extremely powerful when it comes to manipulating classes, but it can not weave advises into specific objects since objects is not created at the time of weaving. So you can only work with objects at runtime and that is why I have added the addJButton(..) method above. That enables the aspect to check the advised button against a list of registered buttons.
The ButtonManager class:
public enum ButtonManager {
;
private static Collection<JButton> buttonList = new LinkedList<JButton>();
public static void addJButton(JButton jButton) {
buttonList.add(jButton);
}
public static Collection<JButton> getButtonList() {
return buttonList;
}
}
Modified pointcut and advice to only print the name of the buttons registered in the ButtonManager:
#Pointcut("execution(* *.actionPerformed(*)) && args(actionEvent) && if()")
public static boolean buttonListPointcut(ActionEvent actionEvent) {
Collection<JButton> buttonList = ButtonManager.getButtonList();
JButton registeredButton = null;
for (JButton jButton : buttonList) {
if (actionEvent.getSource() == jButton) {
registeredButton = jButton;
}
}
return registeredButton != null;
}
#Before("buttonListPointcut(actionEvent)")
public void beforeButtonListPointcut(ActionEvent actionEvent) {
JButton clickedButton = (JButton) actionEvent.getSource();
System.out.println("Registered button name: " + clickedButton.getText());
}
UPDATED 2
Okay, I believe I understand what you want. You want to listen to mouse events. That is possible. The downside is that you have to register all your GUI components that you want to listen for clicks with a mouse listener. It is not enough to register the JPanel of the JFrame with a MouseListener. So if you only have registered an ActionListener for your buttons, you also have to add a mouse listener.
I have created a quick solution that works for me. It only shows that it works. I have not tried to make the solution generic with many different GUI objects. But that should be quite easy to refactor in when you have got the basics to work.
In the Swing class:
private class MouseListener extends MouseInputAdapter {
public void mouseClicked(MouseEvent e) {}
}
In the init method of the Swing class:
MouseListener myListener = new MouseListener();
btn1.addMouseListener(myListener);
btn2.addMouseListener(myListener);
In the Aspect class:
#Pointcut("execution(* *.mouseClicked(*)) && args(mouseEvent)")
public void mouseEventPointcut(MouseEvent mouseEvent) {}
#Before("mouseEventPointcut(mouseEvent)")
public void beforeMouseEventPointcut(MouseEvent mouseEvent) {
if (mouseEvent.getSource() instanceof JButton) {
JButton clickedButton = (JButton) mouseEvent.getSource();
System.out.println("aspectJ --> mouseClicked: " + clickedButton.getText());
}
}
This results in the following output in the console:
aspectJ --> mouseClicked: Test1
I hope it helps!

Resources