How To Start Phonestatelistener Programmatically?
There's an activity in my application. It contains a button. By clicking the button it should be start PhoneStateListener (and BroadcastReceiver?) to catch incoming and outgoing c
Solution 1:
Try this:
publicclassmyActivityextendsActivity{
privateTelephonyManager telephonyManager = null;
publicvoidonCreate(Bundle savedInstanceState) {
// setcontentview and other
button.setOnClickListener(newOnClickListener(){
publicvoidonClick(View arg0) {
btnClick();
}
});
}
privatevoidbtnClick(){
telephonyManager = (TelephonyManager)getApplicationContext()
.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(newPhoneStateListener(){
@OverridepublicvoidonCallStateChanged(int state, String incomingNumber) {
switch(state){
caseTelephonyManager.CALL_STATE_IDLE:
/*your code*/break;
caseTelephonyManager.CALL_STATE_OFFHOOK:
/*your code*/break;
caseTelephonyManager.CALL_STATE_RINGING:
/*your code*/break;
}
//super.onCallStateChanged(state, incomingNumber);
}
}, PhoneStateListener.LISTEN_CALL_STATE);
}
}
Solution 2:
you have to used this code and it is 100% work.
(1) you have to start services
startService(newIntent(this, CallServices.class));
(2) you have to make CallServices class and start your contentobser.
publicclassCallServicesextendsService{
privatestaticfinalStringTAG="CallService";
Context mContext;
@Overridepublic IBinder onBind(Intent intent) {
Log.d(TAG, "onBind");
returnnull;
}
@OverridepublicvoidonCreate() {
Log.d(TAG, "onCreate");
mContext=getApplicationContext();
}
@OverridepublicvoidonStart(Intent intent, int startId) {
//super.onStart(intent, startId);
Log.d(TAG, "onStart services is started");
UrimediaUri= Uri.parse("content://call_log/calls");
Handlerhandler=newHandler(){};
CustomContentObservercustObser=newCustomContentObserver(handler,mContext);
mContext.getContentResolver().registerContentObserver(mediaUri, true, custObser);
}
}
(3) make CustomContentObserver class to getting you to call log.
publicclassCustomContentObserverextendsContentObserver {
long lastTimeofCall = 0L;
long lastTimeofUpdate = 0L;
long threshold_time = 10000;
publicCustomContentObserver(Handler handler,Context con) {
super(handler);
this.mContext=con;
System.out.println("Content obser");
callflag=true;
}
@OverridepublicbooleandeliverSelfNotifications() {
returnfalse;
}
publicvoidonChange(boolean selfChange) {
super.onChange(selfChange);
lastTimeofCall = System.currentTimeMillis();
if(lastTimeofCall - lastTimeofUpdate > threshold_time){
if(callflag){
System.out.println("Content obser onChange()");
Log.d("PhoneService", "custom StringsContentObserver.onChange( " + selfChange + ")");
//if(!callFlag){ String[] projection = newString[]{CallLog.Calls.NUMBER,
CallLog.Calls.TYPE,
CallLog.Calls.DURATION,
CallLog.Calls.CACHED_NAME,
CallLog.Calls._ID};
Cursor c;
c=mContext.getContentResolver().query(CallLog.Calls.CONTENT_URI, projection, null, null, CallLog.Calls._ID + " DESC");
if(c.getCount()!=0){
c.moveToFirst();
lastCallnumber = c.getString(0);
lastCallnumber=FormatNumber(lastCallnumber);
Stringtype=c.getString(1);
String duration=c.getString(2);
String name=c.getString(3);
String id=c.getString(4);
System.out.println("CALLLLing:"+lastCallnumber+"Type:"+type);
Database db=newDatabase(mContext);
Cursor cur =db.getFirstRecord(lastCallnumber);
System.out.println("CUSTOM GALLARY..."+cur.getCount());
final String endCall=lastCallnumber;
//checking incoming/outgoing callif(type.equals("1")){
//incoming call code
}elseif(type.equals("2")){
//outgoing call code
} else{
//missed call code
}
}
lastTimeofUpdate = System.currentTimeMillis();
}
}
}
}
in this way you have to read call log data. i am using content Observer because of in htc device can not read phonestateListner that's way.
Post a Comment for "How To Start Phonestatelistener Programmatically?"