Android = 电话管理器 (Intent.ACTION_CALL)
Android = TelephonyManager (Intent.ACTION_CALL)
我想在 TelephonyManager 状态 IDLE 检测到 CallEnded[=29 上的真实值时拨打电话=]变量。
第一次通话正常,但是当从OFFHOOK变为IDLE时,我需要执行一个新的呼叫意图,但是有没有电话显示。
我错过了什么?
感谢您的宝贵时间。
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Boolean CallEnded = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TelephonyManager telephoneM = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
PhoneStateListener listener = new PhoneStateListener() {
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
if(CallEnded){
performDial();
}
break;
case TelephonyManager.CALL_STATE_RINGING:
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
CallEnded=true;
}
}
};
telephoneM.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
performDial();
}
});
}
private void performDial() {
Intent dial = new Intent(Intent.ACTION_CALL);
dial.setData(Uri.parse("tel:911"));
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED){
return;
}
}
清单
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
第一个问题是您不能 ACTION_CALL
紧急电话号码 911
因为您不是系统应用程序。
意图将转换为 ACTION_DIAL
意图,导致系统 phone 应用程序出现并允许用户手动拨打该号码(如果她选择的话)。
我会先尝试使用标准 phone 号码,以确保这不是问题所在。
第二个问题与在 Android 上两次调用相同的意图有关。
在某些情况下 Android 可能会检测到完全相同的意图,并且会简单地忽略新的意图。
如果这可能是问题所在,请尝试为 url 的数据添加一些不断变化的值,例如:
Intent dial = new Intent(Intent.ACTION_CALL);
dial.setData(Uri.parse("tel:212-555-1234" + "?time=" + System.currentTimeMillis()));
我想在 TelephonyManager 状态 IDLE 检测到 CallEnded[=29 上的真实值时拨打电话=]变量。
第一次通话正常,但是当从OFFHOOK变为IDLE时,我需要执行一个新的呼叫意图,但是有没有电话显示。
我错过了什么?
感谢您的宝贵时间。
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Boolean CallEnded = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TelephonyManager telephoneM = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
PhoneStateListener listener = new PhoneStateListener() {
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
if(CallEnded){
performDial();
}
break;
case TelephonyManager.CALL_STATE_RINGING:
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
CallEnded=true;
}
}
};
telephoneM.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
performDial();
}
});
}
private void performDial() {
Intent dial = new Intent(Intent.ACTION_CALL);
dial.setData(Uri.parse("tel:911"));
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED){
return;
}
}
清单
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
第一个问题是您不能 ACTION_CALL
紧急电话号码 911
因为您不是系统应用程序。
意图将转换为 ACTION_DIAL
意图,导致系统 phone 应用程序出现并允许用户手动拨打该号码(如果她选择的话)。
我会先尝试使用标准 phone 号码,以确保这不是问题所在。
第二个问题与在 Android 上两次调用相同的意图有关。 在某些情况下 Android 可能会检测到完全相同的意图,并且会简单地忽略新的意图。 如果这可能是问题所在,请尝试为 url 的数据添加一些不断变化的值,例如:
Intent dial = new Intent(Intent.ACTION_CALL);
dial.setData(Uri.parse("tel:212-555-1234" + "?time=" + System.currentTimeMillis()));