如何在android中拨打电话,并在通话结束后返回我的活动?
我正在启动一个活动以拨打电话,但当我按下“结束通话”按钮时,它不会返回到我的活动。您能告诉我,当按下“结束通话”按钮时,我如何启动通话活动?这就是我打电话的方式:如何在android中拨打电话,并在通话结束后返回我的活动?,android,phone-call,Android,Phone Call,我正在启动一个活动以拨打电话,但当我按下“结束通话”按钮时,它不会返回到我的活动。您能告诉我,当按下“结束通话”按钮时,我如何启动通话活动?这就是我打电话的方式: String url = "tel:3334444"; Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url)); 在我看来,这是一个解决方案: ok.setOnClickListener(this); @Override public void o
String url = "tel:3334444";
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
在我看来,这是一个解决方案:
ok.setOnClickListener(this);
@Override
public void onClick(View view) {
if(view == ok){
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + num));
activity.startActivity(intent);
}
当然,在活动(类)定义中,您必须实现View.OnClickListener。这是关于初学者提出的问题 代码的问题是没有正确地传递数字 代码应为:
private OnClickListener next = new OnClickListener() {
public void onClick(View v) {
EditText num=(EditText)findViewById(R.id.EditText01);
String number = "tel:" + num.getText().toString().trim();
Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse(number));
startActivity(callIntent);
}
};
不要忘记在清单文件中添加权限
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
或
如果使用
拨号
,请拨打紧急电话号码 要返回您的活动
,您需要收听电话状态
。在该侦听器上
您可以发送意图
,以便在手机空闲时重新打开您的活动
至少我会这样做。使用PhoneStateListener查看通话何时结束。您很可能需要触发侦听器操作以等待呼叫启动(等待从PHONE_STATE_OFFHOOK再次更改为PHONE_STATE_IDLE),然后编写一些代码将应用程序恢复到空闲状态 您可能需要在服务中运行侦听器,以确保它保持正常,并重新启动应用程序。一些示例代码:
EndCallListener callListener = new EndCallListener();
TelephonyManager mTM = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
mTM.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE);
侦听器定义:
private class EndCallListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
if(TelephonyManager.CALL_STATE_RINGING == state) {
Log.i(LOG_TAG, "RINGING, number: " + incomingNumber);
}
if(TelephonyManager.CALL_STATE_OFFHOOK == state) {
//wait for phone to go offhook (probably set a boolean flag) so you know your app initiated the call.
Log.i(LOG_TAG, "OFFHOOK");
}
if(TelephonyManager.CALL_STATE_IDLE == state) {
//when this state occurs, and your flag is set, restart your app
Log.i(LOG_TAG, "IDLE");
}
}
}
在Manifest.xml
文件中添加以下权限:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
您可以使用startActivityForResult()尝试使用:
finish();
在活动结束时。它会将您重定向到以前的活动。我们也遇到了同样的问题,并设法通过使用
PhoneStateListener
来识别调用何时结束来解决它,但另外,我们必须完成()
原始活动,然后再使用startActivity
,否则,调用日志将在它前面 如果要使用侦听器,还需要将此权限添加到清单中
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
我发现EndCallListener是功能最强大的示例,为了获得所描述的行为(finish()、call、restart),我添加了一些SharedReference,以便侦听器有一个管理此行为的引用 我的OnClick、initialise和EndCallListener只响应来自应用程序的呼叫。其他呼叫被忽略
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
public class EndCallListener extends PhoneStateListener {
private String TAG ="EndCallListener";
private int LAUNCHED = -1;
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(
myActivity.mApp.getBaseContext());
SharedPreferences.Editor _ed = prefs.edit();
@Override
public void onCallStateChanged(int state, String incomingNumber) {
String _prefKey = myActivity.mApp
.getResources().getString(R.string.last_phone_call_state_key),
_bPartyNumber = myActivity.mApp
.getResources().getString(R.string.last_phone_call_bparty_key);
int mLastCallState = prefs.getInt(_prefKey, LAUNCHED);
//Save current call sate for next call
_ed.putInt(_prefKey,state);
_ed.commit();
if(TelephonyManager.CALL_STATE_RINGING == state) {
Log.i(TAG, " >> RINGING, number: " + incomingNumber);
}
if(TelephonyManager.CALL_STATE_IDLE == state && mLastCallState != LAUNCHED ) {
//when this state occurs, and your flag is set, restart your app
if (incomingNumber.equals(_bPartyNumber) == true) {
//Call relates to last app initiated call
Intent _startMyActivity =
myActivity.mApp
.getPackageManager()
.getLaunchIntentForPackage(
myActivity.mApp.getResources()
.getString(R.string.figjam_package_path));
_startMyActivity.setAction(
myActivity.mApp.getResources()
.getString(R.string.main_show_phone_call_list));
myActivity.mApp
.startActivity(_startMyActivity);
Log.i(TAG, "IDLE >> Starting MyActivity with intent");
}
else
Log.i(TAG, "IDLE after calling "+incomingNumber);
}
}
}
将这些添加到strings.xml
<string name="main_show_phone_call_list">android.intent.action.SHOW_PHONE_CALL_LIST</string>
<string name="last_phone_call_state_key">activityLpcsKey</string>
<string name="last_phone_call_bparty_key">activityLpbpKey</string>
您应该会发现,单击电话号码列表会结束您的活动,拨打该号码,并在通话结束时返回您的活动
在应用程序仍然存在的情况下从应用程序外部拨打电话不会重新启动您的活动(除非它与上次拨打的BParty号码相同)
:)在看到呼叫完成后,在PhoneStateListener内部更好地使用:
Intent intent = new Intent(CallDispatcherActivity.this, CallDispatcherActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
其中CallDispatcherActivity是用户发起呼叫的活动(在我的例子中是呼叫出租车服务调度员)。这只是从顶部删除了Android电话应用程序,用户返回,而不是我在这里看到的丑陋代码。当使用
PhoneStateListener
时,需要确保PHONE\u STATE\u IDLE
之后的PHONE\u OFFHOOK
用于触发呼叫后要执行的操作。如果在看到PHONE\u STATE\u IDLE
时触发,您将在通话前执行此操作。因为您将看到状态更改PHONE\u state\u IDLE->PHONE\u state\u OFFHOOK->PHONE\u state\u IDLE.
@Dmitri Novikov,FLAG\u ACTIVITY\u CLEAR\u TOP
清除新实例之上的任何活动实例。因此,它可能会在完成该过程之前结束旧实例。添加这是您的xml:android:autoLink=“phone”
//在setonclicklistener中输入以下代码:
EditText et_number=(EditText)findViewById(R.id.id_of_edittext);
String my_number = et_number.getText().toString().trim();
Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse(my_number));
startActivity(callIntent);
//授予调用清单的权限:
Intent callIntent=新的意图(Intent.ACTION\u调用);
setData(Uri.parse(“电话:“+number”);
星触觉;
**添加权限:**
开始通话时,它看起来很好
不过,android 11+和down的区别在于将你的应用程序放在最前面
Android 10或更少您需要开始一个新的意图,Android 11+您只需使用BringTaskToFront
在调用状态空闲时:
if (Build.VERSION.SDK_INT >= 11) {
ActivityManager am = (ActivityManager) activity.getSystemService(Activity.ACTIVITY_SERVICE);
am.moveTaskToFront(MyActivity.MyActivityTaskId, ActivityManager.MOVE_TASK_WITH_HOME);
} else {
Intent intent = new Intent(activity, MyActivity.class);
activity.startActivity(intent);
}
我设置了MyActivity.MyActivityTaskId
在调用我的活动时,如果这样做不起作用,请在要返回的页面的父活动页面上设置此变量
MyActivity.MyActivityTaskId = this.getTaskId();
MyActivityTaskId
是我的活动类上的静态变量
public static int MyActivityTaskId = 0;
我希望这对你有用。我使用上面的代码有点不同,我会在接听电话后立即打开我的应用程序,这样用户就可以看到来电者的详细信息
我还在AndroidManifest.xml
中设置了一些内容:
/*Dont really know if this makes a difference*/
<activity android:name="MyActivity" android:taskAffinity="" android:launchMode="singleTask" />
/*我真的不知道这是否会产生影响*/
和权限:
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.REORDER_TASKS" />
如果或当您陷入困境时,请提出问题。步骤:
1) 在Manifest.xml
文件中添加所需的权限
<!--For using the phone calls -->
<uses-permission android:name="android.permission.CALL_PHONE" />
<!--For reading phone call state-->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
3) 在OnCreate中初始化侦听器
EndCallListener callListener = new EndCallListener();
TelephonyManager mTM = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
mTM.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE);
但是如果您希望恢复应用程序的上一个状态或将其从后堆栈中带回来,则将标记活动\u清除\u顶部
替换为标记活动\u单个\u顶部
参考此以下是我的示例,首先用户输入他/她想要拨打的号码,然后按下呼叫按钮并直接拨打电话。取消呼叫后,用户将被发送回应用程序。为此,按钮需要在xml中有一个onClick方法(本例中为“makePhoneCall”)。您还需要在清单中注册权限
显示
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
XML
我有一个困难的t
/*Dont really know if this makes a difference*/
<activity android:name="MyActivity" android:taskAffinity="" android:launchMode="singleTask" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.REORDER_TASKS" />
<!--For using the phone calls -->
<uses-permission android:name="android.permission.CALL_PHONE" />
<!--For reading phone call state-->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
public class EndCallListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
if(TelephonyManager.CALL_STATE_RINGING == state) {
}
if(TelephonyManager.CALL_STATE_OFFHOOK == state) {
//wait for phone to go offhook (probably set a boolean flag) so you know your app initiated the call.
}
if(TelephonyManager.CALL_STATE_IDLE == state) {
//when this state occurs, and your flag is set, restart your app
Intent i = context.getPackageManager().getLaunchIntentForPackage(
context.getPackageName());
//For resuming the application from the previous state
i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
//Uncomment the following if you want to restart the application instead of bring to front.
//i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(i);
}
}
}
EndCallListener callListener = new EndCallListener();
TelephonyManager mTM = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
mTM.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE);
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class PhoneCall extends Activity {
EditText phoneTo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_phone_call);
phoneTo = (EditText) findViewById(R.id.phoneNumber);
}
public void makePhoneCall(View view) {
try {
String number = phoneTo.getText().toString();
Intent phoneIntent = new Intent(Intent.ACTION_CALL);
phoneIntent.setData(Uri.parse("tel:"+ number));
startActivity(phoneIntent);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(PhoneCall.this,
"Call failed, please try again later!", Toast.LENGTH_SHORT).show();
}
}
}
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="@+id/phoneNumber"
android:layout_marginTop="67dp"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Call"
android:id="@+id/makePhoneCall"
android:onClick="makePhoneCall"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent .setData(Uri.parse("tel:+91-XXXXXXXXX"));
startActivity(callIntent );
@Override
public void onClick(View view) {
Intent phoneIntent = new Intent(Intent.ACTION_CALL);
phoneIntent.setData(Uri.parse("tel:91-000-000-0000"));
if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
return;
}
startActivity(phoneIntent);
}