如何以编程方式启动PhoneStateListener?

时间:2012-02-20 12:28:39

标签: android android-2.3-gingerbread phone-state-listener

我的申请中有一项活动。它包含一个按钮。通过单击按钮,它应该启动PhoneStateListener(和BroadcastReceiver?)来捕获传入和传出的调用。它似乎应该是一种服务。

有没有人能解释如何以编程方式启动PhoneStateListener(和BroadcastReceiver?)?

2 个答案:

答案 0 :(得分:2)

试试这个:

public class myActivity extends Activity{

private TelephonyManager telephonyManager = null;

public void onCreate(Bundle savedInstanceState) {

    // setcontentview and other

    button.setOnClickListener(new OnClickListener(){
           public void onClick(View arg0) {
               btnClick();       
           }
    });

}

private void btnClick(){

telephonyManager = (TelephonyManager)getApplicationContext()
                .getSystemService(Context.TELEPHONY_SERVICE);
        telephonyManager.listen(new PhoneStateListener(){

            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                switch(state){
                    case TelephonyManager.CALL_STATE_IDLE:
                        /*your code*/
                        break;
                    case TelephonyManager.CALL_STATE_OFFHOOK:
                        /*your code*/
                        break;
                    case TelephonyManager.CALL_STATE_RINGING:
                        /*your code*/
                        break;
                }
                //super.onCallStateChanged(state, incomingNumber);
            }

        }, PhoneStateListener.LISTEN_CALL_STATE);

    } 

}

答案 1 :(得分:0)

你必须使用这段代码才能100%正常工作。

(1)你必须开始服务

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

(2)你必须制作CallServices类并启动你的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类让你调用日志。

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();
    }
}

}

}

这样你就必须阅读通话记录数据。 我正在使用内容Observer,因为在htc设备中无法读取phonestateListner的方式。