2012-02-20 105 views
0

我的应用程序中有一项活动。它包含一个按钮。通过点击按钮,它应该启动PhoneStateListener(和BroadcastReceiver?)来捕获传入和传出的呼叫。它似乎应该是一项服务。如何以编程方式启动PhoneStateListener?

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

回答

1

,你必须使用此代码,它是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(); 
    } 
} 

}

}

这样你要读的通话记录资料

。 我正在使用内容观察器,因为在HTC设备无法读取phonestateListner的方式。

3

试试这个:

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

    } 

} 
+0

服务是怎么回事?广播接收器? – 2012-02-20 12:42:45

+0

可选。你可以直接在你的活动中使用它作为一个字段。 – 2012-02-20 12:44:45

+0

如何使用它作为一个领域? – 2012-02-20 13:07:53