Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android編程中黑名單的實現方法

Android編程中黑名單的實現方法

編輯:關於Android編程

本文實例講述了Android編程中黑名單的實現方法。分享給大家供大家參考,具體如下:

說明:由於掛斷電話android   api不是對外開放的,所以需要使用反射的方法得到撥打電話的服務。

1.將android源代碼中的"aidl"文件拷貝到項目中

這樣項目中會生成兩個包:android.telephony;此包中文件為:NeighboringCellInfo.aidl

com.android.internal.telephony;此包中文件為:ITelephony.aidl

2.通過反射掛斷電話;代碼如下:

/**
* 掛斷電話
*/
public void endCall() {
    try {
      Method method = Class.forName("android.os.ServiceManager").getMethod("getService", String.class);
      IBinder binder = (IBinder) method.invoke(null, new Object[]{TELEPHONY_SERVICE});
      ITelephony telephony = ITelephony.Stub.asInterface(binder);
      telephony.endCall();
    } catch (Exception e) {
      e.printStackTrace();
    }
}

3.刪除通話記錄中的記錄

/**
* 刪除呼叫記錄
*/
public void deleteCallLog(String incomingNumber) {
    ContentResolver resolver = getContentResolver();
    Cursor cursor = resolver.query(CallLog.Calls.CONTENT_URI, null,"number=?", new String[]{incomingNumber}, null);
    if(cursor.moveToNext()){
      String id = cursor.getString(cursor.getColumnIndex("_id"));
      resolver.delete(CallLog.Calls.CONTENT_URI, "_id=?", new String[]{id});
    }
}

4.直接這樣調用是不能刪除電話記錄的,因為生成電話記錄的過程是一個異步的過程,在掛斷電話之後不能立即刪除電話記錄,所以這裡要使用ContentObserver(內容觀察者)

private class MyObserver extends ContentObserver{
    private String incomingNumber;
    public MyObserver(Handler handler,String incomingNumber) {
      super(handler);
      this.incomingNumber = incomingNumber;
    }
    @Override
    public void onChange(boolean selfChange) {
      super.onChange(selfChange);
      deleteCallLog(incomingNumber);
      getContentResolver().unregisterContentObserver(this);
    }
}

6.最後把整個service代碼貼到下面

public class AddressService extends Service{
  private static final String TAG = "AddressService";
  private TelephonyManager manager;
  private MyPhoneListener listener;
  private WindowManager wManager;
  private View view;
  private SharedPreferences sp;
  long startTime = 0;
  long endTime = 0;
  private BlackNumberDao dao;
  @Override
  public IBinder onBind(Intent arg0) {
    return null;
  }
  /**
   * 服務第一次被創建的時候調用的方法
   * 服務被初始化時調用的方法
   */
  @Override
  public void onCreate() {
    super.onCreate();
    listener = new MyPhoneListener();
    manager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
    wManager = (WindowManager) this.getSystemService(WINDOW_SERVICE);
    manager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
    sp = getSharedPreferences("config", MODE_PRIVATE);
    dao = new BlackNumberDao(this);
//   if(3000>(endTime - startTime)){
//     String ns = Context.NOTIFICATION_SERVICE;
//     NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
//     //定義通知欄展現的內容信息
//     int icon = R.drawable.icon5;
//     CharSequence tickerText = "我的通知欄標題";
//     long when = System.currentTimeMillis();
//     Notification notification = new Notification(icon, tickerText, when);
//     //定義下拉通知欄時要展現的內容信息
//     Context context = getApplicationContext();
//     CharSequence contentTitle = "我的通知欄標展開標題";
//     CharSequence contentText = "我的通知欄展開詳細內容";
//     Intent notificationIntent = new Intent(AddressService.this,BootStartDemo.class);
//     PendingIntent contentIntent = PendingIntent.getActivity(AddressService.this, 0,notificationIntent, 0);
//     notification.setLatestEventInfo(context, contentTitle, contentText,contentIntent);
//     //用mNotificationManager的notify方法通知用戶生成標題欄消息通知
//     mNotificationManager.notify(1, notification);
//   }
  }
  /**
   * 服務停止的時候調用
   */
  @Override
  public void onDestroy() {
    super.onDestroy();
    manager.listen(listener, PhoneStateListener.LISTEN_NONE);
    listener = null;
  }
  private class MyPhoneListener extends PhoneStateListener{
    /**
     * 電話狀態發生改變的時候調用的方法
     */
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
      super.onCallStateChanged(state, incomingNumber);
      switch (state) {
      case TelephonyManager.CALL_STATE_IDLE:
        if(null != view){
          wManager.removeView(view);
          view = null;
        }
        endTime = System.currentTimeMillis();
        break;
      case TelephonyManager.CALL_STATE_RINGING: // 零響狀態
        //判斷number是否在黑名單中
        if(dao.find(incomingNumber)){
          //掛斷電話
          endCall();
          //刪除呼叫記錄
//         deleteCallLog(incomingNumber);
          ContentResolver resolver = getContentResolver();
          resolver.registerContentObserver(CallLog.Calls.CONTENT_URI, true, new MyObserver(new Handler(),incomingNumber));
        }
        Log.i(TAG,"來電號碼為"+ incomingNumber);
        String address = NumberAddressService.getAddress(incomingNumber);
        Log.i(TAG,"歸屬地為"+ address);
        showLocation(address);
        //獲取當前系統的時間
        startTime = System.currentTimeMillis();
        break;
      case TelephonyManager.CALL_STATE_OFFHOOK: //接通電話狀態
        break;
      }
    }
  }
  /**
   * 在窗體上顯示出來位置信息
   * @param address
   */
  public void showLocation(String address) {
    WindowManager.LayoutParams params = new WindowManager.LayoutParams();
    params.height = WindowManager.LayoutParams.WRAP_CONTENT;
    params.width = WindowManager.LayoutParams.WRAP_CONTENT;
    params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
        | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
        | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
    params.format = PixelFormat.TRANSLUCENT;
    params.type = WindowManager.LayoutParams.TYPE_TOAST;
    params.setTitle("Toast");
    params.gravity = Gravity.LEFT | Gravity.TOP;
    int x = sp.getInt("lastx", 0);
    int y = sp.getInt("lasty", 0);
    params.x = x;
    params.y = y;
    view = View.inflate(getApplicationContext(), R.layout.show_location, null);
    LinearLayout ll_location = (LinearLayout) view.findViewById(R.id.ll_location);
    TextView tv_location = (TextView) view.findViewById(R.id.tv_location);
    int background = sp.getInt("background", 0);
    if(0 == background){
      ll_location.setBackgroundResource(R.drawable.call_locate_gray);
    }else if(1 == background){
      ll_location.setBackgroundResource(R.drawable.call_locate_orange);
    }else {
      ll_location.setBackgroundResource(R.drawable.call_locate_green);
    }
    tv_location.setText(address);
    tv_location.setTextSize(24);
    wManager.addView(view, params);
  }
  /**
   * 刪除呼叫記錄
   */
  public void deleteCallLog(String incomingNumber) {
    ContentResolver resolver = getContentResolver();
    Cursor cursor = resolver.query(CallLog.Calls.CONTENT_URI, null,"number=?", new String[]{incomingNumber}, null);
    if(cursor.moveToNext()){
      String id = cursor.getString(cursor.getColumnIndex("_id"));
      resolver.delete(CallLog.Calls.CONTENT_URI, "_id=?", new String[]{id});
    }
  }
  /**
   * 掛斷電話
   */
  public void endCall() {
    try {
      Method method = Class.forName("android.os.ServiceManager").getMethod("getService", String.class);
      IBinder binder = (IBinder) method.invoke(null, new Object[]{TELEPHONY_SERVICE});
      ITelephony telephony = ITelephony.Stub.asInterface(binder);
      telephony.endCall();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  private class MyObserver extends ContentObserver{
    private String incomingNumber;
    public MyObserver(Handler handler,String incomingNumber) {
      super(handler);
      this.incomingNumber = incomingNumber;
    }
    @Override
    public void onChange(boolean selfChange) {
      super.onChange(selfChange);
      deleteCallLog(incomingNumber);
      getContentResolver().unregisterContentObserver(this);
    }
  }
}

更多關於Android相關內容感興趣的讀者可查看本站專題:《Android開發入門與進階教程》、《Android通信方式總結》、《Android基本組件用法總結》、《Android視圖View技巧總結》、《Android布局layout技巧總結》及《Android控件用法總結》

希望本文所述對大家Android程序設計有所幫助。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved