我想在android中制作一个应用程序,当我点击按钮时,来电屏幕应该打开。请帮我 -
- 是否可以在自己的应用程序中使用来电屏幕?
- 如果是,那么请帮助我如何使用它?
提前致谢
如果您将“呼叫屏幕”称为默认电话应用程序,那么答案是否定的,但您可以使用意图启动它(请参阅:如何创建打开呼叫日志活动的意图?)但它不会在您的应用程序中。
或者:您可以访问电话历史记录(请参阅:如何访问 android 的通话记录?)并以您的方式显示它(可能在 ListView 中)
试试这个方法
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" />
欢呼