2016-09-19 73 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); 

这是无效的语法。

acceptCall(Context ctx); 
相关问题