Implementing Twilio's incoming calling code fr

2019-06-04 15:20发布

Here, I'm trying to show a Dialog for incoming call which is working fine when I implement within an activity but I want to implement in a background service so i can get call anytime when someone calls. I'm using Service for background process but I'm not getting proper success due to some methods. I always get error in same line when I implement in Service but never gets error in Activity.

I'm starting this service from BROADCASTRECEIVER

public class BReciever extends BroadcastReceiver {
Context context;

@Override
public void onReceive(Context con, Intent intent) {
    this.context = con;
    intent = new Intent(context, PingService.class);
    context.startService(intent);
    intent = new Intent(context, IncomingCallService.class);
    intent.putExtra("operator_id", "12");
    context.startService(intent);

}

}

The method where I get error is

  public boolean handleIncomingIntent(Intent intent) {
    Device inDevice = intent.getParcelableExtra(Device.EXTRA_DEVICE);
    Connection inConnection = intent
            .getParcelableExtra(Device.EXTRA_CONNECTION);
    if (inDevice == null && inConnection == null)
        return false;

    intent.removeExtra(Device.EXTRA_DEVICE);
    intent.removeExtra(Device.EXTRA_CONNECTION);

    if (pendingIncomingConnection != null) {
        Log.i(TAG, "A pending connection already exists");
        inConnection.ignore();
        return false;
    }

    pendingIncomingConnection = inConnection;
    pendingIncomingConnection.setConnectionListener(this);

    return true;
}

I always get null in Device and Connection when I call this method via Service.

Service class

public class IncomingCallService extends Service implements LoginListener,
    BasicConnectionListener, BasicDeviceListener, View.OnClickListener,
    CompoundButton.OnCheckedChangeListener,
    RadioGroup.OnCheckedChangeListener {

private static Handler handler;
public BasicPhone phone;
BasicPhoneActivity call_screen;
SharedPreferences login_details;
Vibrator vibrator;
Ringtone r;
private AlertDialog incomingAlert;
Uri notification;
public static final String LOGIN_DETAILS = "u18_sales_logins_details";
AudioManager am;
Intent intent;
static String Twilio_id = "",
        INCOMING_AUTH_PHP_SCRIPT = "http://example.com/client_generate_token?users_id=";
Runnable newrun;
Activity context;
Context ctx;

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onCreate() {
    login_details = getSharedPreferences(LOGIN_DETAILS,
            Context.MODE_PRIVATE);
    if (login_details.contains("twilio_Id")) {
        Twilio_id = login_details.getString("twilio_Id", "");
    }
    phone = BasicPhone.getInstance(getApplicationContext());
    phone.setListeners(this, this, this);
    new IncomingTokenTask().execute();
    onCallHandler();
    Log.d("toekn", BasicPhone.capabilityToken);
}

@Override
public void onDestroy() {
    super.onDestroy();
    if (phone != null) {
        phone.setListeners(null, null, null);
        phone = null;
    }
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub
    op_id = (String) intent.getExtras().get("operator_id");
    if (null != intent) {
                if (phone.handleIncomingIntent(intent)) {
                    showIncomingAlert();
                }
            }
    return START_STICKY;
}

private void showIncomingAlert() {

    handler.post(new Runnable() {
        @Override
        public void run() {
            if (incomingAlert == null) {
                notification = RingtoneManager
                        .getDefaultUri(RingtoneManager.TYPE_RINGTONE);
                r = RingtoneManager.getRingtone(getApplicationContext(),
                        notification);
                am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
                switch (am.getRingerMode()) {
                case AudioManager.RINGER_MODE_SILENT:
                    r.play();
                    break;
                case AudioManager.RINGER_MODE_VIBRATE:
                    long pattern[] = { 0, 500, 200, 300, 500 };
                    vibrator.vibrate(pattern, 0);
                    break;
                case AudioManager.RINGER_MODE_NORMAL:
                    r.play();
                    break;
                }
                incomingAlert = new AlertDialog.Builder(
                        getApplicationContext())
                        .setTitle(R.string.incoming_call)
                        .setCancelable(false)
                        .setMessage(R.string.incoming_call_message)
                        .setPositiveButton(R.string.answer,
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(
                                            DialogInterface dialog,
                                            int which) {
                                        switch (am.getRingerMode()) {
                                        case AudioManager.RINGER_MODE_SILENT:
                                            r.stop();
                                            break;
                                        case AudioManager.RINGER_MODE_VIBRATE:
                                            vibrator.cancel();
                                            break;
                                        case AudioManager.RINGER_MODE_NORMAL:
                                            r.stop();
                                            break;
                                        }
                                        phone.acceptConnection();
                                        incomingAlert = null;
                                    }
                                })
                        .setNegativeButton(R.string.ignore,
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(
                                            DialogInterface dialog,
                                            int which) {
                                        switch (am.getRingerMode()) {
                                        case AudioManager.RINGER_MODE_SILENT:
                                            r.stop();
                                            break;
                                        case AudioManager.RINGER_MODE_VIBRATE:
                                            vibrator.cancel();
                                            break;
                                        case AudioManager.RINGER_MODE_NORMAL:
                                            r.stop();
                                            break;
                                        }
                                        phone.ignoreIncomingConnection();
                                        incomingAlert = null;
                                    }
                                })
                        .setOnCancelListener(
                                new DialogInterface.OnCancelListener() {
                                    @Override
                                    public void onCancel(
                                            DialogInterface dialog) {
                                        phone.ignoreIncomingConnection();
                                    }
                                }).create();
                incomingAlert.show();
            }
        }
    });
}

private void hideIncomingAlert() {
    handler.post(new Runnable() {
        @Override
        public void run() {

            if (incomingAlert != null) {
                incomingAlert.dismiss();
                incomingAlert = null;
            }
        }
    });
}

public void onCallHandler() {

    // TODO Auto-generated method stub

    handler = new Handler();
    newrun = new Runnable() {

        @Override
        public void run() {
            handler.removeCallbacks(newrun);
            new IncomingTokenTask().execute();
               if (null != intent) {
                if (phone.handleIncomingIntent(intent)) {
                    showIncomingAlert();
                }
            }
            handler.postDelayed(this, 200000);
        }
    };
    handler.postDelayed(newrun, 80000);

}

class IncomingTokenTask extends AsyncTask<Void, Void, Void> {
    String message;
    JSONObject jsonResponse;
    int crash_app;

    @Override
    protected void onPreExecute() {
    }

    @Override
    protected Void doInBackground(Void... params) {
        HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
        DefaultHttpClient httpclient = new DefaultHttpClient();
        SchemeRegistry registry = new SchemeRegistry();
        SSLSocketFactory socketFactory = SSLSocketFactory
                .getSocketFactory();
        socketFactory
                .setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
        registry.register(new Scheme("https", socketFactory, 443));
        SingleClientConnManager mgr = new SingleClientConnManager(
                httpclient.getParams(), registry);
        @SuppressWarnings("unused")
        DefaultHttpClient httpClient = new DefaultHttpClient(mgr,
                httpclient.getParams());
        // Set verifier
        HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);// Create
                                                                        // new
                                                                        // HTTP
                                                                        // POST
                                                                        // with
                                                                        // URL
                                                                        // to
                                                                        // php
                                                                        // file
                                                                        // as
                                                                        // parameter

        HttpGet httppost = new HttpGet(INCOMING_AUTH_PHP_SCRIPT + Twilio_id);
        try {
            HttpResponse response = httpclient.execute(httppost);
            if (response.getStatusLine().getStatusCode() == 200) {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    BasicPhone.capabilityToken = EntityUtils
                            .toString(entity);
                    BasicPhone.decodedString = BasicPhone.capabilityToken
                            .replace("\"", "");
                }
            }
        } catch (Exception e) {
            crash_app = 5;
            message = "Something went wrong. Please try again later.";
            return null;
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
    }
}

@Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
    // TODO Auto-generated method stub

}

@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
    // TODO Auto-generated method stub

}

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onDeviceStartedListening() {
    // TODO Auto-generated method stub

}

@Override
public void onDeviceStoppedListening(Exception error) {
    // TODO Auto-generated method stub

}

@Override
public void onIncomingConnectionDisconnected() {
    hideIncomingAlert();
}

@Override
public void onConnectionConnecting() {
    // TODO Auto-generated method stub

}

@Override
public void onConnectionConnected() {
    // TODO Auto-generated method stub

}

@Override
public void onConnectionFailedConnecting(Exception error) {
    // TODO Auto-generated method stub

}

@Override
public void onConnectionDisconnecting() {
    // TODO Auto-generated method stub

}

@Override
public void onConnectionDisconnected() {
    // TODO Auto-generated method stub

}

@Override
public void onConnectionFailed(Exception error) {
    // TODO Auto-generated method stub

}

@Override
public void onLoginStarted() {
    // TODO Auto-generated method stub

}

@Override
public void onLoginFinished() {
    // TODO Auto-generated method stub

}

@Override
public void onLoginError(Exception error) {
    // TODO Auto-generated method stub

}

}

And logcat is

07-16 00:12:56.305: E/AndroidRuntime(1104): FATAL EXCEPTION: main
07-16 00:12:56.305: E/AndroidRuntime(1104): Process: com.u18.enroll, PID: 1104
07-16 00:12:56.305: E/AndroidRuntime(1104): java.lang.NullPointerException
07-16 00:12:56.305: E/AndroidRuntime(1104):     at com.u18.enroll.BasicPhone.handleIncomingIntent(BasicPhone.java:290)
07-16 00:12:56.305: E/AndroidRuntime(1104):     at com.u18.enroll.IncomingCallService$3.run(IncomingCallService.java:217)
07-16 00:12:56.305: E/AndroidRuntime(1104):     at android.os.Handler.handleCallback(Handler.java:733)
07-16 00:12:56.305: E/AndroidRuntime(1104):     at android.os.Handler.dispatchMessage(Handler.java:95)
07-16 00:12:56.305: E/AndroidRuntime(1104):     at android.os.Looper.loop(Looper.java:136)
07-16 00:12:56.305: E/AndroidRuntime(1104):     at android.app.ActivityThread.main(ActivityThread.java:5017)
07-16 00:12:56.305: E/AndroidRuntime(1104):     at java.lang.reflect.Method.invokeNative(Native Method)
07-16 00:12:56.305: E/AndroidRuntime(1104):     at java.lang.reflect.Method.invoke(Method.java:515)
07-16 00:12:56.305: E/AndroidRuntime(1104):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-16 00:12:56.305: E/AndroidRuntime(1104):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-16 00:12:56.305: E/AndroidRuntime(1104):     at dalvik.system.NativeStart.main(Native Method)

I get error in first condition always. Can anyone help me to solve it please?

2条回答
一纸荒年 Trace。
2楼-- · 2019-06-04 15:29

I always get null in Device and Connection when I call this method via Service.

Of course. Here is your code where you are calling startService() on IncomingCallService:

intent = new Intent(context, IncomingCallService.class);
context.startService(intent);

You will notice that you are not putting any extras on this Intent. Therefore, there will be no extras when you try to pull them out of the Intent delivered to the service in onStartCommand(). If you want extras to be on this Intent, you need to type in the putExtra() calls to do so.

查看更多
够拽才男人
3楼-- · 2019-06-04 15:44

So I have solved my problem by just adding a line in Service class

        phone.login(DEFAULT_CLIENT_NAME, true, true);
查看更多
登录 后发表回答