Background
So I've inherited an android application that in various places connects to web services to send or receive information.
As it is right now, when a connection fails the user of the android device is prompted with an error message that for most users are viewed as obscure.
The problem
So what I'm looking for is a pattern or suggestion on how to implement a solution in this android application that catches all java connection exceptions and presents a dialog telling the user he lost the internet connection and needs to try again.
Any ideas? :-)
Edit, current idea
My current idea to solve this issue, or making it a bit more managed instead of having a fractured exception handling that takes care of the problem at every possible connection is this.
Each communication instance/class will inherit from a super class, or implement an interface that forces this class to be used. This class contains the logic for connection exceptions. Lets just for now call it ConnectionExceptionManager.
The problem then only becomes implementation of this in each communication class in each web service call. In each web service calls exception I need to check for communication exceptions and if it hits, use the ConnectionExceptionManager in this catch.
This would however still make it a hassle, to copy into request method in each communication class.
Any ideas about improving this current idea of mine?
What I understood from your question is that you need to check internet connectivity before performing any internet related action. For that you can simply create public static boolean function i.e. public static boolean isInternetConnected(){...} in global class so that you can reuse your function.
If you want your application to check internet connectivity automatically then you can use IntentService which will work in background and in its protected abstract void onHandleIntent (Intent intent) you can check isInternetConnected() returns true and if not then you can open AlertDialog.
To use this background service after repeated intervals while application is running, you can use AlarmManager.
Hope this helps.
Related
So I am trying to set up a system for multiple phones being connected to a device that handles phonecalls. In order to do so, I've been investigating the getUserSelectedOutgoingPhoneAccount() method, which should help me differentiate between the phone making the calls and the rest of the phones that are connected.
However, when I try to use that method with my instance of a Telecom Manager, it does not appear in the list of callable methods. I can see things like "getDefaultOutgoingPhoneAccount()" and "getCallCapablePhoneAccounts()", but nothing about getting or setting the user's selection. According to the android documentation, it's public and callable from a Telecom Manager. This manager is already set up in such a way that it can make phonecalls and such just fine, so I assumed this method would appear.
EDIT: This is how the telecom manager is initially defined; could this be why I'm not able to use the method in question?
Should I use a new telecom manager instance? Is it possible I'm missing something else, like an import? The current import for Telecom Manager is "android.telecom.TelecomManager".
Answer:
After looking into it some more, it appears my application is running on a lower API Level than this method has been implemented in (Android 8.1.0, which is API Level 27). This method requires API Level 29 or higher in order to call it.
I'm curious if it's possible to trigger a specific piece of code when a REST API its values changes. Actually a sort of realtime update a mechanism.
If it's not possible, what's a better to do it?
The idea is when I push on a button (on my android device) a text will appear in a game (Java Desktop).
Thank you!
Your question is not clear please rephrase it.
As you said you want that has to be done realtime.
Possible three approaches:
In android there are connection APIs with which you can connect your desktop(must have WiFi enabled) with phone. Then you can send or receive files and messages directly.
The other way is to have Push notifications but it's not guarantees realtime fast some time gets delayed otherwise performs good always.
Other way is to keep the connected socket open for listening changes from phone side to desktop side once connected but it's more costly.
Hope this gives you a direction, rest you can search over internet.
Why not just make a simple setter?
When you get a rest command to update something (for example some int value) you can do:
public void setValue(int newValue)
{
value = newValue;
doAction(newValue);
}
define the doAction as you want
If you need asynchronous update, use the thread. Learn multithreading
Check the link to see if your requirement could be completed with the java thread concepts. http://www.javatpoint.com/multithreading-in-java
I'm not asking about repairing my code or something I've just have a problem on where to or how to put methods in proper places in my application.
I wrote an application enhancing bluetooth chat - I made service for this bluetooth chat that runs in background. I will have more such services. Basically I want to be able to run methods across whole my application:
send message via bluetooth chat and wait for answer,
scan RFID tag with NFC,
scan Barcode with camera,
etc.
Each of this I know how to do in 1 activity easily. Now I'm looking for a solution to put this in something like a global class that will allow me to call this methods across my whole application - so I don't have to initialize anything but just - doSomething() and it does it.
Where should I put such things:
in custom activity class (all my other activities will use it)?
in application?
something else?
The same applies to handlers. Basically as to bluetooth chat you have to make handler to listen to received messages - where to put it as well.
I'm basically looking for propositions on how to solve this.
You can have one BaseActivity that extends activity and put your common functions in BaseActivity, now extend all other activities with BaseActivity.
Your activity will have these functions available.
For example:
class BaseActivity extends activity{
...........
public void sendMsgViaBluetooth(String msg){
...........
}
}
class MyActivity extends BaseActivity{
<OnSomeEvent>{
sendMsgViaBluetooth(msg);
}
}
I would create a class with some static methods that allows you to obtain instances of certain classes handling different functionality. You can create a listener system where multiple activities can register themselves for events, such as received messages. When the main class then receives something, it will go through all listeners, and inform them.
Otherwise, you can also send broadcasts, and let those activities interested listen for them. A problem here though is that no one is listening, messages might be lost. When you handle it yourself, in case no one is listening you can store messages in a queue, and send them when new listeners register.
I would not go with the BaseActivity idea. The bigger your app gets, the harder this becomes (e.g., what if you want a Fragment to do things as well, a service that should obtain something, or when you want to implement other classes that require to extend anything else than Activity).
To have data/methods that "follows" you all over the scope of your application, create a Class that extends Application. You have to specify in your Manifest that this Class will be your Application class.
android:name="com.example.MyApplicationClass">
After that, you can call getApplication() to get the Application context and cast it to your Class
myAppContext = (MyApplicationClass)getApplication();
Try avoiding Singleton pattern. I've done one in an application and when the app became big enough, my singleton would get erased sometimes (when resuming the application after coming back from launcher). The application context is supposed to keep the data even if the application is in background for a long time.
When a single activity crashes, all the activities in flow crashes.
As all activities crash, each generates its own error dialog, so can we have a single dialog for all crashes?
Try catch is already implemented.
You use a singleton static final object to handle all your diablog boxes across all your activities. One such singleton object is the application object, see http://developer.android.com/reference/android/app/Application.html, but you don't have to use that one if you don't want to.
Also, you have to make sure that try/catch is implemented absolutely everywhere, wherever the android operating system makes calls into your app code. I'll find that I've got try/catch in most places in my code, but somehow when I'm trying to develop quickly I accidentally miss a few places and of course they're the ones that end up causing me a problem!
I have a binder service and a client that live in different processes. Using AIDL, when the client calls into my remote binder service, there are times that I need to relay an error (exception) back to the client.
However, from my understanding, this is not possible. I tried throwing a "RemoteException" from my binder service to see what will happen, and I get
Uncaught remote exception! (Exceptions are not yet supported across processes.)
in my logcat.
Since it looks like this is not possible, what is the best approach for informing the client of an error? I was thinking I can just convert my AIDLs to use C-style interfaces in which I just return an error code (and 0 on success), but this looks ugly.
Is there a better approach?
Your remote method can return a Parcel that contains the result data or an Exception if there is an error. See the Parcel#writeException method. I believe that this is how Android exceptions make it back when performing actions on a ContentProvider that lives in another process. There are many ways to return the result data including using the Bundle class.
Your manager class can hide the implementation details by unparcelling and returning the data or throwing the unparcelled exception so users never interact with the Parcel.
Here is a link to the source for Parcel#writeException.