简体   繁体   中英

Android: how can I use incoming call screen in own application?

I want to make an application in android, when I click on button then incoming call screen should be open. Please help me--

  1. Is is possible to use incoming call screen in own application ?
  2. If yes, then please help me how can I use it ?

Thanks in advance

If you are calling "call screen" the default Phone application then the answer is no, but you can start it with an intent (see this : How do I create an Intent that opens the Call Log Activity? ) but it won't be IN your application.

OR : you can access to the phone call history (see this : How do I access call log for android? ) and display it it in your way (probably in a ListView)

try this way

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import android.telephony.PhoneStateListener;
import android.telephony.SmsManager;
import android.telephony.TelephonyManager;
import android.util.Log;

import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View;

public class MainActivity extends Activity implements View.OnClickListener{

TextView textView;
TelephonyManager telephonyManager;
PhoneStateListener listener;

Button btndial;
EditText txtdialnum;

Button btnsend;
EditText txtsms;

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

    initialize();

    // add PhoneStateListener
    PhoneCallListener phoneListener = new PhoneCallListener();
    TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
    telephonyManager.listen(phoneListener,PhoneStateListener.LISTEN_CALL_STATE);

}

private void initialize() {
    // TODO Auto-generated method stub
    btndial = (Button) findViewById(R.id.btnDial);
    btndial.setOnClickListener(this);

    txtdialnum = (EditText) findViewById(R.id.editText1);

    btnsend = (Button) findViewById(R.id.btnSend);
    btnsend.setOnClickListener(this);

    txtsms = (EditText) findViewById(R.id.editText2);

}

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

    switch (v.getId()) {
    case R.id.btnDial:
        String strnum = txtdialnum.getText().toString().trim();
        Intent callIntent = new Intent(Intent.ACTION_CALL);
        callIntent.setData(Uri.parse("tel:" + strnum));
        startActivity(callIntent);
        break;

    case R.id.btnSend:
        String strsmsnum = txtdialnum.getText().toString().trim();
        String strsmsmsg = txtsms.getText().toString().trim();;
        try {
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage(strsmsnum, null, strsmsmsg, null, null);
            Toast.makeText(getApplicationContext(), "SMS Sent!",Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(),"SMS faild, please try again later!",Toast.LENGTH_LONG).show();
            e.printStackTrace();

        }

        break;

    }

}


//monitor phone call activities
    private class PhoneCallListener extends PhoneStateListener {

        private boolean isPhoneCalling = false;

        String LOG_TAG = "LOGGING 123";

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

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

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

                if (isPhoneCalling) {

                    Log.i(LOG_TAG, "restart app");

                    // restart app
                    Intent i = getBaseContext().getPackageManager() .getLaunchIntentForPackage(getBaseContext().getPackageName());
                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(i);

                    isPhoneCalling = false;
                }

            }
        }
    }

}

xml

<TextView
    android:id="@+id/txtHead"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="@string/txtHead"
    android:textSize="22sp" />

<TextView
    android:id="@+id/txtfld"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/txtHead"
    android:layout_below="@+id/txtHead"
    android:text="@string/txtfld" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/txtfld"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="16dp"
    android:ems="10"
    android:inputType="phone" />

<Button
    android:id="@+id/btnDial"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText1"
    android:layout_marginTop="22dp"
    android:layout_toRightOf="@+id/txtfld"
    android:text="@string/btnDial" />

<EditText
    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText1"
    android:layout_below="@+id/btnDial"
    android:layout_marginTop="70dp"
    android:ems="10" />

<Button
    android:id="@+id/btnSend"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/btnDial"
    android:layout_below="@+id/editText2"
    android:layout_marginTop="26dp"
    android:text="@string/btnSend" />

Add permissions on manifest.xml

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.SEND_SMS" />

cheerz

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM