Search code examples
androidbroadcastreceivercallsmsmanagerphone-state-listener

Whenever I receive an incoming call, my app should cut the incoming call and sends a custom sms to the incoming number


I want to create an app that should do the following whenever I receive an incoming call, my app should cut the incoming call and sends a custom SMS to the incoming number. How can I do this? In this app I wanted to have one UI in which I can toggle the state ON and OFF, one Edit text should be there to give my custom message. My UI:

<i>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/r" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="Welcome to call Handler"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:text="All Calls will Automatically Handled" />

    <ToggleButton
        android:id="@+id/toggleButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2"
        android:layout_centerHorizontal="true"
        android:text="ToggleButton"
        android:textOff="Tap To Turn ON"
        android:textOn="Tap To Turn OFF" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="App BY Rishi Serumadar 2014"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/button3"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/button1" 
        android:layout_toRightOf="@+id/button1"
        android:text="Clear" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button2"
        android:layout_below="@+id/ed1"

        android:text="Save" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/toggleButton1"
        android:text="Enter The text you would like to sent by defult"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView4"

        android:text="View Callers" />

    <EditText
        android:id="@+id/ed1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView3"
        android:ems="10" >

        <requestFocus />
    </EditText>

</RelativeLayout>
</i>

My MainActivity

package com.example.first_app;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends Activity {


    Button savebtn,viewcallerbtn,clearbtn;

    String typedmsg,incomingNumber;

    EditText msgbox;
    ToggleButton toggleButton1;
    TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        final PhoneCallListener phoneListener = new PhoneCallListener();

        TelephonyManager telephonyManager = (TelephonyManager) this
                .getSystemService(Context.TELEPHONY_SERVICE);

        telephonyManager.listen(phoneListener,
                PhoneStateListener.LISTEN_CALL_STATE);

        savebtn = (Button)findViewById(R.id.button1);
        tv = (TextView)findViewById(R.id.textView3);
        clearbtn = (Button)findViewById(R.id.button3);
        viewcallerbtn = (Button)findViewById(R.id.button2);
        toggleButton1 = (ToggleButton) findViewById(R.id.toggleButton1);

        toggleButton1.setChecked(false);

        clearbtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                msgbox.setText("");
            }
        });
        msgbox=(EditText)findViewById(R.id.ed1);

        toggleButton1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                if(!isChecked)
                {

                    msgbox.setVisibility(100);
                    savebtn.setVisibility(100);
                    clearbtn.setVisibility(100);
                    tv.setVisibility(100);
                    AudioManager am;
                    am= (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE);

                    am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);

                    Toast.makeText(getApplicationContext(), "App Disabled an Ringer set to Normal Mode", Toast.LENGTH_LONG).show();

                }
                else 
                {
                    msgbox.setVisibility(0);
                    savebtn.setVisibility(0);
                    clearbtn.setVisibility(0);
                    tv.setVisibility(0);
                    AudioManager am;
                    am= (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE);

                    am.setRingerMode(AudioManager.RINGER_MODE_SILENT);

                    Toast.makeText(getApplicationContext(), "App Enabled an Ringer set to Silent Mode", Toast.LENGTH_LONG).show();
                }
            }
        });

        savebtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                typedmsg = msgbox.getText().toString();

                final String togglestatus ;

            //  togglestatus = toggleButton1.getText().toString();

                Toast.makeText(getApplicationContext(),"typed stored is"+incomingNumber +" this ", Toast.LENGTH_SHORT).show();

            }
        });

        viewcallerbtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                String typedmsg;

                typedmsg = msgbox.getText().toString();

                Intent intob = new Intent(MainActivity.this,Callers.class);

                startActivity(intob);
                Toast.makeText(getApplicationContext(), typedmsg, Toast.LENGTH_SHORT).show();

            }
        });
    }
}

My Second activity

public class PhoneCallListener extends PhoneStateListener {

    private boolean isPhoneCalling = false;

    @Override
    public void onCallStateChanged(int state, String incomingNumber) {

        if (TelephonyManager.CALL_STATE_RINGING == state) {
            // phone ringing
            Log.i("hi", "RINGING 2, number: " + incomingNumber);

            SmsManager smsManager = SmsManager.getDefault();

            smsManager.sendTextMessage(incomingNumber, null, "im busy", null, null);
        }

        if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
            // active
            //Log.i(LOG_TAG, "OFFHOOK");

            isPhoneCalling = true;
        }

        if (TelephonyManager.CALL_STATE_IDLE == state) {
            // run when class initial and phone call ended, need detect flag
            // from CALL_STATE_OFFHOOK
            //Log.i(LOG_TAG, "IDLE number");            
        }
    }
}

My app is working fine so far. but the sms what i receive is what I'm giving in my second activity in the onCallStateChanged method. but i want what ever i typing in the edit text should come as the sms content.

now my question is very simple , in my first activity i have a string variable "Typedmsg" that will get what ever i'm typing in the edit text and it will store in it. here i want this typedmsg string to be my sms content in my second activity that is in PhoneCallListener. how can i pass typedmsg contents to the onCallStateChanged method?


Solution

  • Try this code

    public class MainActivity extends Activity {
    
        private BroadcastReceiver broad;
        ToggleButton togglebtn; 
        Button viewbtn,savebtn,clearbtn;
        EditText ed1;
        String typedmsg;
    
        SmsManager smsManager = SmsManager.getDefault();
    
        //String appstatus = togglebtn.getText().toString();
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            togglebtn=(ToggleButton)findViewById(R.id.toggleButton1);
            savebtn=(Button)findViewById(R.id.button1);
            viewbtn=(Button)findViewById(R.id.button3);
            clearbtn=(Button)findViewById(R.id.button2);
            ed1=(EditText)findViewById(R.id.editText1);
            typedmsg=ed1.getText().toString();
            togglebtn.setChecked(true);
    
            broad = new BroadcastReceiver() {
    
                @Override
                public void onReceive(Context context, Intent intent) {
                    // TODO Auto-generated method stub
    
                    if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)) {
                        // This code will execute when the phone has an incoming call
    
    
    
                        // get the phone number
                        String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
                        Toast.makeText(context, "Call from:" +incomingNumber, Toast.LENGTH_LONG).show();
                       if(togglebtn.isEnabled())
                       { 
                        smsManager.sendTextMessage(incomingNumber, null, typedmsg, null,null);
                       }   
                    }           
                }
            };
    
            registerReceiver(broad, new IntentFilter("android.intent.action.PHONE_STATE"));
    
            clearbtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { ed1.setText("");}});
    
            final AudioManager am  ;
            am= (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE);
            //final int mode = am.getRingerMode();
    
            togglebtn.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    
                    if(isChecked)
                    {
                        savebtn.setVisibility(1);
                        viewbtn.setVisibility(1);
                        clearbtn.setVisibility(1);
                        ed1.setVisibility(1);
                        am.setRingerMode(AudioManager.RINGER_MODE_SILENT);
                        Toast.makeText(getApplicationContext(),"App Enabled and Sound profile se to Silent", Toast.LENGTH_SHORT).show();
                    }
                    else
                    {
                        savebtn.setVisibility(4);
                        viewbtn.setVisibility(4);
                        clearbtn.setVisibility(4);
                        ed1.setVisibility(4);
                        am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                        Toast.makeText(getApplicationContext(),"App Disabled and Sound profile se to Normal", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
            if (id == R.id.action_settings) 
            {
                Toast.makeText(getApplicationContext(), "Settings seleted", Toast.LENGTH_SHORT).show();
                return true;
            }else 
                if(id==R.id.a1){
                Toast.makeText(getApplicationContext(), "Help seleted", Toast.LENGTH_SHORT).show();
                return true;
            }else 
                if (id==R.id.a2){
                Toast.makeText(getApplicationContext(), "Contact Developer seleted", Toast.LENGTH_SHORT).show();
                return true;    
            }       
            return super.onOptionsItemSelected(item);
        }
    }
    

    i have done a similar project like this recently.