I am new in Android and I am attempting to Send an SMS through an Android application. I need it such that if the Message Contains the Word "MYSELF" Prompt the user whether they want to go ahead to send the SMS My Method is :
public void sendSmsByManager() {
try {
// Get the default instance of the SmsManager
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber.getText().toString(),
null,
smsBody.getText().toString(),
null,
null);
//If the SMS Contains the WORD MYSELF Prompt the User If they Want to send the SMS
Toast.makeText(getApplicationContext(),
"Your sms has successfully sent!",
Toast.LENGTH_LONG).show();
} catch (Exception ex) {
Toast.makeText(getApplicationContext(), "Your sms has failed...",
Toast.LENGTH_LONG).show();
ex.printStackTrace();
}
}
How can I achieve this?
Create an Alert Confirmation style dialog.
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Some Message");
builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//send text
}
});
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//discard text
}
});
AlertDialog dialog = builder.create();
dialog.show();
This will work for you :
public void sendSmsByManager() {
try {
//If the SMS Contains the WORD MYSELF Prompt the User If they Want to send the SMS
if (smsBody.getText().toString().contains("MYSELF")){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("SMS CONTAINS MYSELF! Do you really Want to send this SMS"); // It will set the message you want to display
builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Get the default instance of the SmsManager
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber.getText().toString(),
null,
smsBody.getText().toString(),
null,
null);
Toast.makeText(getApplicationContext(),
"Your sms has successfully sent!",
Toast.LENGTH_LONG).show();
}
});
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
} catch (Exception ex) {
Toast.makeText(getApplicationContext(), "Your sms has failed...",
Toast.LENGTH_LONG).show();
ex.printStackTrace();
}
}
Related
If the location is disabled I want to show box and stop the execution process until I turn ON the location and come back to my app. Please Help with necessary suggestions.
function,
alertDialog.setTitle("GPS Setting");
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
alertDialog.setPositiveButton("OK ", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
});
alertDialog.setNegativeButton("Cancel ", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
Toast.makeText(mContext, "Sorry we cannot proceed", Toast.LENGTH_SHORT).show();
}
});
alertDialog.show();// Showing Alert Message
You should use ProgressDialog at https://developer.android.com/reference/android/app/ProgressDialog.html
see this example:
private ProgressDialog progressDialog;
public void cerrarSession() {
try {
showDialog();
// do something
} catch (InternetException e) {
// some exception
e.printStackTrace();
}
}
private void showDialog() {
progressDialog = new ProgressDialog(SavingsRequestsManager.getActivity());
progressDialog.setCancelable(false);
closeDialog();
progressDialog.setMessage(SavingsRequestsManager.getActivity().getString(R.string.progress));
progressDialog.show();
}
public void closeDialog() {
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
I am trying to show an alert message.
But when I click the specific button to show the alert message the app stops unfortunately.
my code for alert message is here...
search.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Cursor res = myDB.getAllData();
if (res.getCount() == 0) {
showMessage("Error", "Nothing found");
return;
}
StringBuffer buffer = new StringBuffer();
while (res.moveToNext()) {
buffer.append("EMAIL: " + res.getString(0) + "\n");
buffer.append("full_name : " + res.getString(1) + "\n");
buffer.append("district : " + res.getString(2) + "\n");
buffer.append("phone_num : " + res.getString(3) + "\n");
}
showMessage("Data", buffer.toString());
}
});
}
public void showMessage(String title, String message)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
Try this code :
AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
builder1.setMessage("Write your message here.");
builder1.setCancelable(true);
builder1.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
builder1.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert11 = builder1.create();
alert11.show();
instead of
AlertDialog.Builder builder = new AlertDialog.Builder(this);
use this
AlertDialog.Builder builder = new AlertDialog.Builder(YourActivity.this); // for activity
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); // for fragment
I have tested showMessage method. and it worked without no problem. I guess you must check your database connection ,cursor and buffer. However This code is for showing an alert message:
/**
* #param context
* #param title
* #param message
*
* Caution:Error--> android.view.WindowManager$BadTokenException:
* Unable to add window -- token null is not for an application
*
* --> Instead of getApplicationContext(), just use
* ActivityName.this
*/
public static AlertDialog showDialog(final Context context, String title,
String message) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setCancelable(true);
builder.setMessage(message);
builder.setTitle(title);
builder.setPositiveButton(R.string.ok,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
return builder.show();
}
public static void showAlert(final Activity activity, String title,
String message, final boolean finish) {
new AlertDialog.Builder(activity).setTitle(title).setMessage(message)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
if (finish) {
activity.finish();
}
}
}).show();
}
I am making a file protection application that asks users to enter a password, this password is stored and next time to view the file they have to enter the same password.
I am able to ask users to set password, and protect the file, and I am able to ask users to enter password for viewing. the issue I have is with the input field.
The code for setting password is:
public static void protect(final File file, final FileListActivity mContext, final OperationCallback<Void> callback)
{
LinearLayout layout = new LinearLayout(mContext);
layout.setOrientation(LinearLayout.VERTICAL);
final EditText input = new EditText(mContext);
input.setHint(mContext.getString(R.string.enter_new_name));
input.setSingleLine();
layout.addView(input);
final EditText confirm = new EditText(mContext);
confirm.setHint(mContext.getString(R.string.confirm_new_name));
confirm.setSingleLine();
layout.addView(confirm);
new Builder(mContext)
.setTitle(mContext.getString(R.string.rename_dialog_title, file.getName()))
.setView(layout)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String newName = confirm.getText().toString();
String password=input.getText().toString();
try
{
//File parentFolder = file.getParentFile();
if (newName.equals(input.getText().toString())){
pass=newName;
pe=true;
filename=file.getName();
Toast.makeText(mContext,"password match", Toast.LENGTH_LONG).show();
}
else if (!newName.equals(password)) {
pe=false;
Toast.makeText(mContext,"password doesnot match", Toast.LENGTH_LONG).show();
}
else
{
if(callback!=null)
{
callback.onFailure(new Exception());
}
if(mContext!=null)
new Builder(mContext)
.setTitle(mContext.getString(R.string.error))
.setMessage(mContext.getString(R.string.rename_failed, file.getName()))
.show();
}
}
catch (Exception e) {
if(callback!=null)
{
callback.onFailure(e);
}
Log.e(TAG, "Error occured while renaming path", e);
if(mContext!=null)
new Builder(mContext)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle(mContext.getString(R.string.error))
.setMessage(mContext.getString(R.string.rename_failed, file.getName()))
.show();
}
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
})
.show();
}
The code for asking users for the password to view the file is:
private void checkpass(final File file,FileListActivity mContext){
if(FileActionsHelper.pe==true && file.getName().equals(FileActionsHelper.filename)){
final EditText input = new EditText(FileListActivity.this);
input.setHint(mContext.getString(R.string.password_add));
input.setSingleLine();
new Builder( mContext)
.setTitle(mContext.getString(R.string.rename_dialog_title, file.getName()))
.setView(input)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
try
{
if ((FileActionsHelper.pass).equals("1")){
openFile(file);
Toast.makeText( FileListActivity.this,"password matched", Toast.LENGTH_LONG).show();
}
else if (!(FileActionsHelper.pass).equals("1")) {
Toast.makeText( FileListActivity.this,"you entered wrong password", Toast.LENGTH_LONG).show();
}
else if (FileActionsHelper.pe!=true){
openFile(file);
}
}
catch (Exception e) {
Log.e(TAG, "Error occured while renaming path", e);
}
}
}).setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
})
.show();
}
}
the code for opening of file is:
void openFile(File file) {
//if(FileActionsHelper.pe==true && file.getName().equals(FileActionsHelper.filename)){
checkpass(file,FileListActivity.this);
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(file);
String type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
MimeTypeMap.getFileExtensionFromUrl(uri.toString()));
intent.setDataAndType(uri, type == null ? "*/*" : type);
startActivity((Intent.createChooser(intent,getString(R.string.open_using))));}
//else {
// }
//}
The error i am getting is in the function check pass, at input.getText.toString(); it says getText cannot be resolved into a type or field.
How can I fix this ?
I want when user click on Uninstall Button,there prompt a password dialog. This Dialog is coming only one time.I'm using this code:
public void run() {
Looper.prepare();
while (!exit) {
// get the info from the currently running task
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(MAX_PRIORITY);
String activityName = taskInfo.get(0).topActivity.getClassName();
Log.d("topActivity", "CURRENT Activity ::" + activityName);
if (activityName.equals("com.android.packageinstaller.UninstallerActivity")) {
//Toast.makeText(context, "Uninstall Clicked", Toast.LENGTH_LONG).show();
Intent startIntent = new Intent(this.context, Alert_Dialog.class);
startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.context.startActivity(startIntent);
exit = true;
} else if (activityName.equals("com.android.settings.ManageApplications")) {
Toast.makeText(this.context, "Back", Toast.LENGTH_LONG).show();
exit = true;
}
}
Looper.loop();
}//Run
I want whenever user click on Unistall Prompt should come , below is the code in onClick,
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); alertDialogBuilder.setView(promptsView);
final EditText userInput = (EditText) promptsView.findViewById(R.id.edit_text);
alertDialogBuilder
.setCancelable(false)
.setNegativeButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
/** DO THE METHOD HERE WHEN PROCEED IS CLICKED*/
String user_text = (userInput.getText()).toString();
/** CHECK FOR USER'S INPUT **/
if (user_text.equals("abc"))
{
Log.d(user_text, "HELLO THIS IS THE MESSAGE CAUGHT :)");
Toast.makeText(myContext,"PAssword Correct",Toast.LENGTH_LONG).show();
Alert_Dialog.this.finish();
//Search_Tips(user_text);
}
else{
Log.d(user_text,"string is empty");
String message = "The password you have entered is incorrect." + " \n \n" + "Please try again!";
AlertDialog.Builder builder = new AlertDialog.Builder(myContext);
builder.setTitle("Error");
builder.setMessage(message);
builder.setPositiveButton("Cancel", null);
builder.create().show();
Alert_Dialog.this.finish();
}
}
});
// .setPositiveButton("Cancel",
// new DialogInterface.OnClickListener() {
// public void onClick(DialogInterface dialog,int id) {
// Alert_Dialog.this.finish();
//
// }
//
// }
// );
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
Once the dialog calls dismiss(), the view you set for the dialog also would be destroyed.
In your case, this line set the view,
alertDialogBuilder.setView(promptsView);
But when the dialog is closed, the view promptsView is destroyed,
The promptsView should be re-created once again you use it.
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
promptsView = new PromptsView();//new or inflate the view
//....
alertDialogBuilder.setView(promptsView);
How come the AlertDialog that has the title "Location was saved to file" doesn't show up? It is the one that should be displayed after the user presses Okay on the first dialog.
I think it has something to do with threads, but I'm not sure.
SimpleDateFormat timeStampFormat = new SimpleDateFormat("MMMMM-dd-yyyy");
final EditText input = new EditText(EncounterActivity.this);
input.setWidth(75);
input.setText("Bear-Encounter-GPS-" + timeStampFormat.format(new Date()) + ".txt");
new AlertDialog.Builder(EncounterActivity.this)
.setTitle("Save GPS Location")
.setMessage("Please enter a filename")
.setView(input)
.setIcon(R.drawable.gps)
.setPositiveButton("Save", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
try {
File root = Environment.getExternalStorageDirectory();
if (root.canWrite()){
File fn = new File(root, input.getText().toString());
FileWriter gpxwriter = new FileWriter(fn);
BufferedWriter out = new BufferedWriter(gpxwriter);
out.write(ll.toUTMRef().toString());
out.close();
AlertDialog.Builder builder = new AlertDialog.Builder(EncounterActivity.this);
builder.setIcon(R.drawable.gps);
builder.setTitle("Location was saved to file");
builder.setMessage("Your GPS coordinates were saved to " + fn.getAbsolutePath())
.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
AlertDialog alert = builder.create();
alert.show();
}
} catch (IOException e) {
//ProfitBandit.alert(Shipment.this, "Couldn't write the file.");
Log.v("IOException", "Could not write file " + e.getMessage());
}
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Do nothing.
}
}).show();
You CAN display an alert dialog from an alert dialog if you use the pattern showDialog(int) getInstanceSomeDialog and onCreateDialog(int) for both dialogs. So in my aboutAlertDialog I have:
builder.setPositiveButton("View EULA", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) { //cancels itself?
showDialog(DIALOG_EULA_SHOW);
}
});
which in turn displays an EULA in yet another AlertDialog. OR you could just toss up a Toast as in:
Toast.makeText(Main.this,"Location Saved.", Toast.LENGTH_SHORT).show();
where Main is the activity class.