2016-09-19 1 views
-1

내가 걸려 오는 전화를 열 수 있도록 클래스에 일부 기능을 추가하는 것을 시도했다, 그러나 나는 매개 변수 "컨텍스트"컨텍스트 매개 변수를 acceptCall 함수와 함께 사용하는 방법?

enter image description here

package com.bitgriff.androidcalls; 
import android.app.AlarmManager; 
import android.app.PendingIntent; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.telephony.PhoneStateListener; 
import android.telephony.TelephonyManager; 
import android.view.KeyEvent; 
import android.widget.Toast; 

public class CallHelper { 

    private Context ctx; 
    private TelephonyManager tm; 
    private CallStateListener callStateListener; 
    private OutgoingReceiver outgoingReceiver; 
    /* 
    public Context context; 


    private CallHelper(Context context) { 

     this.context = context.getApplicationContext(); 

    } 
*/ 
    /** 
    * Listener to detect incoming calls. 
    */ 

    public CallHelper(Context ctx) { 
     this.ctx = ctx; 
     callStateListener = new CallStateListener(); 
     outgoingReceiver = new OutgoingReceiver(); 
    } 


    private class CallStateListener extends PhoneStateListener { 
     @Override 
     public void onCallStateChanged(int state, String incomingNumber) { 
      switch (state) { 
      case TelephonyManager.CALL_STATE_RINGING: 
       // called when someone is ringing to this phone 
       //Not work 
       acceptCall(Context ctx); 
       //----------------------//-------------------------------- 
       Toast.makeText(ctx,"Incoming: "+incomingNumber,Toast.LENGTH_LONG).show(); 
       break; 
      } 
     } 
    } 

    public void acceptCall(Context context){ 
     Intent buttonUp = new Intent(Intent.ACTION_MEDIA_BUTTON); 
     buttonUp.putExtra(Intent.EXTRA_KEY_EVENT,new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK)); 
     context.sendOrderedBroadcast(buttonUp,"android.permission.CALL_PRIVILEGED"); 
    } 


/* 
    private void rejectCall(Context context) 
    { 
     Intent buttonDown = new Intent(Intent.ACTION_MEDIA_BUTTON); buttonDown.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HEADSETHOOK)); 
     context.sendOrderedBroadcast(buttonDown, "android.permission.CALL_PRIVILEGED"); 
    } 
    */ 
    /** 
    * Broadcast receiver to detect the outgoing calls. 
    */ 
    public class OutgoingReceiver extends BroadcastReceiver { 
     public OutgoingReceiver() { 
     } 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER); 

      Toast.makeText(ctx, 
        "Outgoing: "+number, 
        Toast.LENGTH_LONG).show(); 
     } 

    } 

    /** 
    * Start calls detection. 
    */ 
    public void start() { 
     tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE); 
     tm.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE); 

     IntentFilter intentFilter = new IntentFilter(Intent.ACTION_NEW_OUTGOING_CALL); 
     ctx.registerReceiver(outgoingReceiver, intentFilter); 
    } 

    /** 
    * Stop calls detection. 
    */ 
    public void stop() { 
     tm.listen(callStateListener, PhoneStateListener.LISTEN_NONE); 
     ctx.unregisterReceiver(outgoingReceiver); 
    } 

} 
+1

매개 변수 옆에 변수 유형을 넣을 수 없습니다 .... –

답변

1

하나는하지의 통과에 발생하는 문제가 Context

acceptCall(ctx); 

을 넣어 또는 그것을

acceptCall((Context) ctx); 
012 캐스트

유효하지 않은 구문입니다.

acceptCall(Context ctx); 
관련 문제