如何通过来自webservice的检查来电信息在来电屏幕上弹出对话活动?

时间:2017-03-17 14:21:06

标签: android web-services broadcastreceiver

在这个项目中,我想在来电屏幕上创建一个来电信息dialog。为此,我尝试获取来电号码并通过此号码询问webservice用户信息。之后,我尝试在来电屏幕上显示dialog,其中包含联系人详细信息。

但是,我的dialog activity未显示在来电之上,但在来电停止后显示。请帮忙吗?

这是我的BroadcastReceiver班级:

package homework.contactor;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Handler;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class ServiceReceiver extends BroadcastReceiver {

    RequestQueue queue;
    private final static String NUMBERS_URL = "http://we-bright.com/numbers.php?number=";
    private String customer = "Deneme";
    @Override
    public void onReceive(final Context context, Intent intent) {
        queue = Volley.newRequestQueue(context);
        TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        telephony.listen(new PhoneStateListener() {
            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                super.onCallStateChanged(state, incomingNumber);
                switch (state){
                    case TelephonyManager.CALL_STATE_RINGING:
                        LoadInfo(context, incomingNumber);
                        Log.d("STATE", "RINGING");
                        break;
                    case TelephonyManager.CALL_STATE_IDLE:
                        LoadInfo(context, incomingNumber);
                        Log.d("STATE", "IDLE");
                }
            }
        }, PhoneStateListener.LISTEN_CALL_STATE);
    }

    private void LoadInfo(final Context context, String number){
        String url = NUMBERS_URL + number;
        Log.d("TEL", url);
        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
                url,
                new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {
                        if (response==null || response.length()==0){
                            return;
                        }
                        JSONArray jsonArray = null;
                        try {
                            jsonArray = response.getJSONArray("posts");
                            JSONObject jObject = jsonArray.getJSONObject(0);
                            customer = jObject.getString("name") + " " + jObject.getString("surname");

                            // Logging and tosting
                            Log.d("CUSTOMER", customer);
                            Toast.makeText(context, customer, Toast.LENGTH_SHORT).show();

                            // activity intent
                            openDetails(context);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });
        // Add the request to the RequestQueue.
        queue.add(jsonObjReq);
    }

    public void openDetails(final Context context){
        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                Intent intent = new Intent();
                intent.setClassName("homework.contactor", "homework.contactor.MainActivity");
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                intent.putExtra("customer", customer);
                context.startActivity(intent);
            }
        }, 1000);
    }

}

这是我的主要活动类:

package homework.contactor;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.TextView;

import java.util.List;

public class MainActivity extends AppCompatActivity{

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

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
        getWindow().addFlags(
                WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);

        tvName = (TextView) findViewById(R.id.tvName);
        String customer = getIntent().getStringExtra("customer");
        tvName.setText(customer);
    }

    public boolean isPhoneStatePermissionGranted() {
        if (Build.VERSION.SDK_INT >= 23) {
            if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    == PackageManager.PERMISSION_GRANTED) {
                return true;
            } else {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, 1);
                return false;
            }
        }
        else { //permission is automatically granted on sdk<23 upon installation
            return true;
        }
    }

}

以下是AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="homework.contactor">

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity"
            android:theme="@style/Theme.AppCompat.Dialog">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name=".ServiceReceiver">
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
        </receiver>

    </application>

</manifest>

1 个答案:

答案 0 :(得分:0)

只需将此案例添加到您的onCallStateChanged

case TelephonyManager.CALL_STATE_OFFHOOK:

                //Transition of ringing->offhook are pickups of incoming calls.  Nothing done on them
                if(lastState != TelephonyManager.CALL_STATE_RINGING){
                    isIncoming = false;
                    callStartTime = new Date();
                    showVideoDialog(context, "outgoing Ringing: "+savedNumber);
                    //onOutgoingCallStarted

                }
                else
                {
                    isIncoming = true;
                    callStartTime = new Date();


                }
                    isIncoming = false;
                    callStartTime = new Date();

               //

                break;