Android的拨号程序在后台 [英] Android Dialer in the Background

查看:183
本文介绍了Android的拨号程序在后台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能调用 Intent.ACTION_CALL 中的背景是什么?

Is it possible to call the Intent.ACTION_CALL in the background ?

我想我的应用程序调用,但我不希望它放在大背景下,我希望它留在前台它在调用时。

I want my application to call but I don't want it to put in the background, I want it to stay in the foreground while it is calling.

推荐答案

您可以使用此拨打电话后,调用你的活动,

You can invoke your activity after making a call using this,

package com.sdi.androidcall;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

    Button call;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        call = (Button) findViewById(R.id.call);

        final PhoneCallListener phoneListener = new PhoneCallListener();
        final TelephonyManager telephonyManager = (TelephonyManager) this
                .getSystemService(Context.TELEPHONY_SERVICE);

        call.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                try {
                    Intent callIntent = new Intent(Intent.ACTION_CALL);
                    callIntent.setData(Uri.parse("tel:" + phoneNumber));
                    startActivity(callIntent);
                } catch (ActivityNotFoundException e) {
                    e.printStackTrace();
                }

                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        telephonyManager.listen(phoneListener,
                                PhoneStateListener.LISTEN_CALL_STATE);
                    }
                }, 10000);

            }
        });
    }


    private class PhoneCallListener extends PhoneStateListener {

        boolean flag = true;

        String LOG_TAG = "Call TEST";

        @Override
        public void onCallStateChanged(int state, String incomingNumber) {

            if (TelephonyManager.CALL_STATE_RINGING == state) {

                Log.i(LOG_TAG, "number: " + incomingNumber);

            }

            if (TelephonyManager.CALL_STATE_OFFHOOK == state) {

                Log.i(LOG_TAG, "OFFHOOK");

                // invoking activity
                if (flag) {
                    Intent i = getBaseContext().getPackageManager()
                            .getLaunchIntentForPackage(
                                    getBaseContext().getPackageName());
                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(i);
                    System.out.println("flagged retrieving app");
                    flag = false;

                }

            }

            if (TelephonyManager.CALL_STATE_IDLE == state) {

                Log.i(LOG_TAG, "IDLE");

                Log.i(LOG_TAG, "restart app");

            }
        }
    }

}

该活动将在10秒在这里使用的延迟之后被称为 PhoneStateListener

您需要指定在mainfest意图权限

You need to specify the intent permissions in the mainfest as

<uses-permission android:name="android.permission.CALL_PHONE"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

试试吧,让我知道的工作。

Try it, let me know if worked.

这篇关于Android的拨号程序在后台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆