How to get last outgoing call in Android - java

I'm working with an app that uses Android Intent class to make calls.
I can successfully create a call to a number so that is working.
What I want now is to show the last outgoing call number to show in a TextView.
Also I made that TextView to be clickable so by a click I can redial the number.
I'm using CallLog.Calls.getLastOutgoingCall(getApplicationContext()); to get the last called number.
This works only once in my application.
I start the application, enter a number and it makes a call. The first outgoing called number I set in the TextView. After that I enter a second number which is successfully set in the TextView but when I click to redial the app calls the first number!
Why is that?
My last outgoing number is the second.
Why is it calling the first number?
Also if I restart the app then it redials the second outgoing number.
Here is my code:
public class MainActivity extends ActionBarActivity {
Button btnCall;
TextView number;
EditText calledNumber;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnCall = (Button) findViewById(R.id.button);
number = (TextView) findViewById(R.id.textView2);
calledNumber = (EditText) findViewById(R.id.editText);
//Gets the last outgoing number from the call log
final String lastCalledNumber = CallLog.Calls.getLastOutgoingCall(getApplicationContext());
btnCall.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String TheNumber = calledNumber.getText().toString();
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + TheNumber));
startActivity(callIntent);
number.setText(TheNumber);
}
});
//redial number in TextView by click
number.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent call = new Intent(Intent.ACTION_CALL);
call.setData(Uri.parse("tel:" + lastCalledNumber));
startActivity(call);
}
});
}
}

Per the activity lifecycle, onCreate() is only called once and does not run each time your app appears - this would mean that your getLastOutgoingCall() would be correct the first time but wouldn't necessarily work the second time.
If you'd like to run something every time the activity appears, you should move it to onResume() - this ensures it will always be up to date:
String lastCalledNumber;
#Override
protected void onCreate(Bundle savedInstanceState) {
// Same except without the `getLastOutgoingCall()`
}
#Override
public void onResume() {
lastCalledNumber = CallLog.Calls.getLastOutgoingCall(this);
}

Related

OnClick open Activity1 but send EXTRA_TEXT to Activity10...Is a String necessary or can I simply use intent? Can post XML if helpful

Multiple Activity App...Activity1 (SiteData) collects data and sends to Activity10 (DataReview)...but before going straight to Activity10 user must go through Activities2-9 (SanDiegoArea, etc...) collecting data and also passing EXTRA_TEXT to the other activity until Activity10...
This way:
(Activity1 -> Activity2 -> ... -> Activity10)
Activity10 will be able to review Activity1-9 EXTRA_TEXT for verification...Code is as follows but I'm getting no EXTRA_TEXT at Activity10 to display in it's TextViews for review? How can I solve the problem?
Activity1 - SITEDATA - data info input
public class SiteData extends AppCompatActivity {
public static final String EXTRA_TEXT = "MultipleActivities.EXTRA_TEXT";
public static final String EXTRA_NUMBER = "MultipleActivities.EXTRA_NUMBER";
public void clickFunction(View view) {
Intent intent = new Intent(getApplicationContext(), SanDiegoArea.class);
startActivity(intent);
Log.i("Info", "Login Details Entered");
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sitedata);
//Possible coding needed for Autocomplete...see more
Button button = (Button) findViewById(R.id.buttonSend);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openSanDiegoArea();
}
});
}
public void openSanDiegoArea() {
EditText editText1 = (EditText) findViewById(R.id.editTextName);
String text = editText1.getText().toString();
EditText editText2 = (EditText) findViewById(R.id.editTextPhone);
int number = Integer.parseInt(editText2.getText().toString());
Intent intent = new Intent(this, DataReview.class);
intent.putExtra(EXTRA_TEXT, text);
intent.putExtra(EXTRA_NUMBER, number);
startActivity(intent);
}
}
ACTIVITY10 - DATAREVIEW Section getting info
public class DataReview extends AppCompatActivity {
public void clickFunction(View view) {
Intent intent = new Intent(getApplicationContext(), ThankYou.class);
startActivity(intent);
Log.i("Info", "Sample Data Sent to BWTF");
}
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.datareview);
Intent intent = getIntent();
String text = intent.getStringExtra(SiteData.EXTRA_TEXT);
int number = intent.getIntExtra(SiteData.EXTRA_NUMBER, 0);
TextView textView1 = (TextView) findViewById(R.id.editTextName);
TextView textView2 = (TextView) findViewById(R.id.editTextPhone);
TextView textView3 = (TextView) findViewById(R.id.editTextEmail);
TextView textView4 = (TextView) findViewById(R.id.editTextDate);
TextView textView5 = (TextView) findViewById(R.id.editTextTime);
textView1.setText(text);
textView2.setText("" + number);
}
};
If Activity1 calls Activity2, it will have to pass data to it the way you are doing. ( I'm guessing here that your Activity2 is DataReview.class)
But Activity2 will call Activity3 and, in order for it not to loose the data received from activity 2 you will have to send it too. In other words, Activity1 sends Activity1's data to Activity2, Activity2 sends Activity1's and Activity2's data to Activity3 and so on.
In this way, Activity9 will receive (1, 2, 3, 4, 5, 6, 7, 8) data from it's caller Activity8 and will have to pass all this accumulated data to Activity10.
Considering that Activity3 is called by Activity2 it can't see the data sent from Activity1 because it don't know it unless Activity2 pass it too. Am I been clear?
If you are just learning Android it is ok to do it this way in order to learn but it is not the better way to solve this problem. I would use a same ViewModel on all activities in order to represent this data in order to always have just one place for holding it as it would make my code more clear. In reality I wouldn't even use all those Activities, I would use only one activity and work with 10 fragments, one for each page (it is just to provide navigation between then). So those are Ideas for you to improve your app if you feel It would be good.

An app that automatically sends messages and makes calls to emergency contact

I am totally new to Android, and Java. I am designing an app that is constantly getting some input data(vital signals) and once abnormal reading is observed, ie heart rate < 50, the app will automatically send messages and make calls to one (or many) emergency contact(s). The tutorials I have found so far mentioning "auto send/call" are all about sending/calling within our own app using Intent, but the user still has to press the button to continue. Is there any way to do that?
The one I have now:
public class MainActivity extends AppCompatActivity {
private EditText phone;
private ImageButton call;
#RequiresApi(api = Build.VERSION_CODES.M)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
phone = findViewById(R.id.number);
call = findViewById(R.id.call);
call.setOnClickListener(new View.OnClickListener(){
//set number
public void onClick(View v){
String phoneNumber = phone.getText().toString();
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:"+phoneNumber));
startActivity(intent);
}
});
}
}
I have tried removing the listener so that it no longer waits for me to click the button to call:
phone = findViewById(R.id.number);
//Removed listener
String phoneNumber = phone.getText().toString();
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:"+phoneNumber));
startActivity(intent);
But it turns out that once I launch the app, the app makes call directly even without typing in the number and of course call fail.
I think you need to use a Trigger Event. You can check this site for more information about it.
https://developer.android.com/reference/android/hardware/TriggerEvent

SavedIntanceState.getBoolean() is making my app crash (i think)

My Goal:
So I need help putting a boolean primitives into a bundle and retrieving it from the bundle when there is a screen orientation change in Android. I am using that boolean value in a conditional statement that helps decide if 2 Button views (mTrueButton, mFalseButton) should be enabled or not. What i have so far is causing the app to shut down (aka crash) when there is a screen rotation. I think I am not retrieving or writing my boolean from my bundle or into my bundle correctly, and it is causing the app to crash.
How The App Should Works:
When a user touches the mTrueButton or mFalseButton button to answer a question, both buttons become disabled so the user is not allowed to answer again. I want those buttons to keep being disabled when the user answers and then rotates the screen.**
I know that when a user rotates their Android device, onDestroy() is called because runtime configuration changes take place, causing the app to be relaunched without having knowledge of it's previous state, (unless store my necessary data onto a bundle and pass it onto my onCreate method).
These are SOME global variables in my activity class
private int index = 0;
priavate Button mTrueButton,mFalseButton;
private static final String KEY_INDEX = "index";
private static final String BTTN_ENABLED = "bttnEnabled";
private boolean trueFalseButtonsEnabled = true;
These are SOME statements in my onCreate() method of the same activity class
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate(Bundle) called");
setContentView(R.layout.activity_quiz);
if(savedInstanceState != null) {
index = savedInstanceState.getInt(KEY_INDEX, 0);
changeButtonEnableStatus(savedInstanceState.getBoolean(BTTN_ENABLED,true));
}
mTrueButton = (Button)findViewById(R.id.true_button);
mFalseButton = (Button)findViewById(R.id.false_button);
mTrueButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
checkAnswer(true);
changeButtonEnableStatus(false);
}
});
mFalseButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
checkAnswer(false);
changeButtonEnableStatus(false);
}
});
}
These are SOME methods in the same activity class but not in my onCreate()
private void changeButtonEnableStatus(boolean bool){
trueFalseButtonsEnabled = bool;
mTrueButton.setEnabled(bool);
mFalseButton.setEnabled(bool);
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState){
super.onSaveInstanceState(savedInstanceState);
Log.d(TAG,"onSavedInstanceState() called");
savedInstanceState.putInt(KEY_INDEX,index);
savedInstanceState.putBoolean(BTTN_ENABLED, trueFalseButtonsEnabled);
}
Note that:
index = savedInstanceState.getInt(KEY_INDEX, 0);
works properly. It is setting global variable "index" to equal to the int primitive what was stored in into keywork "KEY_INDEX".
However I don't think: changeButtonEnableStatus(savedInstanceState.getBoolean(BTTN_ENABLED,true)); is working properly. My app seems to crash when I include that statement and run the app, and then rotate the device.

java physics app in android ( maths )

Basically, I'm new to java and I have to make an android application that can do simple physics equations such as I=Q*t.... etc etc, basically I cannot for the life of me get the result to output, has anyone got any idea's why this wont work on the emulator?
I have tried putting intents and all sorts in there. Help please
public class Current extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_current);
// Show the Up button in the action bar.
setupActionBar();
#SuppressWarnings("unused")
Intent intent = getIntent();
}
public void Main(String[]args){
Button calc1 = (Button)findViewById(string.Calculate_Current);
calc1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// IIIIII HATE JAVA
EditText Charge1 = (EditText)findViewById(R.id.number_input_2);
EditText Time1 = (EditText)findViewById(R.id.number_input_3);
TextView Distances_answer = (TextView)findViewById(R.id.Distances_answer);
double charge = Double.parseDouble(Charge1.getText().toString());
double Time = Double.parseDouble(Time1.getText().toString());
#SuppressWarnings("unused")
Intent intent = new Intent();
Distances_answer.setText("" + charge + Time);
}
});
I feel like you are taking input of charge and time from two edittexts and then on click of a button you are updating a textview to place that product of charge and time there.Try this out
public class Current extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_name);
// Show the Up button in the action bar.
setupActionBar();
Button calc1 = (Button)findViewById(R.id.buttons_id_in_layout);
calc1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText Charge1 = (EditText)findViewById(R.id.number_input_2);
EditText Time1 = (EditText)findViewById(R.id.number_input_3);
TextView Distances_answer = (TextView)findViewById(R.id.distances_answer);
double charge = Double.parseDouble(Charge1.getText().toString());
double time = Double.parseDouble(Time1.getText().toString());
//Time is a class in Java
Distances_answer.setText("" +charge*time);
}
});
}

How to assign a numerical value to a check RadioButton and pass this onto the next activity (similar to a questionnaire app)

I am new to Android development and I am trying to design a simple questionnaire-type app. Each question has set answers within a RadioGroup. I would like to assign a numerical value to the checked RadioButton so I can then send this to the next activity where eventually it will be used to total the scores and produce a message.
I have been able to create a toast; for testing, to show which RadioButton is checked. But I am having trouble with assigning a value to each RadioButton once checked. I know that I will need to use a IF statement but not sure how. Can anyone help or send me a link to a good tutorial?
Here's some sample code within one of my .java file;
public class Diabetes_Question_1 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.diabetes_question_1);
Button btnBack = (Button) findViewById(R.id.btnBack1);
btnBack.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(Diabetes_Question_1.this, Diabetes_Question_2.class));
}
});
final RadioGroup radioDQ1Group = (RadioGroup) findViewById(R.id.radioDQ1Group1);
Button btnNext = (Button) findViewById(R.id.btnNext1);
btnNext.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(Diabetes_Question_1.this, Diabetes_Question_3.class));
int selectedId = radioDQ1Group.getCheckedRadioButtonId();
RadioButton rb1 = (RadioButton) findViewById(selectedId);
Toast.makeText(Diabetes_Question_1.this, rb1.getText(), Toast.LENGTH_SHORT).show();
}
});
}
};
Every view has a tag- an object that can hold whatever data you want. Use it to hold the integer value- you can do that via setTag() and getTag(). Then when you start the new activity, add it to the extras bundle of the intent you launch. The new activity should then read that value from the incoming intent.

Categories

Resources