Android:如何在自己的应用程序中使用来电屏幕?

时间:2013-09-26 10:01:46

标签: android

我想在android中创建一个应用程序,当我点击按钮然后打开来电屏幕。 请帮帮我 -

  1. 可以在自己的应用程序中使用来电屏幕吗?
  2. 如果是,请帮助我如何使用它?
  3. 提前致谢

2 个答案:

答案 0 :(得分:1)

如果您将“呼叫屏幕”称为默认电话应用程序,则答案为否,但您可以通过意图启动它(请参阅:How do I create an Intent that opens the Call Log Activity?)但它不会在您的应用程序中。< / p>

或者:您可以访问电话记录(请参阅:How do I access call log for android?)并以您的方式显示(可能在ListView中)

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

cheerz