Android開發之黑名單來電自動掛斷,android黑名單

來源:互聯網
上載者:User

Android開發之黑名單來電自動掛斷,android黑名單

          本執行個體允許使用者動態添加號碼到黑名單,並實現黑名單來電自動掛斷。程式通過建立PhoneStateListener監聽器來監聽TelephonyManager的通話狀態來實現該功能。

由於自Android 10之後Android不再對外公開掛斷電話的API,如果需要掛斷電話必須使用AIDL與電話管理Service進行通訊,並調用服務中的API實現結束電話。

為了調用遠端AIDL Service,開發人員需要將Android源碼中的如下兩個文拷到指定位置:

com.android.internal.telephony包下的ITelephony.aidl

android.telephony包下的NeighboringCellInfo.aidl


這樣就會在gen檔案夾下產生相應的.Java檔案。

另外調用API的方法如果有不解的地方可以參考:Class.forName()的作用與使用總結

另外本執行個體使用BaseAdapter作為ListView的適配器,BaseAdapter具有很強的定製型。

示範圖片:


程式碼:

package com.jph.callguard;import java.lang.reflect.Method;import java.util.ArrayList;import com.android.internal.telephony.ITelephony;import android.os.Bundle;import android.os.IBinder;import android.provider.ContactsContract;import android.telephony.PhoneStateListener;import android.telephony.TelephonyManager;import android.view.View;import android.view.ViewGroup;import android.view.View.OnClickListener;import android.widget.BaseAdapter;import android.widget.Button;import android.widget.CheckBox;import android.widget.ListView;import android.app.Activity;import android.app.AlertDialog;import android.content.DialogInterface;import android.database.Cursor;/** * Describe:</br> * 黑名單自動掛斷 * 本執行個體實現了擷取連絡人,並將選中的連絡人添加到黑名單中 * 並通過ITelephony.aidl與NeighboringCellInfo.aidl * 介面使用AIDL與電話管理Servic進行通訊掛斷黑名單中的電話 * @author jph * Time:2014.07.24  * */public class CallGuard extends Activity {Button btnManage;TelephonyManager tManager;//建立一個集合用於儲存黑名單中的號碼ArrayList<String>blockList=new ArrayList<String>();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);btnManage=(Button)findViewById(R.id.btnManage);tManager=(TelephonyManager) getSystemService(TELEPHONY_SERVICE);//建立電話狀態監聽器PhoneStateListener pStateListener=new PhoneStateListener(){@Overridepublic void onCallStateChanged(int state, String incomingNumber) {// TODO Auto-generated method stubswitch (state) {case TelephonyManager.CALL_STATE_IDLE://空閑狀態不做處理break;case TelephonyManager.CALL_STATE_OFFHOOK://接起電話不做處理break;case TelephonyManager.CALL_STATE_RINGING://正在響鈴//如果來電號碼在黑名單中則自動掛斷if (isBlock(incomingNumber)) {try {//擷取android.os.ServiceManager類的對象的getService()方法Method method=Class.forName("android.os.ServiceManager").getMethod("getService",String.class);// 擷取遠程TELEPHONY_SERVICE的IBinder對象的代理IBinder binder =(IBinder)method.invoke(null, new Object[] {TELEPHONY_SERVICE});// 將IBinder對象的代理轉換為ITelephony對象ITelephony telephony=ITelephony.Stub.asInterface(binder);//掛斷電話telephony.endCall();   } catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}break;}super.onCallStateChanged(state, incomingNumber);}};//為TelephonyManager添加監聽器tManager.listen(pStateListener, PhoneStateListener.LISTEN_CALL_STATE);btnManage.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stub//查詢通訊錄中的電話號碼final Cursor cursor=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);//建立一個BaseAdapter作為ListView的適配器BaseAdapter adapter=new BaseAdapter() {@Overridepublic View getView(int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stub//將遊標的指標移動到指定位置cursor.moveToPosition(position);CheckBox box=new CheckBox(CallGuard.this);//擷取指定位置的電話號碼String number=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));//去掉電話號碼中間的中劃線、空格number.replace("-", "").replace(" ", "");box.setText(number);//如果該號碼已經被加入黑名單、預設勾選該號碼if (isBlock(number)) {box.setChecked(true);}return box;}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn position;}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn position;}//返回列表的總條數@Overridepublic int getCount() {// TODO Auto-generated method stubreturn cursor.getCount();}};//載入line布局檔案View view=getLayoutInflater().inflate(R.layout.line, null);//擷取line布局檔案中Id為list的組件final ListView list=(ListView)view.findViewById(R.id.list);list.setAdapter(adapter);new AlertDialog.Builder(CallGuard.this).setView(view).setPositiveButton("確定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubblockList.clear();for (int i = 0; i < list.getCount(); i++) {CheckBox box=(CheckBox)list.getChildAt(i);//如果連絡人被選中則將其添加到blockList集合if (box.isChecked()) {blockList.add(box.getText().toString());}}}}).show();}});}private boolean isBlock(String number) {// TODO Auto-generated method stub//判斷號碼是否在blockList集合中for (String s:blockList) {if (s.equals(number)) {return true;}}return false;}}

最後別忘添加相應的許可權:

AndroidManifest.xml

<!-- 授予該應用控制通話的許可權 --><uses-permission android:name="android.permission.CALL_PHONE" /><!-- 授予該應用讀取通話狀態的許可權 --><uses-permission android:name="android.permission.READ_PHONE_STATE" /><!-- 授予讀連絡人ContentProvider的許可權 --><uses-permission android:name="android.permission.READ_CONTACTS" /><!-- 授予寫連絡人ContentProvider的許可權 --><uses-permission android:name="android.permission.WRITE_CONTACTS" />




android手機黑名單裡面沒有人 但是別人給打電話的時會自動掛斷 快的追加分數

是不是裝了360手機衛士或者QQ手機管家之類的軟體,如果是可以進入設定一下就行了。
 
安卓23系統,尋一款來電自動掛斷並選擇是否回撥的軟體

來電寶,到安卓市場下載吧,安裝後右上方會增加一個表徵圖,按鍵後可回撥,可設定回撥間隔時間,也可加入掛斷後自動發簡訊功能,很好用喲!雖然沒你說的那麼一樣,但你的想法他能基本做到。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.