Android 如何在来电时激活广播接收器并使用ITelephony切断通话?

Android 如何在来电时激活广播接收器并使用ITelephony切断通话?,android,Android,我在我的代码中使用了Android SDK的隐藏API(即,ITelephony),但我的广播接收器类在调用时没有激活。正因为如此,我的电话没有结束 我在活动中使用广播接收器是因为我想将活动的上下文用于某些显示目的。我不明白问题出在哪里!请帮忙 package com.example.callprio; import java.lang.reflect.Method; import android.app.Activity; import android.app.ActivityMan

我在我的代码中使用了Android SDK的隐藏API(即,
ITelephony
),但我的广播接收器类在调用时没有激活。正因为如此,我的电话没有结束

我在活动中使用广播接收器是因为我想将活动的上下文用于某些显示目的。我不明白问题出在哪里!请帮忙

 package com.example.callprio;

 import java.lang.reflect.Method;
 import android.app.Activity;
 import android.app.ActivityManager;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.database.Cursor;
 import android.net.Uri;
 import android.os.Bundle;
 import android.provider.ContactsContract;
 import android.telephony.PhoneStateListener;
 import android.telephony.SmsManager;
 import android.telephony.TelephonyManager;
 import android.util.Log;
 import com.android.internal.telephony.ITelephony; 

 public class MyCallBroadcastReceiver extends Activity

 {
private static final boolean USE_ITELEPHONY = true;

/**
    * AIDL access to the telephony service process
    */
    private ITelephony telephonyService;  
  
    private BroadcastReceiver receiver=new BroadcastReceiver()
    {

     private String id;

public void onReceive(Context context, Intent intent) {

     telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
    
     MyPhoneStateListener customPhoneListener = new MyPhoneStateListener();

    telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);

    Bundle bundle = intent.getExtras();
     phone_number = bundle.getString("incoming_number");
    System.out.println("Phone Number : " + phone_number);
    Log.i("phone_number", phone_number);
    
    
    Uri myPhoneUri = Uri.withAppendedPath(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, phone_number);

    // Query the table
    Cursor phoneCursor = MyCallBroadcastReceiver.this.getContentResolver().query(
            myPhoneUri, null, null, null, null);

    // Get the phone numbers from the contact
    for (phoneCursor.moveToFirst(); !phoneCursor.isAfterLast();                
phoneCursor.moveToNext())
    {
   // Get a phone number id
        id = phoneCursor.getString(phoneCursor
                .getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone._ID));

        Log.i("Contact_id", id);
    //    sb.append("Phone: " + phoneNumber + "\n");
    }       
    
    connectToTelephonyService();
    
    Log.i("After connectToTelephonyService()", "Telephony service connected");
          
    fetchPrioById(id);
}
};



private TelephonyManager telephony;


private MyPhoneStateListener customPhoneListener;

private String phone_number;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
}
@Override
protected void onResume() {
this.registerReceiver(receiver, new IntentFilter("android.intent.action.PHONE_STATE"));
super.onResume();
}

@Override
protected void onPause() {
this.unregisterReceiver(receiver);
super.onPause();
}

protected void onDestroy() {
telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_NONE);
}


/** 
 * get an instance of ITelephony to talk handle calls with 
 */
@SuppressWarnings("unchecked") private void connectToTelephonyService() {
    try 
    {
    // "cheat" with Java reflection to gain access to TelephonyManager's ITelephony 
            Class c = Class.forName(telephony.getClass().getName());
            Method m = c.getDeclaredMethod("getITelephony");
            m.setAccessible(true);
            telephonyService = (ITelephony)m.invoke(telephony);

    } catch (Exception e) {
         e.printStackTrace();
         Log.e("TAG",
                 "FATAL ERROR: could not connect to telephony subsystem");
         Log.e("TAG", "Exception object: " + e);
        finish();
    }               
}

protected void fetchPrioById(String id) {

int prio=0;

int cid=Integer.parseInt(id);
 String query="SELECT priority FROM Call WHERE contact_id="+cid;
 
  DatabaseAdapter databaseAdapter = new DatabaseAdapter(getApplicationContext());
  databaseAdapter.open();
 
 Cursor cursor=null;
 
  cursor= databaseAdapter.getSQLiteDatabase().rawQuery(query,null);
 
  if (cursor != null)
     {
      //that means priority is assigned take prio from table
      if (cursor.moveToFirst())
         {
       prio = cursor.getInt(cursor.getColumnIndex("priority"));  
         }
     }
  
  if(prio==0)
  {
      ignoreCall();
  }
  else if(prio==1)
  {
      onSilent(); 
  }
  else if(prio==3)
  {
     sendMessage();
  }

  else if (cursor == null)
  {
      //means priority is not assigned so default behaviour i.e. lower prio
   ignoreCall();      
  }
  
  cursor.close();
  databaseAdapter.open();
 
}//FetchFunc

private void sendMessage() {
// TODO Auto-generated method stub
ignoreCall();
String message="I am in meeting,Call you later";
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phone_number, null, message, null, null);
}

private void onSilent() {
telephonyService.silenceRinger();
}

private void ignoreCall() {
if (USE_ITELEPHONY)
        ignoreCallAidl();
else
        ignoreCallPackageRestart();
}

 /**
  * package restart technique for ignoring calls 
   */
 private void ignoreCallPackageRestart() {
    ActivityManager am = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
    am.restartPackage("com.android.providers.telephony");
    am.restartPackage("com.android.phone");
 }

/** 
  * AIDL/ITelephony technique for ignoring calls
 */
 private void ignoreCallAidl() {
    try 
    {
            telephonyService.silenceRinger();
            telephonyService.endCall();
    } 
    catch(Exception e) 
    {
            e.printStackTrace();
            Log.e("EXCEPTION","FATAL ERROR: call to service method endCall failed.");
            Log.e("TAG", "Exception object: " + e);
    }
}       

}//class Activity ends here