安卓:如何在自己的应用中使用来电界面?

3
我希望在安卓系统上开发一个应用,点击按钮后可以打开来电界面。 请帮助我:
  1. 是否可以在自己的应用中使用来电界面?
  2. 如果可以,那么如何实现呢?
非常感谢!

我知道这是一个老问题,但你找到答案了吗? - Jaswant Singh
2个回答

1

0

试试这个方法

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" />

在 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" />

加油


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接