I'm developing an android app which I want to check if there's internet connection, if it's not the case display a warning message, and when there's internet connection again load a certain url.
It can be said that both displaying the message and checking there's internet connection work... but no separately.
I mean I have the following code:
if (!checkConnectivity())
{
browser.stopLoading();
LayoutInflater layoutinflater = LayoutInflater.from(app);
final View textEntryView;
textEntryView = layoutinflater.inflate(R.layout.exitdialog, null);
new AlertDialog.Builder(app)
.setView(textEntryView)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Exit")
.setNegativeButton("CANCEL", null)
.show();
while (!checkConnectivity())
{
}
browser.loadUrl(url);
}
If I don't use from while (!checkConnectivity()) to browser.loadUrl(url); then AlertDialog shows properly.
If I don't use from LayoutInflater layoutinflater = LayoutInflater.from(app); to .show(); then the app loads the web page correctly on browser.
But if I use the whole code it looks like it enters the while (!checkConnectivity()) loop before it does the show as what happens when internet connection is restabilished is that the alert dialog is shown briefly and the the web page is loaded.
I find this pretty weird, as I'm not using, as far as I know, threads that could cause that one could be executed before other one, and this is not the case this thing doesn't fulfill one of the most basic things in programming, I mean if an instruction is before another one, the one that's before should be plenty executed logically (except for threads).
Hope someone has an idea about this and can help me.
Your while loop blocks the UI thread and the dialog has no chance to get displayed.
Busy-looping to poll for a condition is also a big red flag.
Consider waiting for a connectivity event, and use a cancellable progress dialog instead of an an alert dialog to disable other UI until the event is received.
Related
I have an Android app that serves content via webview. I've implemented a network connectivity alert so if my users are in a location where they can't get internet, I show a message. This takes place inside the onReceivedError method. It works as expected; if I disable all internet connections, I get a warning message. However, even when internet connection exists, the warning message still shows. If I minimize my app, open the native browser and visit 8-10 links, when I switch back to my app, I am given the network warning even though the device is connected to the network.
This only happens on one of my 6 test devices (my current phone, a Samsung Galaxy S10e. The problem does not occur on 5 different Android devices, both newer and older. I'm just looking for a way to ignore the warning when the app is in the background state. I've searched for an easy way to do this, but I haven't found anything simple that doesn't involve writing a new class.
How can I write a conditional statement to do nothing when the onReceivedError method is triggered while the app is in the background? Here is a portion of my code, along with what I would like to have happen:
#override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
// the part I need help with
if ("app in background"){
// do nothing
} else {
// this is where I have employed my notification, a snackbar
}
}
Thank you for your help!
As soon as I receive a push notification from my app I want to trigger the KeyguardManager to launch the fingerprint/pass code screen to open the phone from lock screen so that person can enter the phone and unlock the device.
I want to trigger this programmatically similar to when we click on any notification from lock screen we get the fingerprint/pass-code screen.
I did a lot of RnD but didn't find any solution, this is one of the challenging use case task given to me in class, I have been exploring a lot from quite few weeks with no success at all.
Did tried Broadcast receiver with BiometricManager and many things with no success, any lead will be very helpful.
As soon as you receive push message, onNotificationReceived() (or some other method if you use some 3rd party libs) method gets called as below. from there, you can launch your Main screen where you have written biometric/unlocking code.
class MyReceiver : PushReceiver {
override fun onNotificationReceived(message: Message) : Boolean {
//Launch your MainActivity where you can show Unlock screen.
return super.onNotificationReceived(message);
}
}
I'm developing an application that among other things , lets user interact with it by voice recognition (Speech to Text).For example user speaks the word "contacts" and the application can recognize and proceed to use logic to it to do something (i.e get the list of contacts and present them).
Now I have used a custom speech Recognizer , and not the one Google provides with the alert dialog.
I also have an animated gradient as a background with some colors.
From the RecognitionListener we get either an error as a response or we get results from the method onResults(Bundle results).In both cases after the response i need to restart the listener in order for the user to continue to interact with the application. However, when my speechRecognition is destroyed and created again, my application "blinks"(very quick black screen, but then the Ui is returned to normal).
I know that the problem is happening because i try to destroy my speechListener and then i re-create it(By calling the function restartSpeechListener), but i cannot seem to find a way to solve this "blinking".
Below is my code for the speechRecognizer initiation and restart :
public void startRecognising() {
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
mySpeechRecogniser listener = new mySpeechRecogniser();
Intent mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE,
"el");
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
this.getPackageName());
mSpeechRecognizer.setRecognitionListener(listener);
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
startActivity(mSpeechRecognizerIntent);
}
// Restart speech recogniser
public void restartSpeechListener() {
mSpeechRecognizer.stopListening();
mSpeechRecognizer.destroy();
startRecognising();
}
Any suggestions or help would be very helpful !
Good day friends of mine.
I want to add a functionality to my ANDROID application, which I think it is new to me... but it is new in the development world. The application runs very well at the moment... But I need to add this functionality as explained below.
Please see the sample picture below...which tells exactly what I want to do.
I want to check network connection state on Application Startup.
If connection state is true, then, the application should load the MainActivity.xml.
If connection state is false, then, the application should return a new String ("Error") message.
Please see my code sample below...in the onCreate() {} thread.
NOTE: I am not proficient in android development please.
#Override
protected void onCreate(Bundle savedInstanceState) {
cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
activeNetwork = cm.getActiveNetworkInfo();//this is line 91...
Log.d("Network Info", "" + activeNetwork);//log the connection info
if (activeNetwork != null && activeNetwork.isConnected()) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initialize the status bar textview control
statusBar = (TextView) findViewById(R.id.textViewStatusMsg);//status bar of the UI
titleLogin = (TextView) findViewById(R.id.textViewLogin);//title text of the UI
If the above condition is TRUE, the application runs well but if the following condition below is FALSE,
the application crashes with ANDROID's error message "Unfortunately, App has crashed!".
Instead of it giving me its own message and crashing, I want to kill the app programmatically using the else...statement below.
} else {
data = new String("NO NETWORK CONNECTION! TRY AGAIN...");
return; //return the message data and kill the APP's UI here
}
}
Any help will suffice please... I am not well vested in android programming and development...
I guess the reason for crashing is because of misplaced of super.onCreate method.
Move this statement outside if block and either you can do a toast message or show an alert dialog.
If above solution didn't work, comment the crash stacktrace.
How can I register for SMS database changes?
I tried:
mCursor = mActivity.getContentResolver().query(Sms.CONTENT_URI, new String[] {
Sms.ADDRESS
}, null, null, null);
mCursor.registerDataSetObserver(mydataSetObserver);
where mydataSetObserver is implemented like this:
private class MyDataSetObserver extends DataSetObserver {
public void onChanged() {
System.out.println ("1");
}
public void onInvalidated() {
System.out.println ("2");
}
}
But when I tried sending a SMS message in the emulator,
MyDataSetObserver never get called.
Can you please tell me why?
Thank you.
It sounds like all you are trying to do is have the ability to make changes to the SMS database on the device.
The way I have done it in the past is by using tags in the AndroidManifest.xml. The application I made needed to use the READ_SMS permission as well as the READ_CONTACTS permission, however gaining permission for writing to the database would be done in the same way.
I defined these desired permissions in the AndroidManifest.xml file with the following tag:
Included in the list of permissions you can use is WRITE_SMS, which should give you the desired capability.
Please note: because I am a new user, StackOverflow would only let me post one hyperlink for this post, I tried including alot more information however was unable to do so. Please go to the android developer website and search for the AndroidManifest.xml file and see more info if need be.
DataSetObservers only observe DataSetObservables they are registered with. Your MyDataSetObserver is registered with your mCursor and will be notified whenever mCursor changes (e.g. after requery) but not when the content is written by another process (like the Messaging application).
Unfortunately there is currently no good way to listen for the event of sent text messages, the best alternative seems to be polling content://sms/sent, potentially using a ContentObserver.
This question is related.