Thursday 22 March 2012

Demo Receive Call



import java.lang.reflect.Method;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

import com.android.internal.telephony.ITelephony;

public class DempReceivePhone extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TelephonyManager tl = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
myPhoneListener ph = new myPhoneListener();
tl.listen(ph, PhoneStateListener.LISTEN_CALL_STATE);

}

public class myPhoneListener extends PhoneStateListener {

/*
* (non-Javadoc)
*
* @see android.telephony.PhoneStateListener#onCallStateChanged(int,
* java.lang.String)
*/
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
ITelephony telServices = getTeleService();
;
if (telServices != null) {
telServices.answerRingingCall();
}
Toast.makeText(getApplicationContext(), "Answer Call", 1000);

break;
case TelephonyManager.CALL_STATE_IDLE:
deleteCallLogEntry();
break;
default:
break;
}

}

}

private ITelephony getTeleService() {
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
com.android.internal.telephony.ITelephony telephonyService = null;
try {
// Java reflection to gain access to TelephonyManager's
// ITelephony getter

Class c = Class.forName(tm.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
telephonyService = (ITelephony) m.invoke(tm);
telephonyService.endCall();
// count=telephonyService.getCallState();
Log.i("TEST",
"SHOWSCREEN: " + telephonyService.getActivePhoneType());
// telephonyService.call("9408488605");

} catch (Exception e) {
e.printStackTrace();

}
return telephonyService;
}

public void deleteCallLogEntry() {
// TODO Auto-generated method stub

String strUriCalls = "content://call_log/calls";

Uri UriCalls = Uri.parse(strUriCalls);

Cursor c = DempReceivePhone.this.getContentResolver().query(UriCalls,
null, null, null, null);

if (c.getCount() <= 0)

{

Toast.makeText(getApplicationContext(), "Call log empty",
Toast.LENGTH_SHORT).show();

}

if (c.moveToFirst()) {
//while (c.moveToNext())

{

String strNumber = fetchOutgoingNumber();

String queryString = "NUMBER='" + strNumber + "'";

Log.v("Number", queryString);

int i = DempReceivePhone.this.getContentResolver().delete(
UriCalls, queryString, null);

if (i >= 1)

{

Toast.makeText(getApplicationContext(), "Number deleted",
Toast.LENGTH_SHORT).show();

}

else

{

Toast.makeText(getApplicationContext(),
"No such number in call logs", Toast.LENGTH_SHORT)
.show();

}

}
}

}

private String fetchOutgoingNumber() {
String[] STR_FIELDS = new String[] { android.provider.CallLog.Calls.NUMBER };
Cursor cr = getContentResolver().query(
android.provider.CallLog.Calls.CONTENT_URI, STR_FIELDS, null,
null, null);
cr.moveToLast();

return cr.getString(0);

}
}

1 comment:

  1. this is nic. but how can i open activity after cut received call.???

    ReplyDelete