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

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

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.

Android Studio cant process questionmark in String when using equals Method?

Currently I try to learn about creating an android app and work through a Tutorial on it using android studio.
So far, I've created two activites.
The Main activity features a Plain Text Field and a Button. The other activity displays the input of the Plain Text Field when pressing the Button.
The Tutorial ends here.
For fun purpose, I want to change it to the second activity displaying certain text on one special input. Now the line to determine if input is equal to certain String looks like this:
String result = message.equals("foo?") ? "bar" : "wrong";
As long as I leave the Questionmark at the end of "foo?", it sets result to "wrong", although I intend it to be bar.
(With message being the content of the Plain Text Field from the Main Activity)
MCVE Attempt
Main Activity
public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE ="";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/** Called when the user taps the Go Button */
public void sendMessage(View view)
{
Intent intent = new Intent(this,DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.editText);
String message = editText.getText().toString(); //User puts in "foo?"
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}}
Second Activity (Showing the result)
public class DisplayMessageActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = findViewById(R.id.textView3);
String result = message.equals("foo?") ? "Bar" : "wrong"; //result is set to "wrong"
textView.setText(result);
//But "Bar" expected
}}
Changing userinput to foo instead of foo? and removing the questionmark in .equals("foo?") (to .equals("foo")) produces correct output. Hope thats enough

setVolume Using MediaPlayer API is Not Working

I don't want to use AudioManager... Using the MediaPlayer API, my app
is not able to set the volume to desired level.
As it playes on previous level of volume which is set by Volume UP and Volume Down Key.
public class MainActivity extends AppCompatActivity {
protected static MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button next=(Button) findViewById(R.id.button);
mp=MediaPlayer.create(this,R.raw.m1);
mp.setVolume(0.02f,0.02f);
mp.start();
mp.setLooping(true);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
}
});
As mentioned in documentation:
"This API is recommended for balancing the output of audio streams within an application. Unless you are writing an application to control user settings, this API should be used in preference to setStreamVolume(int, int, int) which sets the volume of ALL streams of a particular type. "
https://developer.android.com/reference/android/media/MediaPlayer.html#setVolume(float,%20float)
You can however set a stream to your mediaplayer and let is play on
the desired stream like Music / Notification or Alarm which should
suffice the req.
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
Otherwise if you want to play some sound with certain level you have to use AudioManager API's to set a certain stream & set the volume of the stream and play the audio. This is a common practice.

How to go another Activity using function instead of Button widget

I hope if someone help me in this ,
in my projet I am trying to open another avtivity used voice command Ex, say "one " it should compare if the string is equal to what I? have it should switch to another activity or external activity such as Phone call, Camera. What I did here, I store the recognized ward in Edittext and store it in string and compare it . my 2 projects counted on how to do this
Inside onCreate:
final Button btntx = (Button)findViewById(R.id.btn1);
final EditText edittxt1 = (EditText)findViewById(R.id.edttxt);
btntx.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent (Intent.ACTION_DIAL); // sow the Dial window
intent.setData(Uri.parse("tel:"));
startActivity(intent);
);
}
////////////////////////// outside the onCreate //////// Function after I get the result from the Speech recognizer and call this function to see if the what it said is equal to my keyword . SO i need to call the button to open that activity
String gwdata= edtxt.getText().toString(); // data from EditText
if (gwdata.equals("One"))
{
Toast.makeText(getBaseContext(),"They are equal", Toast.LENGTH_SHORT).show();
// Here Do someting to go to another activity
}
Thnks guys in advance .

How to get last outgoing call in Android

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);
}

Categories

Resources