What is the proper way to handle a screen rotation and the media route button and the existing session? - google-cast

The example on https://developers.google.com/cast/cast_2nd-screen_app_tutorial shows a onDestroy method which calls unregisterMediaRouteProvider. This causes the MediaRouter.Callback.onRouteUnselected method to get called which in turn ends the session. This leads to the app getting disconnected from the chromecast device and the MediaRouteButton stops being blue. Below is the onDestroy method from the example:
#Override
protected void onDestroy() {
MediaRouteHelper.unregisterMediaRouteProvider(mCastContext);
mCastContext.dispose();
super.onDestroy();
}
So my question is, what is the proper way to handle screen rotation when using the chromecast device from an app?

You can try using isFinishing() method of Activity to figure out if onDestroy is called due to application really "finishing" or is called for other reasons. Another option is to handle orientation change yourself.

You can see the guidelines for handling setup/destruction of the Chromecast (such as when orientation change is happening) on https://developers.google.com/cast/docs/android_sender
The relevant sections of code are the following ones:
#Override
protected void onResume() {
super.onResume();
mMediaRouter.addCallback(mMediaRouteSelector, mMediaRouterCallback,
MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY);
}
#Override
protected void onPause() {
if (isFinishing()) {
mMediaRouter.removeCallback(mMediaRouterCallback);
}
super.onPause();
}
And also the following code:
#Override
protected void onStart() {
super.onStart();
mMediaRouter.addCallback(mMediaRouteSelector, mMediaRouterCallback,
MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY);
}
#Override
protected void onStop() {
mMediaRouter.removeCallback(mMediaRouterCallback);
super.onStop();
}
It is also a good idea to have a boolean value in your program, for instance "wasLaunched" or "isConnected" to keep track of whether the connection to the chromecast is active or not. I use this variable in my code to check if I can send messages to the receiver or not. Then simply remember to save this variable and restore it when there is an orientation change on the device. This works for me in my chromecast enabled app. The code for saving/restoring my variable, so it survices orientation change is shown below:
protected void onSaveInstanceState(Bundle bundle) {
if (bundle!=null)
{
bundle.putBoolean("wasLaunched", wasLaunched);
}
super.onSaveInstanceState(bundle);
}
protected void onRestoreInstanceState(Bundle savedInstanceState) {
if (savedInstanceState!=null)
{
wasLaunched = savedInstanceState.getBoolean("wasLaunched");
}
super.onRestoreInstanceState(savedInstanceState);
};
Of course you can also put other stuff in your bundle that needs to survive an orientation change. I am not using the onDestroy override you describe there, nor is it mentioned in the google documentation I link to. But I use the teardown() method described in that document I link to for cleaning up, but this only happens when I close the connection, because I dont want to close the connection to the chromecast upon orientation change.

Related

Action listeners not firing

I've been developing with codenameone for over a year, and I never ran into this problem before, I feel like I'm losing my mind. I just redesigned one part of an app I'm working on, and now the ActionListeners aren't firing. I'm attaching them to a Button and a SpanButton in the code:
ActionListener goToDoc = new ActionListener() {
String mDocId = docId;
#Override
public void actionPerformed(ActionEvent evt) {
mStateMachine.currentExpertId = mDocId;
mStateMachine.showForm("DoctorDetails", null);
}
};
name.addActionListener(goToDoc);
Util.downloadImageToStorage(mStateMachine.URL_PREFIX+"images/doctors/"+(String)value.get("doc_pic"),
"doc_post_pic_"+(String)value.get("doc_id")+".png", new Callback<Image>() {
#Override
public void onSucess(Image img) {
pic.setIcon(img.scaledWidth(mStateMachine.getProportionalWidth(.23)));
StateMachine.applyGreenBorder(pic);
pic.addActionListener(goToDoc);
pic.getParent().revalidate();
}
#Override
public void onError(Object sender, Throwable err, int errorCode, String errorMessage) {
System.out.println("Unable to download expert profile picture");
}
});
When I debug the code, the components do show that the ActionListener is attached, but the actionPerformed method is never reached, no matter how many times I click on the buttons. I experience this problem both on the simulator and on an Android device. I have yet to test on an iPhone.
Did you set a parent to be a lead component or focusable?
The reason the click event wasn't firing was because the Components weren't enabled, possibly a bug in the BUI Builder. After checking off the 'Enabled' and 'Focusable' checkboxes in the GUI Builder, and seeing they were unchecked every time I went back to that form, I just used component.setFocusable(true) and component.setEnabled(true) in the code, and it worked fine after that.

KeyListener doesn't work, my Jframe can not recognise my keyboard

I dont know why my application can not detect my keyboard.
Application its a JFrame who implements SerialPortEventListener and KeyListener.
In the Jframe of my application I have 1 Tabbed Pane with 2 Panels.
When I press some buttons it doesn't work( NOTHING IN THE OUTPUT).
I will show you my code(Not all, just the most importants parts) :
MY CLASS:
public class java_arduino_frame extends javax.swing.JFrame implements SerialPortEventListener, KeyListener {
MY KEYS FUNCTIONS:
public void keyTyped(KeyEvent e){
throw new UnsupportedOperationException("not yet");
}
public void keyPressed(KeyEvent e){
int keyCode;
keyCode = e.getKeyCode();
System.out.println(keyCode);
}
public void keyReleased(KeyEvent e){
throw new UnsupportedOperationException("not yet");
}
MY CONSTRUCTOR OF MY CLASS:
public java_arduino_frame() {
getContentPane().setBackground( Color.LIGHT_GRAY );
initComponents();
addKeyListener(this);
}
THE MAIN
public static void main(String args[]) {
strong text
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new java_arduino_frame().setVisible(true);
}
});
A KeyListener will only respond when the component it is registered IS focusable AND has focus, since clicking a button will cause the focus to be transferred to the button, the frame will no longer have input focus.
It's also unlikely that the frame would be able to receive keyboard focus directly either.
As a general rule of thumb, you should be using the Key Bindings API instead, which will provide you with the ability to over come the focus realated issues.
Depending on what you're doing, you might also consider using a JTextArea or a JTextField with a DocumentListener, assuming you want to send characters to through the serial connection. Have a look at Listening for Changes on a Document for more details

Update UI while working on background

I have an update database operation, that has an activity, which keeps updating the percentage and it runs inside an AsyncTask.
Inside doInBackground() I call the controller that updates the database and keep updating the percentage on the activity, however, if I press home button or back button, the operation is cancelled. What u suggest me to do?
I was trying to start a Service inside doInBackground() so it would run in background, but it looks like its not working.
My code looks like this:
public class UpdateDatabaseAsyncTask extends AsyncTask<Void, Integer, Integer>
{
#Override
public void onPreExecute()
{
mCustomProgressBar.startAnimation();
}
#Override
public Integer doInBackground(Void... params)
{
return mController.updateDatabase();
}
#Override
public void onPostExecute(Integer result)
{
mCustomProgressBar.stopAnimation();
// finish the activity
}
#Override
public void onProgressUpdate(Integer... value)
{
updatePercentageValue(value[0]);
}
public void callPublishProgress(Integer value)
{
publishProgress(value);
}
}
And inside the controller I call the method callPublishProgress(value) passing the current percentage value, so it will publishProgress(value) in the UI.
I was debugging, and I pressed the home/back button, and it just stopped running the worker thread.
Another solution I tried, was starting a Service to run in background no matter the user press home/back button or not, so I thought, and the Service would make a call to the controller method that does the work, and it would call the callPublishProgress(value) to update the percentage value on the UI anyways.
However, what was happening is, the code reach doInBackground() and start the service, but it goes to onPostExecute() immediately, it just didn't wait the service to finish(of course!). So it gives a NullPointerException. I thought of making a loop inside doInBackground() with a flag set in the Service, so it would leave this loop while the service hasn't been finished (I was using an IntentService), but it didn't work anyways.
I thought of using a Timer too. But I don't know.
I was reading the articles in documentation about Threads, etc. And it suggests using AsyncTask, just as I was trying to do. It also talks about runOnUiThread(Runnable).
Anyways what I need is to make an operation in background(probably using an IntentService), so no matter if the user press the home button, it will keep running, but it must update the percentage on the UI, and when the user leave the screen and back to it, it shows the current percentage value updated in the screen.
What is the best solution for my case?
Thanks.
public class MyServce extends Service{
public static final String BROADCAST_ACTION = "com.myapp";
Intent intent;
private final Handler handler = new Handler();
#Override
public void onCreate()
{
super.onCreate();
intent = new Intent(BROADCAST_ACTION);
}
#Override
public void onStart(Intent intent, int startId) {
handler.removeCallbacks(sendUpdatesToUI);
handler.postDelayed(sendUpdatesToUI, 1000); // 1 second
}
private Runnable sendUpdatesToUI = new Runnable() {
public void run() {
DoYourWorking();
handler.postDelayed(this, 1000); // 1 seconds
}
private void DoYourWorking() {
........
........
intent.putExtra("key", progress);
sendBroadcast(intent);
}
};
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onDestroy() {
super.onDestroy();
handler.removeCallbacks(sendUpdatesToUI);
}
Now in your Activity register broadcast to service
private BroadcastReceiver brodcast = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//intent.getWhatever
// update your progress
//progressbar.setProgress
}
register broadcast
registerReceiver(brodcast, new IntentFilter(MyService.BROADCAST_ACTION));
This worked for me. I started a background service on a thread that just fetches the values and updates an object in a singleton.
In the view controller, I start a timer that keeps updating the view by fetching data from the object in singleton.
I had a little problem understanding your entire question text, so I'm not sure if you have tried this. But this is what worked. Also, the service was started with START_STICKY
Use an IntentService (which is a Service on a thread of its own), and Handler to pass the data back to the Activity.

Creating a JavaFX Dialog Box

I am coding a javafx program and i need to create and use my own Stage based (Javafx.Stage) dialog box for showing messages and confirmations. I have written all the necessary code by now but i have a problem:
A dialog box must stop execution of rest of the code until a respond is given like "yes" or "no" or "retry". When i use my dialog box like "DialogBox.ShowMessage", a stage appears with message and buttons. But, as you may think, the rest of the code continues to execute. How can i get around this? When i create the stage, it must stop the other threads or the thread that it depends on. I have searched through internet and here, but i can not find exact solution. One idea is using "javafx.event.EventDispatcher" or "javafx.event.EventDispatchChain" but i couldn't figure out how to use them. And another idea is using "java.awt.EventQueue". And here is somthing that can help: I have a control of stage show and hide events and showing or hiding eventhandlers. I think som sort of thread queue can be used in one of these spesific sections.
I hope i clarified the situation enough. Briefly, ı need to manage threads while using another stage with my own code.
Thank you.
About execution suspending there is a Jira issue for it http://javafx-jira.kenai.com/browse/RT-19783.
As a workaround, I have no idea how to use EventDispatcher and EventDispatchChain to overcome this problem but you can send the EventHandlers as parameter. For instance,
public static void ShowMessage(final EventHandler<ActionEvent> okAction, final EventHandler<ActionEvent> cancelAction){
// Define and add buttons to the "view" and show message
btnOk.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
okAction.handle(null);
}
});
btnCancel.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
cancelAction.handle(null);
}
});
}
and use it as,
DialogBox.ShowMessage(
new EventHandler<ActionEvent>() {
#Override public void handle(ActionEvent arg0) {
// Do stuff when "ok" clicked.
},
new EventHandler<ActionEvent>() {
#Override public void handle(ActionEvent arg0) {
// Do stuff when "cancel" clicked.
});
I agree with this is a kind of "winding" way however.
Siteye hoş geldin ve kolay gelsin.

How can I develop a custom "non-fullscreen" softkeyboard for tablets?

I have the sample of softkeyboard. When I install it on a android 2.x, it has the following non-fullscreen view:
But when I install it on tablet 10in it gets whole of the screen like this:
How can I change the sample to have a non-fullscreen custom keyboard on tablet pcs like this image (default keyboard of tablet)?
The following code in InputMethodService solved my problem:
#Override
public boolean onEvaluateFullscreenMode() {
return false;
}
overide the following two methods from InputmethodService as bellow, this works always
#Override
public void onUpdateExtractingVisibility(EditorInfo ei) {
// TODO Auto-generated method stub
setExtractViewShown(false);
}
#Override
public boolean onEvaluateFullscreenMode() {
return false;
}
This worked for me :) and if you want to show keyboard in fullscreen mode always then change both false to true.

Resources