本文介绍了如何以编程方式启动PhoneStateListener?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个在我的应用程序的活动。它包含一个按钮。通过点击按钮,它应该是开始PhoneStateListener(和BroadcastReceiver的?)赶上呼入和呼出。现在看来,这应该是一个服务。

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 calls. It seems it should be a service.

有谁可以解释如何启动PhoneStateListener(和BroadcastReceiver的?)编程?

Does anyone can explain how to start PhoneStateListener (and BroadcastReceiver?) programmatically?

推荐答案

您必须使用此code和它是100%的工作。

you have to used this code and it is 100% work.

(1)必须启动服务

startService(new Intent(this, CallServices.class));

(2)你必须做出Call​​Services类,并启动contentobser。

(2) you have to make CallServices class and start your contentobser.

public class CallServices extends Service{
private static final String TAG = "CallService";
Context mContext;

@Override
public IBinder onBind(Intent intent) {
    Log.d(TAG, "onBind");
    return null;
}

@Override
public void onCreate() {
    Log.d(TAG, "onCreate");
    mContext=getApplicationContext();
}

@Override
public void onStart(Intent intent, int startId) {
    //super.onStart(intent, startId);
    Log.d(TAG, "onStart services is started");
       Uri mediaUri = Uri.parse("content://call_log/calls");
       Handler handler = new Handler(){};
       CustomContentObserver custObser = new CustomContentObserver(handler,mContext);
       mContext.getContentResolver().registerContentObserver(mediaUri, true, custObser);
}

}

(3)作出CustomContentObserver类,让你到通话记录。

(3) make CustomContentObserver class to getting you to call log.

public class CustomContentObserver extends ContentObserver {
long lastTimeofCall = 0L;
long lastTimeofUpdate = 0L;
long threshold_time = 10000;

public CustomContentObserver(Handler handler,Context con) {
    super(handler);
    this.mContext=con;
    System.out.println("Content obser");
    callflag=true;
}
@Override
public boolean deliverSelfNotifications() {
    return false;
}

public void onChange(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 = new String[]{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);
                 String type=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=new Database(mContext);
                 Cursor cur =db.getFirstRecord(lastCallnumber);
                 System.out.println("CUSTOM GALLARY..."+cur.getCount());
                 final String endCall=lastCallnumber;
                 //checking incoming/outgoing call
                 if(type.equals("1")){
                    //incoming call code

                 }else if(type.equals("2")){
                        //outgoing call code
                 } else{
                    //missed call code
                }
            }

        lastTimeofUpdate = System.currentTimeMillis();
    }
}

}

}

在这种方式,你必须读通话记录的数据。
我使用的,因为在HTC设备中的内容观察者无法读取phonestateListner这就是方法。

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.

这篇关于如何以编程方式启动PhoneStateListener?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-03 14:43