Do a command when State Call is ringing - java

I have a Little app to make in Android.
So I have few Button and each button call a specific intermediate number first.
After this first call I have to wait ~15seconds and the app have to call the true number.
for example, I click on a button named "toto" and is telephone number is 001 so the app first make a call to the intermediate number which is for example 000 and there will be a voice saying that he has to wait few seconds before reach toto.
For now I have this :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TelephonyManager telephoneM = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
PhoneStateListener listner = new PhoneStateListener() {
public void onCallStateChanged(int state, String incomingnumber) {
String etat = "N/A";
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
etat = "pas de reponse";
Toast.makeText(MainActivity.this, "" + etat,
Toast.LENGTH_SHORT).show();
break;
case TelephonyManager.CALL_STATE_RINGING:
etat = "sonne";
Toast.makeText(MainActivity.this, "" + etat,
Toast.LENGTH_SHORT).show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
etat = "en ligne";
Toast.makeText(MainActivity.this, "" + etat,
Toast.LENGTH_SHORT).show();
break;
}
}
};
telephoneM.listen(listner, PhoneStateListener.LISTEN_CALL_STATE);
Btoto = (Button) findViewById(R.id.num1);
Btoto.setOnClickListener(this);
public void onClick(View v) {
switch (v.getId()) {
case R.id.num1:
Intent localIntent = new Intent("android.intent.action.CALL");
localIntent.setData(Uri.parse("tel:0153204255")); // the intermediate number
startActivity(localIntent);
//here I have to make a delay after the first ringing
// And here i have to make the real call to toto
break;
Can someone give me some advice please ?
Thank you

Related

How to show sum of all amount in textview and than to submit button?

I am new to android and i want to know that when i press lets say Rs 10 or 20 etc than there total plus value should be shown in the textview amount and when submit button is pressed than the total value is saved in dabase and if clear button is pressed then the last entered value should be clear.How would i do this can anybody help in the form of code.? image is also attached.....
public class Sale extends AppCompatActivity implements View.OnClickListener {
Button btn10, btn20, btn50, btn100, btn500, btn1000, btn5000,btnsubmit_sale,btnclearamount;
TextView textViewamount;
int sum=0;
int total_sum;
Intent intent;
String email;
public static final String Firebase_Server_URL = "https://accountbook-a5340.firebaseio.com/";
Firebase firebase;
DatabaseReference databaseReference;
public static final String Database_Path = "Sales_Details_DataBase";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sale);
btn10 = findViewById(R.id.button10);
btn20 = findViewById(R.id.button20);
btn50 = findViewById(R.id.button50);
btn100 = findViewById(R.id.button100);
btn500 = findViewById(R.id.button500);
btn1000 = findViewById(R.id.button1000);
btn5000 = findViewById(R.id.button5000);
btnclearamount = findViewById(R.id.btnclear_sale);
textViewamount = findViewById(R.id.txtviwamount);
btnsubmit_sale=findViewById(R.id.btnsubmitSale);
btn10.setOnClickListener(this);
btn20.setOnClickListener(this);
btn50.setOnClickListener(this);
btn100.setOnClickListener(this);
btn500.setOnClickListener(this);
btn1000.setOnClickListener(this);
btn5000.setOnClickListener(this);
textViewamount.setText(getText(sum));
intent=getIntent();
email=intent.getStringExtra("email1");
Log.d("email1",""+email);
firebase.setAndroidContext(Sale.this);
firebase = new Firebase(Firebase_Server_URL);
databaseReference = FirebaseDatabase.getInstance().getReference(Database_Path);
// databaseReference.keepSynced(true);
final SaleDetailSG saledetails = new SaleDetailSG();
final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
// int clicked_number = Integer.parseInt(btn10.getText().toString());
btnsubmit_sale.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (sum==0){
Toast.makeText(Sale.this, "Select any sale value to save into DataBase", Toast.LENGTH_SHORT).show();
}else {
Log.d("emailsale", "" + email+sum);
saledetails.setSale(sum);
saledetails.setEmail(email);
Date c = Calendar.getInstance().getTime();
SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy--hh:mm:ss a");
df.setTimeZone(TimeZone.getTimeZone("GMT+05"));
String formattedDate = df.format(c);
// Adding the both detail and amount values using Expense details class object using ID.
databaseReference.child(formattedDate).setValue(saledetails);
databaseReference.keepSynced(true);
// Showing Toast message after successfully data submit.
Toast.makeText(Sale.this, "Data Inserted Successfully into Database", Toast.LENGTH_LONG).show();
sum = 0;
}
}
});
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button10:
sum= sum+Integer.parseInt(btn10.getText().toString());
Toast.makeText(this, "Rs 10 Entered", Toast.LENGTH_SHORT).show();
break;
case R.id.button20:
sum=sum+Integer.parseInt(btn20.getText().toString());
Toast.makeText(this, "Rs 20 Entered", Toast.LENGTH_SHORT).show();
break;
case R.id.button50:
sum= sum+Integer.parseInt(btn50.getText().toString());
Toast.makeText(this, "Rs 50 Entered", Toast.LENGTH_SHORT).show();
break;
case R.id.button100:
sum= sum+Integer.parseInt(btn100.getText().toString());
Toast.makeText(this, "Rs 100 Entered", Toast.LENGTH_SHORT).show();
break;
case R.id.button500:
sum= sum+Integer.parseInt(btn500.getText().toString());
Toast.makeText(this, "Rs 500 Entered", Toast.LENGTH_SHORT).show();
break;
case R.id.button1000:
sum= sum+Integer.parseInt(btn1000.getText().toString());
Toast.makeText(this, "Rs 1000 Entered", Toast.LENGTH_SHORT).show();
break;
case R.id.button5000:
sum= sum+Integer.parseInt(btn5000.getText().toString());
Toast.makeText(this, "Rs 5000 Entered", Toast.LENGTH_SHORT).show();
break;
}
}
it looks like you are not setting the sum value to your textView on each button click
also to deduct the last value you have to add them in a List<Integer>
try this
declare the List like this
List<Integer> valueList=new ArrayList<>();
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button10:
sum= sum+Integer.parseInt(btn10.getText().toString());
Toast.makeText(this, "Rs 10 Entered", Toast.LENGTH_SHORT).show();
textViewamount.setText(Integer.toString(sum) //<-- change
//add value to list
valueList.add(Integer.parseInt(btn10.getText().toString());
break;
case R.id.button20:
sum=sum+Integer.parseInt(btn20.getText().toString());
Toast.makeText(this, "Rs 20 Entered", Toast.LENGTH_SHORT).show();
textViewamount.setText(Integer.toString(sum)
//add value to list
valueList.add(Integer.parseInt(btn20.getText().toString());
break;
case R.id.button50:
sum= sum+Integer.parseInt(btn50.getText().toString());
Toast.makeText(this, "Rs 50 Entered", Toast.LENGTH_SHORT).show();
textViewamount.setText(Integer.toString(sum)
//add value to list
valueList.add(Integer.parseInt(btn50.getText().toString());
break;
case R.id.button100:
sum= sum+Integer.parseInt(btn100.getText().toString());
Toast.makeText(this, "Rs 100 Entered", Toast.LENGTH_SHORT).show();
textViewamount.setText(Integer.toString(sum)
//add value to list
valueList.add(Integer.parseInt(btn100.getText().toString());
break;
case R.id.button500:
sum= sum+Integer.parseInt(btn500.getText().toString());
Toast.makeText(this, "Rs 500 Entered", Toast.LENGTH_SHORT).show();
textViewamount.setText(Integer.toString(sum)
//add value to list
valueList.add(Integer.parseInt(btn500.getText().toString());
break;
case R.id.button1000:
sum= sum+Integer.parseInt(btn1000.getText().toString());
Toast.makeText(this, "Rs 1000 Entered", Toast.LENGTH_SHORT).show();
textViewamount.setText(Integer.toString(sum)
//add value to list
valueList.add(Integer.parseInt(btn1000.getText().toString());
break;
case R.id.button5000:
sum= sum+Integer.parseInt(btn5000.getText().toString());
Toast.makeText(this, "Rs 5000 Entered", Toast.LENGTH_SHORT).show();
textViewamount.setText(Integer.toString(sum)
//add value to list
valueList.add(Integer.parseInt(btn5000.getText().toString());
break;
}
then on clear button
take the value from valueList in reverse order like this
if(!valueList.isEmpty()){
int lastValue=valueList.get(valueList.size()-1);
sum=sum-lastValue;
valueList.remove(valueList.size()-1);
textViewamount.setText(Integer.toString(sum));
}
to clear the textViewamount on submit
add below line in end of submit button code
textViewamount.setText("");

How count missed call or reject call in android studio

Here I make a call from my app and I want to count missed or reject call
and I use a counter (counterNoiseCalling) to count missed or reject call but in node in Firebase counter still always equal 0 and I don't know why.
private class MyPhoneListener extends PhoneStateListener {
private boolean onCall = false;
String userNameCalling = UserDetails.username;
int counterNoisingCall = 0;
#Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
// phone ringing...
Toast.makeText(Call.this, incomingNumber + " calls you", Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
// one call exists that is dialing, active, or on hold
Toast.makeText(Call.this, "on call...", Toast.LENGTH_LONG).show();
if(!onCall) {
//because user answer the incoming call
Toast.makeText(Call.this, "The call is being answered", Toast.LENGTH_SHORT).show();
}else {
// reject call or missed call
Toast.makeText(Call.this, "Number Busy Or No Reply", Toast.LENGTH_SHORT).show();
counterNoisingCall++;
}
break;
case TelephonyManager.CALL_STATE_IDLE:
// in initialization of the class and at the end of phone call
// detect flag from CALL_STATE_OFFHOOK
if (onCall == true) {
Toast.makeText(Call.this, "restart app after call to Users List", Toast.LENGTH_LONG).show();
// restart our application to return to Our Contact
Intent restart = new Intent(Call.this , Users.class);
restart.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(restart);
onCall = false;
}
break;
default:
break;
}
Firebase childRef = mRootRef.child(userNameCalling);
childRef.setValue(counterNoisingCall);
}
}
If you can debug and see that counterNoisingCall variable is not 0 then the problem seem to be related to Firebase.
I guess that may be is related to security rules. Maybe the user does not have permission write on the Firebase node.
I would put a complete listener on setValue to see that the command completed with success.
Try this.
Firebase childRef = mRootRef.child(userNameCalling);
childRef.setValue(counterNoisingCall).addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if (!task.isSuccessful()) {
Log.d("",task.getException().getMessage());
}
}
});

How to define broadcast receiver class and phonestatechange class in mainactivity?

this is my phonestate class where im checking phone state change
public class CustomPhoneStateListener extends PhoneStateListener {
private static final String TAG = "CustomPhoneStateListener";
public void onCallStateChange(int state, String incomingNumber){
Log.v(TAG, "WE ARE INSIDE!!!!!!!!!!!");
Log.v(TAG, incomingNumber);
switch(state){
case TelephonyManager.CALL_STATE_RINGING:
Log.d(TAG, "RINGING");
break;
} }
this is my broadcast receiver class where connecting with broadcast
public class CustomBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "CustomBroadcastReceiver";
#Override
public void onReceive(Context context, Intent intent) {
Log.v(TAG, "WE ARE INSIDE!!!!!!!!!!!");
TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
CustomPhoneStateListener customPhoneListener = new CustomPhoneStateListener();
telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
Bundle bundle = intent.getExtras();
String phoneNr= bundle.getString("incoming_number");
Log.v(TAG, "phoneNr: "+phoneNr);
}
how do define this in main activity?
Include this in your android manifest
<uses-permission android name="android.permission.READ_PHONE_STATE" />
If you want to listen phone state in your activity than use this instead of BroadcastReceiver class. Include This in your activity :
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
PhoneStateListener listener = new PhoneStateListener() {
#Override
public void onCallStateChanged(int state, String incomingNumber) {
String stateString = "N/A";
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
stateString = "Idle";
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
stateString = "Off Hook";
break;
case TelephonyManager.CALL_STATE_RINGING:
stateString = "Ringing";
break;
}
}
};
// Register the listener with the telephony manager
telephonyManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
See this.
Just put it in your Activity, like:
private CustomBroadcastReceiver receiver = new CustomBroadcastReceiver();
Then add to OnCreate()
registerReceiver(receiver, new IntentFilter(YPUR_ACTION));
But there is no sense in putting TelephonyManager in BroadcastReceiver. You can just add it to your activity like in the answer above.

Sending sms on multiple emulator at time

I'm developing an Android app where i need to send single message to
receive multiple emulator at a time .But the problem is only one
emulator is receiving the message.Here is my code.
public class SMS extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnSendSMS = (Button) findViewById(R.id.btn_SendSms);
txtPhoneNo = (EditText) findViewById(R.id.edittext_PhoneNumber);
txtMessage = (EditText) findViewById(R.id.edittext_MessageBody);
btnSendSMS.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
String message = txtMessage.getText().toString();
String phoneNo = txtPhoneNo.getText().toString();
StringTokenizer st=new StringTokenizer(phoneNo,",");
while (st.hasMoreElements())
{
String tempMobileNumber = (String)st.nextElement();
if(tempMobileNumber.length()>0 && message.trim().length()>0)
{
sendSMS(tempMobileNumber, message);
}
else
{
Toast.makeText(getBaseContext(),
"Please enter both phone number and message.",
Toast.LENGTH_SHORT).show();
}
}
}
});
}
private void sendSMS(String phoneNumber, String message)
{
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0);
//---when the SMS has been sent---
registerReceiver(new BroadcastReceiver()
{
#Override
public void onReceive(Context arg0, Intent arg1)
{
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS sent",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic failure",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No service",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio off",
Toast.LENGTH_SHORT).show();
break;
}
}
} , new IntentFilter(SENT));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}
}
To send an SMS message to another emulator instance which running on same machine, launch the SMS application. Specify the console port number(ex:5555) of the target emulator instance as as the SMS address.
Please note that this answer is based on :Linuxtopia guide.
You can create the array of emulator id or number then put sendTextMessage into that execute the loop as many element into the the array. OR You can have UI that allow the user to insert the phone number or emulator number in to the list and same procedure as above !!!
note that sendTextMessage `s first argument is "Phone Number" to whom you want to send sms
simply replace it with your requirement every as loop iterate

Toast showing up twice

I have a program that sends pre-defined text messages to a group of people at a push of a button. I have it working well but the problem I have is that when it sends the messages, it pops up with 2 toasts per message sent. Code:
package com.mfd.alerter;
//imports
public class homeScreen extends Activity {
//buttons
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//vars
// Grab the time
final Date anotherCurDate = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("km");
final String formattedTime = formatter.format(anotherCurDate);
// Contacts
final String[] numbers = getResources().getStringArray(R.array.numbers);
// Start messages. Only 1 is given to shorten post
callStructureFire.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String msgText = "MFD PAGE OUT:\nStructure Fire\nTimeout:"+formattedTime;
for (int i = 0; i < numbers.length; i++) {
sendSMS(numbers[i], msgText);
}
}
});
//more call types. not important.
}
//---sends a SMS message to another device---
private void sendSMS(String numbers, String message)
{
String SENT = "SMS_SENT";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
new Intent(SENT), 0);
//---when the SMS has been sent---
registerReceiver(new BroadcastReceiver(){
#Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS sent",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic failure",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No service",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio off",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(numbers, null, message, sentPI, null);
}
//action bar stuff. not important.
}
More in detail: Lets say I send the text to 3 people, 6 toast messages will pop up saying "SMS Sent". How do I make it so only 3 will show up?
Also, Is there a way to maybe add a counter of the messages sent? Ex: "Message 1/10 sent", "Message 2/10 sent", etc?
I didn't really look at your code or asked myself why this happens but here's a trick to stop toasts show up twice:
Create a Toast instance using makeToast(), before showing it you call cancel(), set your text and then call show(). This will dismiss the previous toast. You won't even notice that a toast is displayed twice.
That's a stupid workaround, but it works for me ;-)
Aren't you supposed to register the receiver only once and not every time you call sendSMS.
You get 6 Toasts with three sms messages because you have 3 BroadCastReceivers. So in the first run you get 1 Toast. In the second run you get 2 Toasts (the receiver that was registered in the first run is called, and the one in the second). In the third run all three receivers are called, so you get three more toasts. All in sum - 6 Toasts...
so I guess, you have to register only one receiver before the for loop where you call sendSMS, or if you want the registration in sendSMS, then you have to unregister at the end of the method.
I hope this helps,
cheers!

Categories