Android 對電話進行監聽和掛斷


1.添加權限

<!--撥打電話的權限-->
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

<!--電話攔截-->
<receiver android:name=".receiver.PhoneBroadcastReceiver">
<intent-filter android:priority="1000">
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
<!-- 電話攔截服務 -->

<service android:name="com.lvshandian.menshen.service.PhoneService">
<intent-filter>
<action android:name="com.xinwang.telesms.PhoneReciever"></action>
<action android:name="com.xinwang.telesms.server.IMICHAT" />
</intent-filter>
</service>


package com.lvshandian.menshen.receiver;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.telephony.TelephonyManager;
import android.util.Log;


import com.android.internal.telephony.ITelephony;
import com.lvshandian.menshen.service.PhoneService;

import java.lang.reflect.Method;
import java.util.ArrayList;

/**
* Created by zhang on 2016/11/3.
* 創建電話的監聽
*/

public class PhoneBroadcastReceiver extends BroadcastReceiver {


String TAG = "tag";
TelephonyManager telMgr;

@Override
public void onReceive(Context context, Intent intent) {


telMgr = (TelephonyManager) context.getSystemService(Service.TELEPHONY_SERVICE);
switch (telMgr.getCallState()) {
//來電
case TelephonyManager.CALL_STATE_RINGING:
String number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.v(TAG, "number:" + number);
Intent myintent = new Intent(context, PhoneService.class);
myintent.setAction("com.lvshandian.menshen.service.PhoneReciever");
context.startService(myintent);
// if (!getPhoneNum(context).contains(number)) {
// SharedPreferences phonenumSP = context.getSharedPreferences("in_phone_num", Context.MODE_PRIVATE);
// SharedPreferences.Editor editor = phonenumSP.edit();
// editor.putString(number, number);
// editor.commit();
// endCall();
// }
break;
//響鈴
case TelephonyManager.CALL_STATE_OFFHOOK:
break;
//掛斷
case TelephonyManager.CALL_STATE_IDLE:
break;
}

}

/**
* 掛斷電話
*/
private void endCall() {
Class<TelephonyManager> c = TelephonyManager.class;
try {
Method getITelephonyMethod = c.getDeclaredMethod("getITelephony", (Class[]) null);
getITelephonyMethod.setAccessible(true);
ITelephony iTelephony = null;
Log.e(TAG, "End call.");
iTelephony = (ITelephony) getITelephonyMethod.invoke(telMgr, (Object[]) null);
iTelephony.endCall();
} catch (Exception e) {
Log.e(TAG, "Fail to answer ring call.", e);
}
}

private ArrayList<String> getPhoneNum(Context context) {
ArrayList<String> numList = new ArrayList<String>();
//得到ContentResolver對象
ContentResolver cr = context.getContentResolver();
//取得電話本中開始一項的光標
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (cursor.moveToNext()) {
// 取得聯系人ID
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
// 取得電話號碼(可能存在多個號碼)
while (phone.moveToNext()) {
String strPhoneNumber = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
numList.add(strPhoneNumber);
Log.v("tag", "strPhoneNumber:" + strPhoneNumber);
}

phone.close();
}
cursor.close();
return numList;
}


}

//進行過濾對比電話

package com.lvshandian.menshen.service;

import java.lang.reflect.Method;
import java.util.List;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

import com.android.internal.telephony.ITelephony;
import com.lvshandian.menshen.bean.PhoneBean;
import com.lvshandian.menshen.receiver.PhoneBroadcastReceiver;
import com.lvshandian.menshen.utils.TextUtils;
import com.lvshandian.menshen.utils.XUtils;

public class PhoneService extends Service {
String TAG = "tag";
TelephonyManager telManager;

@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}

@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
telManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
telManager.listen(new MyPhoneStateListener(),
PhoneStateListener.LISTEN_CALL_STATE);
}

/**
* 掛斷電話
*/
private void endCall() {
Class<TelephonyManager> c = TelephonyManager.class;
try {
Method getITelephonyMethod = c.getDeclaredMethod("getITelephony", (Class[]) null);
getITelephonyMethod.setAccessible(true);
ITelephony iTelephony = null;
iTelephony = (ITelephony) getITelephonyMethod.invoke(telManager, (Object[]) null);
iTelephony.endCall();
} catch (Exception e) {
}
}

private class MyPhoneStateListener extends PhoneStateListener {
String phoneNumber;

public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING: /* 接通 */
phoneNumber = incomingNumber;
List<PhoneBean> list = XUtils.findAll(PhoneBean.class);
for (int i = 0; i < list.size(); i++) {
if (TextUtils.isString(list.get(i).getDnseg(), phoneNumber)) {
endCall();
break;
}
}

}
super.onCallStateChanged(state, incomingNumber);
}

}


@Override
public void onDestroy() {
super.onDestroy();
Intent localIntent = new Intent();
localIntent.setClass(this, PhoneService.class); // 銷毀時重新啟動Service
this.startService(localIntent);
}

private int flags;

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY_COMPATIBILITY;
}

@Override
public void onStart(Intent intent, int startId) {
// 再次動態注冊廣播
IntentFilter localIntentFilter = new IntentFilter("android.intent.action.USER_PRESENT");
localIntentFilter.setPriority(Integer.MAX_VALUE);// 整形最大值
myReceiver searchReceiver = new myReceiver();
registerReceiver(searchReceiver, localIntentFilter);
super.onStart(intent, startId);
}

public class myReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context, PhoneBroadcastReceiver.class));
}
}
}



 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM