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

서비스는 무엇입니까? BroadcastReceiver? –

+0

선택 사항입니다. 활동을 현장에서 직접 사용할 수 있습니다. –

+0

어떻게 그것을 필드로 사용할 수 있습니까? –

관련 문제