making phone call from DialogBox in Android - android

I have one DialogBox with 2 buttons. If we click the +ve button, it attempts to open the Dialing Pad; else if we click the -ve button it closes the dialog. When I click the +ve button it shows the null exception. If the same code executes without the DialogBox, it is fine.
Here is my code:
callDialog.setPositiveButton("Call Now", new android.DialogInterface.
OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent dial = new Intent();
dial.setAction("android.intent.action.DIAL");
try {
dial.setData(Uri.parse("tel:9951037343"));
startActivity(dial);
} catch (Exception e) {
Log.e("Calling", "" + e.getMessage());
}
}
}
I've given permissions in the manifest file as <uses-permission android:name="android.permission.CALL_PHONE"/>

Try this..
Intent call = new Intent(Intent.ACTION_DIAL);
call.setData(Uri.parse("tel:phonenumber");
startActivity(call);

Remove the #Overrride line.
Like this:
callDialog.setPositiveButton("Call Now", new android.DialogInterface.
OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent dial = new Intent();
dial.setAction("android.intent.action.DIAL");
try {
dial.setData(Uri.parse("tel:9951037343"));
startActivity(dial);
} catch (Exception e) {
Log.e("Calling", "" + e.getMessage());
}
}
}
----UPDATE-----
Since you havent posted any logcat, its hard to know where it crashes. Try this block:
AlertDialog.Builder callDialog = new AlertDialog.Builder(this);
callDialog.setTitle("My title");
callDialog.setMessage("My message");
callDialog.setPositiveButton("Call", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent dial = new Intent();
dial.setAction("android.intent.action.DIAL");
try {
dial.setData(Uri.parse("tel:9951037343"));
startActivity(dial);
} catch (Exception e) {
Log.e("Calling", "" + e.getMessage());
}
}
});
callDialog.setNegativeButton("Cancel", null);
callDialog.show();

Try the following---
Intent dial = new Intent(Intent.ACTION_DIAL);
//dial.setAction("android.intent.action.DIAL");
try {
dial.setData(Uri.parse("tel:9951037343"));
startActivity(dial);
} catch (Exception e) {
Log.e("Calling", "" + e.getMessage());
}
startActivity(dial);

Use
Intent callIntent = new Intent(Intent.ACTION_CALL);
instead of this
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setAction("android.intent.action.DIAL"); // but this line is an optional one
callIntent.setData(Uri.parse("tel:044 6000 6000"));
startActivity(callIntent);

use this code......
String number = "tel:" + phonenumber;
Intent intent = new Intent(Intent.ACTION_CALL, Uri
.parse(number));
startActivity(intent);
give permission in manifest: <uses-permission android:name="android.permission.CALL_PHONE" />
updated.....
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.menu_icon_pager).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
new AlertDialog.Builder(MainActivity.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Exit")
.setMessage(
"Texting from dialog")
.setNegativeButton(android.R.string.cancel, null)
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
// Exit the activity
String number = "tel:"+phonenumber ;
Intent intent = new Intent(Intent.ACTION_CALL, Uri
.parse(number));
startActivity(intent);
}
})
.show();
}
});
}

Actually I called the AlertDialog from another Activity,so I need the context of called
Activity's Context reference.So I passed the Context as a parameter to Called Activity
Here is Called Activity's code
if(position == 1)new CalledByActivity().dailingDialogBox(param1,
param2,context);//Here I'm passing the context as param
Here CalledBy activity's implemented function
public void dailingDialogBox(final String param1,String param2,final Context context){
Here I implemented in +ve button click
Intent dail = new Intent(Intent.ACTION_DIAL);
dail.setData(Uri.parse("tel:"+station_Number));
context.startActivity(dail);//Here the "context" is reference of called Activity's context

Related

Why OnClickListner is not called on clicking?

This is my code for the BASE class, i am inheriting this class to another child class. And from that class i am calling this function ActiveSMSPackage(). My code is perfect which has not kind of any error but yet the method is not called. Can you guys tell me where i am doing wrong?
public class PrepaidSMSBase extends Activity {
private String smsNumber = "";
private String smsPackageName;
private String smsPrice;
private String smsTitle;
private String smsText;
public PrepaidSMSBase(){}
public void setSmsPackageInformation(String smsTitle, String smsPackageName, String smsPrice, String smsNumber, String smsText)
{
this.smsTitle = smsTitle;
this.smsPackageName = smsPackageName;
this.smsPrice = smsPrice;
this.smsNumber = smsNumber;
this.smsText = smsText;
}
public void activeSMSPackage()
{
try
{AlertDialog.Builder builder = new AlertDialog.Builder(PrepaidSMSBase.this);
builder.setTitle(smsTitle);
builder.setMessage("Are you sure you want to active" + PrepaidSMSBase.this.smsPackageName + "in RS: " +
PrepaidSMSBase.this.smsPrice);
builder.setPositiveButton("Activate", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Uri uri = Uri.parse("smsto:" + smsNumber);
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
intent.putExtra("sms_body", smsText);
startActivity(intent);
Toast.makeText(getApplicationContext(), "Please click send button to activate desire Package", Toast.LENGTH_LONG).show();
}
});
}catch (ActivityNotFoundException e){
e.printStackTrace();
}
}
Code of Child class is:
public class SMSCheckClass extends PrepaidSMSBase implements View.OnClickListener{
Button checkButton;
public SMSCheckClass(){
setSmsPackageInformation("Test 1","Some Thing","50","660","Sub");
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.checksms);
checkButton = (Button)findViewById(R.id.chkBtn);
checkButton.setOnClickListener(this);
}
#Override
public void onClick(View v)
{
activeSMSPackage();
}
Use this code
{AlertDialog.Builder builder = new AlertDialog.Builder(PrepaidSMSBase.this);
builder.setTitle(smsTitle);
builder.setMessage("Are you sure you want to active" + PrepaidSMSBase.this.smsPackageName + "in RS: " +
PrepaidSMSBase.this.smsPrice);
builder.setPositiveButton("Activate", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Uri uri = Uri.parse("smsto:" + smsNumber);
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
intent.putExtra("sms_body", smsText);
startActivity(intent);
Toast.makeText(getApplicationContext(), "Please click send button to activate " + PrepaidSMSBase.this.smsPackageName, Toast.LENGTH_LONG).show();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
you just need to add builder.show() in the end of AlertDialog because your function is written inside of AlertDialog and that show() method is used to show AlertDialog
At first, you should show you dialog in activeSMSPackage, please add this code in that method:
builder.show()

Properly setting up an alertdialog on Activity closing?

I have an activity with two score numbers. I want the user to choose a winner if they're the same when it closes (i.e. player 1 or player2). As a .NET person, I'm trying to basically do the MessageBox.
Unfortunately, from what I've read, android doesn't do UI threads the same way. And that the activity has "a leaked window". I looked on SO and I saw the idea was to put "dismiss" in "onPause". However, I didn't see a "onPause" function and was confused to how to implement it. So I tried what I have below, but got the same error.
What can I do to this guy in order to make sure this works properly?
public void onFinish(View v) {
//get scores
TextView score1tv = (TextView) findViewById(R.id.tvScore1);
score1 = score1tv.getText().toString();
TextView score2tv = (TextView) findViewById(R.id.tvScore2);
score2 = score2tv.getText().toString();
//Make sure they aren't the same
int i_Score1 = Integer.parseInt(score1);
int i_Score2 = Integer.parseInt(score2);
if (i_Score1 == i_Score2){
//alert user
new AlertDialog.Builder(this)
.setTitle("Duplicate score")
.setMessage("Whose score won the bout?")
.setPositiveButton(boutData[1], new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//fencer 1 won
score1 = "V" + score1;
dialog.dismiss();
}
})
.setNegativeButton(boutData[3], new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
score2 = "V" + score2;
dialog.dismiss();
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.create()
.show();
}
Intent intent = new Intent();
intent.putExtra("edittextvalue","value_here");
setResult(RESULT_OK, intent);
finish();
}
Edit: Here's the solution I came to thanks to the help I received:
public void onFinish(View v) {
//get scores
TextView score1tv = (TextView) findViewById(R.id.tvScore1);
score1 = score1tv.getText().toString();
TextView score2tv = (TextView) findViewById(R.id.tvScore2);
score2 = score2tv.getText().toString();
//Make sure they aren't the same
int i_Score1 = Integer.parseInt(score1);
int i_Score2 = Integer.parseInt(score2);
if (score1 == score2){
//alert user
new AlertDialog.Builder(this)
.setTitle("Duplicate score")
.setMessage("Whose score won the bout?")
.setPositiveButton(boutData[1], new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//fencer 1 won
score1 = "V" + score1;
//close out
Intent intent = new Intent();
intent.putExtra("edittextvalue","value_here");
setResult(RESULT_OK, intent);
finish();
}
})
.setNegativeButton(boutData[3], new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
score2 = "V" + score2;
//close out
Intent intent = new Intent();
intent.putExtra("edittextvalue","value_here");
setResult(RESULT_OK, intent);
finish();
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.create()
.show();
}
else{
Intent intent = new Intent();
intent.putExtra("edittextvalue","value_here");
setResult(RESULT_OK, intent);
finish();
}
}
Put the last four lines into an else clause and do something similar in your onClick methods.
Also you shouldn't have to call dialog.dismiss() manually.

how to start an activity inside a custom alertdialog

Here I use this Activatedialog method in onPostExecute inside registration >activity , and need to start a login activity when the alert dialog dissmiss ..however I tried to do this, but I get error.
public void Activatedialog(String jsonmsg, final String name)
{
final AlertDialog.Builder alert = new AlertDialog.Builder(ct);
alert.setTitle("Activate Account");
alert.setMessage(jsonmsg);
// Set an EditText view to get user input
final EditText input = new EditText(ct);
alert.setView(input);
alert.setCancelable(true);
Log.d("MYLOG","before +ve button click");
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
String code = input.getText().toString();
Log.d("MYLOG","before try");
try{
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("u",name ));
params.add(new BasicNameValuePair("code",code ));
JSONObject json = jsonParser.makeHttpRequest(ACTIVATION_URL,"POST", params);
Log.d("MYLOG","after request for json");
stcode = json.getJSONObject("activate").getInt("statuscode");
stmsg = json.getJSONObject("activate").getString("statusmessage");
Log.d("MYLOG","after json parsing : json data is "+stmsg+"stcodeis "+stcode);
if(stcode == 1)
{
dialog.dismiss();
Log.d("MYLOG"," in check json data is "+stmsg+" stcodeis "+stcode);
Intent i = new ("LOGIN");startActivity(i);![i get this error][1]
Toast.makeText(ct, " "+stmsg, Toast.LENGTH_SHORT).show();
}
else if(stcode == 0)
{
Log.d("MYLOG"," in check json data is "+stmsg+" stcodeis "+stcode);
Toast.makeText(ct, " "+stmsg+" "+stcode, Toast.LENGTH_SHORT).show();
}
}
catch(JSONException e)
{
e.printStackTrace();
Toast.makeText(ct, ""+e, Toast.LENGTH_LONG).show();
}
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
// Canceled.
Toast.makeText(ct, "Your Account is not Activated.Login to Activate", Toast.LENGTH_LONG).show();
}
});
alert.show();
}
}
To start an Activity, the intent must have following parameters. A context and target activity. Lets start activity "Login.java" .
Intent i = new Intent(getApplicationContext(), Login.class);
startActivity(i);
you have created intent incorrectly.
1> add the relevant activity you want to open say Loginby adding new activity to the project.
2>create the intent as below
Intent intent = new Intent(getApplicationContext(), Login.class);
3>start activity using the intent created
startActivity(i);
You can do something like this:
AlertDialogManager alert = new AlertDialogManager(StartActivity.this);
AlertDialogManager constructor:
public void AlertDialogManager(final Context context) {
activity = (Activity) context;
}
and you can launch the activity like this:
Intent intent = new Intent(activity , Login.class);
activity.startActivity(intent);

How to execute the negative button function when dialog is canceled through back button?

I am having a dialog which must execute some code on canceled. I set a negative button but its not executing when dialog is canceled from back button.
here is my code.
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Licence expired");
builder.setCancelable(false);
builder.setMessage("Your licence for " + url
+ " has been expired, Please renew it or select another server");
builder.setPositiveButton("Renew now",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Open url in webview
Intent intent = new Intent(MainActivity.this,
WebActivity.class);
intent.putExtra("ServerDomain", url);
startActivity(intent);
/*
* URL domain; try { domain = new URL(url);
* intent.putExtra("ServerDomain", domain.getHost());
* startActivity(intent); } catch (MalformedURLException
* e) { e.printStackTrace(); }
*/
/*
* String url = "http://www.google.com"; Intent i = new
* Intent(Intent.ACTION_VIEW);
* i.setData(Uri.parse(url)); startActivity(i);
*/
}
});
builder.setNegativeButton("Switch Server",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
showServerList();
}
});
builder.create().show();
Create a cancel listner for dialog.
builder.setOnCancelListener(new OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
// TODO Auto-generated method stub
showServerList();
}
});

How to use phone number in alert dialog android?

I want to use default phone number and pass that in alert dialog.How it's possible. I have tried,but it force close.Please solve my issue. Thanks in advance. My coding is as follows:
case R.id.menu_settings:
String phoneNo ="123456789";
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setMessage("Do you want to call us?"+phoneNo);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
String uri = "123456789";
public void onClick(DialogInterface dialog, int whichButton) {
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse(uri));
startActivity(intent);
}
});
alert.setNegativeButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
finish();
}
});
alert.show();
return true;
}
return false;
}
The Uri should be in the format tel:########
String uri = "tel:123456789";
This is happening because at least 1 of 2 reasons.
You do not have the permission <uses-permission android:name="android.permission.CALL_PHONE"/> in your manifest
Your Uri is in an incorrect format. Change it from
String uri = "123456789" to
String uri = "tel:123456789"

Categories

Resources