Notify call disconnected Twilio Android - android

I have implemented Twilio thrid party SDK for making outgoing calls in android. When i make a call when the number is switched off or refused the call gets disconnected after some time automatically i have been trying to fetch this event.To fetch this event i tried implementing The ConnectionListener interface and has override all the method but when the call gets disconnected or connected i was not able to get any logs printed. After implementing the ConnectionListener i have implemented the following method in my code.
#Override
public void onConnected(Connection arg0) {
// TODO Auto-generated method stub
Log.i("test", "onconnected");
}
#Override
public void onConnecting(Connection arg0) {
// TODO Auto-generated method stub
Log.i("test", "onconnecting");
}
#Override
public void onDisconnected(Connection arg0) {
// TODO Auto-generated method stub
Log.i("test", "onDisconnected");
}
#Override
public void onDisconnected(Connection arg0, int arg1, String arg2) {
// TODO Auto-generated method stub
Log.i("test", "onDisconnected");
}
Can anyone help me on this issue. Thank in advance.

check if you have callbacks to notify your app of outgoing connections.
you should have something like this:
connection = device.connect(params, this);

Just implementing the override functions of Connection listener is not sufficient.
But you need to set the listener to the connection. Like
either
connection = device.connect(parameters, this /* ConnectionListener */);
or
connection.setConnectionListener(this /*ConnectionListener */);
Hope this helps...

Related

Is it possible to call a number just by using proximity sensor?

I am trying to make an app which calls a certain number just by hovering palm over android device. I have created a Background service for doing so. But when I start the service and wave over my device, the app crashes! So my question in that is it possible to make a CERTAIN phone call by making use of change in Sensor readings? Here's my code of Service class
public class ProxService extends Service implements SensorEventListener { SensorManager manager;
Sensor prox;
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
manager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
prox = manager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
manager.registerListener(ProxService.this,prox,SensorManager.SENSOR_DELAY_NORMAL);
return START_STICKY;
}
#Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
manager.unregisterListener(this);
Toast.makeText(getApplicationContext(),"Accelerometer Service Stopped!",0).show();
}
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
//Neglect it!
}
#Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
float reading;
reading = event.values[0];
//AM I GIVING RIGHT CONDITION HERE?
while(reading == 0.0){
Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:+XXXXXXXXXX"));
startActivity(intent);
break;
}
}
}
I have checked all necessary Permissions required in manifest!
First, according to SensorEvent documentation values[0] contains measured sensor distance in centimeters.
Instead of while loop you should add condition whether hand is close enough:
// if hand is closer than 5 cm
if(reading < 5) {
// start activity
}
That while condition is same as if(reading == 0.0), because of break sentence (it never loops).
Also you should never compare float or double with ==. Read this why.
Second, please update you question with Exception log, so we can see what is causing it.

Android SockJS with Vert.X SockJS Server

I have a Vert.X SockJS server running using the following code:
HttpServer httpServer = vertx.createHttpServer();
SockJSServer sockJSServer = vertx.createSockJSServer(httpServer);
JsonObject config = new JsonObject().putString("prefix", "/echo");
sockJSServer.installApp(config, new Handler<SockJSSocket>() {
public void handle(SockJSSocket sock) {
Pump.createPump(sock, sock).start();
}
});
httpServer.listen(8080);
Now I need to send messages from the server to an Android (and vice versa) application and I have no idea how to set that up on the client. The documentation talks about handling that in JavaScript but on the browser.
UPDATE: I believe the following code is a bit in the right direction. I still need to add the host ip address (not sure how).
public void start() {
SockJSSocket client = new SockJSSocketBase(vertx){
#Override
public boolean writeQueueFull(){
// TODO Auto-generated method stub
return false;
}
#Override
public SockJSSocket setWriteQueueMaxSize(int arg0){
// TODO Auto-generated method stub
return null;
}
#Override
public SockJSSocket drainHandler(Handler<Void> arg0){
// TODO Auto-generated method stub
return null;
}
#Override
public SockJSSocket write(Buffer arg0){
// TODO Auto-generated method stub
return null;
}
#Override
public SockJSSocket exceptionHandler(Handler<Throwable> arg0){
// TODO Auto-generated method stub
return null;
}
#Override
public SockJSSocket resume(){
// TODO Auto-generated method stub
return null;
}
#Override
public SockJSSocket pause(){
// TODO Auto-generated method stub
return null;
}
#Override
public SockJSSocket dataHandler(Handler<Buffer> arg0){
// TODO Auto-generated method stub
return null;
}
#Override
public SockJSSocket endHandler(Handler<Void> arg0){
// TODO Auto-generated method stub
return null;
}
};
}
I think you can try a different way, if you want your android app to talk to a vertx server: using websocket, it is easier than using sockjs to setup a connection between them. Because I met and solved a similar requirement not long ago.
Because websocket connection is a two-way channel, which means a vertx app could send text to your android app and vice versa. At the same time, there is Java-WebSocket(http://java-websocket.org/) which you can use for your android app. It is very easy to use.
Also, to set up a websocket handler in vertx is not hard, there are many examples in document.

Binder mechanisam to update UI in android

In my project I am using binder mechanism to communicate to remote service. The remote service class will call JNI function and updates to UI for every 10 seconds. The JNI function has below code
static int n = 100;
return ++n;
This JNI function calling in Service class and updating to UI like below
public class MyService extends Service{
private static final String TAG = "MyService";
private final Handler serviceHandler = new Handler();
#Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
System.out.println("inside service onCreate");
Log.d(TAG, "entered onStart");
serviceHandler.removeCallbacks(sendUpdatesToUI);
serviceHandler.postDelayed(sendUpdatesToUI, 1000); // 1 second
}
1. private IMyService.Stub bioStub = new IMyService.Stub() {
2.
3. #Override
4. public int intFromJNI() throws RemoteException {
5. // TODO Auto-generated method stub
6. int libData = Abcd.intFromJNI();
7. System.out.println("inside ****** stub"+libData);
8. return libData;
}
};
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
System.out.println("inside binder ");
return bioStub;
}
private Runnable sendUpdatesToUI = new Runnable() {
public void run() {
Log.d(TAG, "entered Runnable");
try {
bioStub.intFromJNI();
serviceHandler.postDelayed(this, 10000);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
}
This service class is calling in Activty
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Intent serviceIntent=new Intent();
serviceIntent.setClass(context, MyService.class);
boolean ok=bindService(serviceIntent, mServiceConnection , Context.BIND_AUTO_CREATE);
Log.v("ok", String.valueOf(ok));
}
private ServiceConnection mServiceConnection=new ServiceConnection(){
#Override
public void onServiceConnected(ComponentName arg0, IBinder service) {
// TODO Auto-generated method stub
System.out.println("inside ServiceConnection");
myService = IMyService.Stub.asInterface(service);
try {
Log.d("Client", "entered mServiceConnection");
8. int jniValue = myService.intFromJNI();
9. System.out.println("value if JNI==>"+jniValue);
10.
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#Override
public void onServiceDisconnected(ComponentName arg0) {
// TODO Auto-generated method stub
}
};
Here my problem is, The service class is not updating UI for every 10 seconds and the value is not incremeting in UI but the value is incrementing for every 10 seconds in service class i.e the print statement below is updating for every 10 seconds which is in line Number 7 in above code(Service).
System.out.println("inside ****** stub"+libData);
But I want to it update in UI also. i.e the statement line num 10.
System.out.println("value if JNI==>"+jniValue);
I`t is not happening in my code . How to solve this one .`
You are missing any way for the Service to tell the Activity about new events. The call from the Activity to the Service, intFromJNI, is a one-off function call which happens just once - it has no ongoing responsibility for telling the UI about changes in future.
You should add some listener class. For example, add IMyServiceListener.aidl with something like this:
package com.something;
interface IMyServiceListener {
void valueUpdated(int newValue);
}
And then in IMyService.aidl add this:
import com.something.IMyServiceListener;
void addListener(in IMyServiceListener newListener);
Then, within your IMyService.Stub class you will need to implement addListener - you should add the resulting object to some array of listeners, and call valueUpdated on each listener, each time the value changes.
Within the activity, you should call myService.addListener and pass in some object which receives notification of the changes. You can then display that on the UI within the activity.

how to start the same asynctask on again clicking on the button [duplicate]

This question already has answers here:
Execute AsyncTask several times
(5 answers)
Closed 9 years ago.
I am new to Android and am now working on the counter thing using the AsyncTask.
So the thing is I'm having one button and with that button OnClickListener.
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
boolean check;
check=check_button_status();
if(check==true){
rec_forward_task.execute();
}
else
{
rec_forward_task.cancel();
}
}
});
So here the rec_forward_task is the class that extends the AsyncTask. The AsyncTask class is here.
//
private class CounterForwardTask extends AsyncTask<Void, Integer, Integer>
{
TextView record_counter_display;
int rec_counter,count;
int last_value;
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
record_counter_display=(TextView) findViewById(R.id.rec_voice_counter);
rec_counter=0;
record_counter_display.setText(String.valueOf(rec_counter));
count=0;
}
public void cancel() {
// TODO Auto-generated method stub
onCancelled();
}
#Override
protected Integer doInBackground(Void... params) {
// TODO Auto-generated method stub
while(rec_status)
{
publishProgress(count);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
last_value=count;
count=count+1;
}
return 1;
}
#Override
protected void onCancelled() {
// TODO Auto-generated method stub
record_counter_display.setText(String.valueOf(0));
}
#Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
record_counter_display.setText(String.valueOf(count));
}
});
}
#Override
protected void onPostExecute(Integer result) {
// TODO Auto-generated method stub
record_counter_display.setText(String.valueOf(last_value));
}
}
I'm making the object for that class in the oncreate method only so now the question is.
When user press first time the counter is starting and displaying in the textview and again pressing that same button the counter progress is stop due to oncancel method is called.but if user again pressing the same button app force closing and exception that u can't start the task which is already started.so what will be the way to perform this kind of operation.Thanks for any reply.
if(check==true){
rec_forward_task = new CounterForwardTask();
rec_forward_task.execute();
}
else
{
rec_forward_task.cancel();
}
Instead of instantiating AsyncTask in onCreate instaniate it when you need to start it. Hope this helps.
You will have to create a new AsyncTask object.
AsyncTasks are meant to run only once.
Check this answer.
The async task is designed to run only once. But you can run it by creating new instances of the asynctask class. See the answer of Passionate Androiden
Threading rules
There are a few threading rules that must be followed for this class to work properly:
The AsyncTask class must be loaded on the UI thread. This is done automatically as of JELLY_BEAN.
The task instance must be created on the UI thread.
execute(Params...) must be invoked on the UI thread.
Do not call onPreExecute(), onPostExecute(Result), doInBackground(Params...), onProgressUpdate(Progress...) manually.
The task can be executed only once (an exception will be thrown if a second execution is attempted.)
pls findout more in Developer site

Automatic Logout feature in Android App

I am building a native android app, where I want the user to be automatically logged out (kind of session timeout) after a period of inactivity say 5 mins.
It is a standalone app and there are multiple screens in the App. I am not maintaining any user session with a server.
P.S: I found a possible solution in writing a time out event for android. But that is suitable only for a single Activity application. Can anyone suggest a similar solution for a multi-activity app ?
Ok so in response to the link you posted, why not follow that approach but create some sort of abstract base activity, that all of your other activities extend. So essentially you are adding a timeout to each activity, but your base activity will handle this, your child activities will not need to know what is going on.
public class LogoutService extends Service {
public static CountDownTimer timer;
#Override
public void onCreate(){
// TODO Auto-generated method stub
super.onCreate();
timer = new CountDownTimer(1 *60 * 1000, 1000) {
public void onTick(long millisUntilFinished) {
//Some code
Log.v(Constants.TAG, "Service Started");
}
public void onFinish() {
Log.v(Constants.TAG, "Call Logout by Service");
// Code for Logout
stopSelf();
}
};
}
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
And then Add the below code in every activity.
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
LogoutService.timer.start();
}
#Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
LogoutService.timer.cancel();
}

Categories

Resources