Communication between Socket Class and UI - java

I am developing an android application that needs to transmit some data through sockets. At this moment it connects to the socket, send messages when clicking the interface buttons, but I want to alert the user with a AlertDialog on my interface when the socket class receives a message. I've been searching and found information about Handlers but I could not use them. How can I solve this problem?
Thanks

MainActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
// Build an alert dialog
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this)
.setTitle("Title")
.setMessage("Message")
.setCancelable(false)
.setPositiveButton("Done", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// Close the dialog
dialog.cancel();
}
});
// Create the alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// Show the alert dialog
alertDialog.show();
}
});

Related

Exit button that can ask you "Do you want to close it"?

I'm a beginner in java and I have a question for you do you know how I can create the button of exit? This button can ask me before I close the application "Do you want to close this application? or "Are you sure to close it?" I need to do it for my project and I need help. Pls send me some code.
Your question is very broad, however, an AlertDialog is what you are looking for, this is the implementation:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MyActivity.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
1.- Search how create a dialog on Android.
Link -> https://developer.android.com/guide/topics/ui/dialogs?hl=es-419
2.- In your view put a back button with onClickListener, insert showDialog created inside.
3.- Implement the override onBackPressed method, insert showDialogCreated inside.
Example onBackPressed
public void onBackPressed() {
var dialog = CustomDialog.newInstance();
dialog.setCancelable(false);
dialog.show(this.getSupportFragmentManager(), "TAG");
dialog.setOnClickListener((whichViewID, tag, args) => {
// Your Logic
// If is pressed positive button call super.onBackPressed else dialog.dismiss()
});
}

how to turn on Location without permission or Alert in android studio

I wanna to make location Turn on every time i want With Socket.
my problem is what permission should i get of user in first open app .
i dont want show location Alert Dialog for Accept or Delince, just i want one Accept until user uninstall app.
this is my code with Alert dialog
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("لطفا دسترسی به مکان خود را فعال نمایید.")
.setCancelable(false)
.setPositiveButton("باشه", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final int id) {
startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
})
.setNegativeButton("نه", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final int id) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();

How do I detect when user closes ussd dialog in Android?

I'm running ussd code from my code. After it returns the results the user will press the ok button to close the the dialog. I want to detect when the user does this.
Let's assume that this is your dialog code:
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.AppCompatAlertDialogStyle);
builder.setTitle("dialog's title");
builder.setMessage("dialogs's text");
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//do stuff when user presses ok button
}
});
builder.setNeutralButton("CANCEL", null); //same here just add the listener
AlertDialog dialog = builder.create();
dialog.show();
//you can use neutralButton as well

Alert dialog not working correctly - android

I am using an alert dialog to ask the user for confirmation of an item addition to an array. I am printing the array size before and after the alert dialog and it seems that both printings are done before the alert dialog shows up.
Log.d("before", ""+wayPoints.length);
AlertDialog.Builder ab = new AlertDialog.Builder(getContext());
ab .setTitle("Add entry");
ab .setMessage("Are you sure you want to add this entry?");
ab .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{wayPoints = ArrayHandler.addAtIndex(wayPoints, node, 1);}
});
ab .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
});
ab.setIcon(android.R.drawable.ic_dialog_alert);
ab.show();
Log.d("after", ""+wayPoints.length);
The ArrayHandler.addAtIndex method handles the addition of a new item to the provided array.
The problem is it is printing both logging lines before the alert dialog shows up. I need the alert dialog to be done before the printing of the second log.
This is correct behavior. The show method does not wait or block until the dialog is dismissed. Code execution continues after the show method immediately after the dialog is shown. (Actually, the dialog isn't even displayed yet - that happens after you yield control).
You already have setPositiveButton and setNegativeButton handlers, just put the code you want to execute in those handlers when user presses a button.
If you want certain code to run when the dialog is closed regardless of which button is tapped, use setOnDismissListener and put your code there.
Use:
AlertDialog alert1 = ab.create();
alert.show();
Instead of:
ab.show();
Try this
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder( context);  
// set title
alertDialogBuilder.setTitle("Your Title");
  // set dialog message
alertDialogBuilder .setMessage("Click yes to exit!") .setCancelable(false) .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
MainActivity.this.finish(); }
}) .setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
wayPoints = ArrayHandler.addAtIndex(wayPoints, node, 1);
Log.d("after", ""+wayPoints.length);
} });
});  
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();  
// show it
alertDialog.show();

Android alert dialog Issue

In my android application, I use one alert dialog to display some information to the user, and if the user click the dialog , it should finish the activity. My code is
offer.this.runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
AlertDialog alert=new AlertDialog.Builder(offer.this).create();
alert.setTitle("SVSugar Mill");
alert.setMessage("Offer Number is "+offer_no.getText().toString());
alert.setButton("Click to Dismiss", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
finish();
//return;
}
});
alert.show();
}
});
It doesn't wait for the user response to finish(). Instead it will be called even if the user didn't click the Alert dialog. I know this is asynchronous, but I need to do this.(The OfferNO should be displayed to the user. When the user click the alert dialog it should finish the activity). Is there any way to do this?
Someone help me
Edit:
The activity will be finished without waiting for the user to click the alert dialog
public void ShowDialog(final Context context) {
new AlertDialog.Builder(context)
.setTitle(android.R.string.dialog_alert_title)
.setMessage(UContext.getContext().getString(R.string.network_error))
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
this.finish();
}
}).show();
}
The problem is that you created two AlertDialog instances here:
alertDialog=builder.create();
builder.create().show();
Then you called dismiss() on the dialog that is not actually shown. This should fix the problem:
alertDialog=builder.show();

Categories

Resources