如何在Android中拨打电话并在通话结束后回到我的活动中?

hap*_*497 128 android phone-call

我正在启动一个打电话的活动,但当我按下"结束通话"按钮时,它不会返回我的活动.您能否告诉我如何在按下"结束通话"按钮时启动回复给我的通话活动?这就是我拨打电话的方式:

    String url = "tel:3334444";
    Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Run Code Online (Sandbox Code Playgroud)

moo*_*ese 105

使用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);
Run Code Online (Sandbox Code Playgroud)

听众定义:

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");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在您的Manifest.xml文件中添加以下权限:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Run Code Online (Sandbox Code Playgroud)

  • 不要忘记许可.;) (10认同)
  • @moonlightcheese你可以添加代码从通话应用程序回到我们的应用程序吗? (6认同)
  • 正如Gp2mv3所述,不要忘记将READ_PHONE_STATE权限添加到AndroidManifest.xml. (5认同)

Pri*_*ria 49

这是关于Starter提出的问题.

代码的问题在于您没有正确传递数字.

代码应该是:

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);
    }
};
Run Code Online (Sandbox Code Playgroud)

不要忘记在清单文件中添加权限.

<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
Run Code Online (Sandbox Code Playgroud)

要么

<uses-permission android:name="android.permission.CALL_PRIVILEGED"></uses-permission>
Run Code Online (Sandbox Code Playgroud)

用于紧急号码以防万一DIAL.

  • 我很难看到你的代码与原始问题中的代码有什么不同 (7认同)
  • android.permission.CALL_PRIVILEGED权限仅授予应用级别不可用的系统应用程序. (4认同)

And*_*ima 24

我们遇到了同样的问题并设法通过使用a PhoneStateListener来识别呼叫何时结束来解决它,但另外我们必须finish()在再次启动它之前进行原始活动startActivity,否则呼叫日志将在它前面.

  • @André:你的链接似乎被打破了 (11认同)
  • 你可以通过使用不同的方法来避免这种情况.如果您构建一个观察Android调用日志的ContentObserver,那么在调用日志更改之前,应用程序将不会启动.我实际上必须转储PhoneStateListener以支持这个模型,因为我的应用程序需要调用日志数据,并且监听器在进行这些更改之前返回.http://pastebin.com/bq2s9EVa (4认同)

小智 13

我发现EndCallListener是最实用的示例,用于获取描述的行为(finish(),call,restart)我添加了一些SharedPreferences,因此Listener有一个引用来管理这种行为.

我的OnClick,initialise和EndCallListener只响应来自app的调用.其他通话被忽略.

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);

        }

    }
}
Run Code Online (Sandbox Code Playgroud)

将这些添加到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>
Run Code Online (Sandbox Code Playgroud)

如果您需要在通话前回到外观和感觉,那么在您的清单中会出现类似的情况

  <activity android:label="@string/app_name" android:name="com.myPackage.myActivity" 
      android:windowSoftInputMode="stateHidden"
        android:configChanges="keyboardHidden" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <action android:name="android.intent.action.SHOW_PHONE_CALL_LIST" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
  </activity>
Run Code Online (Sandbox Code Playgroud)

把它们放在你的'myActivity'中

public static Activity mApp=null; //Before onCreate()
  ...
onCreate( ... ) {
  ...
if (mApp == null) mApp = this; //Links your resources to other classes
  ...
    //Test if we've been called to show phone call list
    Intent _outcome = getIntent();
    String _phoneCallAction = mApp.getResources().getString(R.string.main_show_phone_call_list);
    String _reqAction = _outcome.getAction();//Can be null when no intent involved

         //Decide if we return to the Phone Call List view
         if (_reqAction != null &&_reqAction.equals(_phoneCallAction) == true) {
                         //DO something to return to look and feel
         }

  ...
        myListView.setOnItemClickListener(new OnItemClickListener() { //Act on item when selected
             @Override
             public void onItemClick(AdapterView<?> a, View v, int position, long id) {

                 myListView.moveToPosition(position);
                 String _bPartyNumber = "tel:"+myListView.getString(myListView.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 

                 //Provide an initial state for the listener to access.
                 initialiseCallStatePreferences(_bPartyNumber);

                 //Setup the listener so we can restart myActivity
                    EndCallListener _callListener = new EndCallListener();
                    TelephonyManager _TM = (TelephonyManager)mApp.getSystemService(Context.TELEPHONY_SERVICE);

                    _TM.listen(_callListener, PhoneStateListener.LISTEN_CALL_STATE);

                         Intent _makeCall = new Intent(Intent.ACTION_CALL, Uri.parse(_bPartyNumber));

                 _makeCall.setComponent(new ComponentName("com.android.phone","com.android.phone.OutgoingCallBroadcaster"));
                    startActivity(_makeCall);                           
                finish();
              //Wait for call to enter the IDLE state and then we will be recalled by _callListener
              }
        });


}//end of onCreate()
Run Code Online (Sandbox Code Playgroud)

使用它来启动myActivity中onClick的行为,例如在onCreate()之后

private void initialiseCallStatePreferences(String _BParty) {
    final int LAUNCHED = -1;
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(
                                mApp.getBaseContext());
    SharedPreferences.Editor _ed = prefs.edit();

    String _prefKey = mApp.getString(R.string.last_phone_call_state_key),
           _bPartyKey = mApp.getString(R.string.last_phone_call_bparty_key);

    //Save default call state before next call
        _ed.putInt(_prefKey,LAUNCHED);
        _ed.putString(_bPartyKey,_BParty);
        _ed.commit();

}
Run Code Online (Sandbox Code Playgroud)

您应该会发现单击您的电话号码列表会完成您的激活,拨打号码并在呼叫结束时返回您的电话号码.

在应用程序外部拨打电话时仍然无法重启您的活动(除非它与上次调用的BParty号码相同).

:)

  • 对不起,但这段代码看起来有些难看.谢谢你的回答 (5认同)

小智 7

你可以使用startActivityForResult()


小智 6

从我的观点来看,这是解决方案:

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);

    }
Run Code Online (Sandbox Code Playgroud)

当然,在Activity(类)定义中,您必须实现View.OnClickListener.


Oye*_*yee 6

这是我的例子,首先用户可以写下他/她想要拨打的号码然后按下呼叫按钮并转到电话.呼叫取消后,用户将被发送回应用程序.为此,按钮需要在xml中具有onClick方法(在此示例中为'makePhoneCall').您还需要在清单中注册权限.

表现

<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Run Code Online (Sandbox Code Playgroud)

活动

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();
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

XML

 <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" />
Run Code Online (Sandbox Code Playgroud)


erl*_*man 6

@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);
}
Run Code Online (Sandbox Code Playgroud)


小智 5

如果您要使用侦听器,则还需要将此权限添加到清单中.

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Run Code Online (Sandbox Code Playgroud)