最近刚接到一个需求,为BOSS做一个来电显示功能,查找号码库显示姓名角色。
一、查找来电监听方法
PhoneStateListener监听器类,用于监视设备上特定电话状态的变化,包括服务状态、信号强度、消息等待指示器(语音邮件)等。
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
public class MyPhoneStateListener extends PhoneStateListener {
private static final String TAG = "MyPhoneStateListener";
protected CallListener listener;
/**
* 返回电话状态
*
* CALL_STATE_IDLE 无任何状态时
* CALL_STATE_OFFHOOK 接起电话时
* CALL_STATE_RINGING 电话响铃时
*/
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
Log.d(TAG ,"电话挂断...");
listener.onCallIdle();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d(TAG ,"正在通话...");
listener.onCallOffHook();
break;
case TelephonyManager.CALL_STATE_RINGING:
Log.d(TAG ,"电话响铃...");
listener.onCallRinging();
break;
}
super.onCallStateChanged(state, incomingNumber);
}
//回调
public void setCallListener(CallListener callListener) {
this.listener = callListener;
}
//回调接口
public interface CallListener {
void onCallIdle();
void onCallOffHook();
void onCallRinging();
}
}
TelephonyManager 提供对设备上电话服务的信息的访问。应用程序可以使用该类中的方法来确定电话服务和状态,以及访问某些类型的订阅者信息。应用程序还可以注册侦听器来接收电话状态更改的通知。
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import com.flymbp.callmonitor.MyPhoneStateListener;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
telephony();
}
private void telephony() {
//获得相应的系统服务
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
if(tm != null) {
try {
MyPhoneStateListener myPhoneStateListener = new MyPhoneStateListener();
myPhoneStateListener.setCallListener(new MyPhoneStateListener.CallListener() {
@Override
public void onCallIdle() {
}
@Override
public void onCallOffHook() {
}
@Override
public void onCallRinging() {
//走接口查询号码信息
}
});
// 注册来电监听
tm.listen(myPhoneStateListener, MyPhoneStateListener.LISTEN_CALL_STATE);
} catch(Exception e) {
// 异常捕捉
}
}
}
}
此时此刻我们就可以监听到来电状态,但是incomingNumber没值,测试设备是华为mate20 pro Android 9.0
需要READ_CALL_LOG权限
<!--读取电话的状态信息的权限-->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<!--读取通话记录的权限-->
<uses-permission android:name="android.permission.READ_CALL_LOG" />
拿到incomingNumber 我们就可以请求后台接口来获取号码信息,或者有本地号码数据库进行查找。
二、来电弹窗提示信息
来电号码信息有了,我们要在来电界面进行提示,既然不能对来电界面进行篡改,那我们就加个弹窗提示吧。
想到两种方式:
1、Toast提示,实现简单,但是显示时间短,不是主动触发,会错过看到提示,不采用。
2、悬浮窗提示,既然要在自身应用以外的界面上显示弹窗,那必然要使用悬浮窗。
我们将使用悬浮窗进行来电提示。为了让悬浮窗与Activity脱离,使其在应用处于后台时悬浮窗仍然可以正常运行,这里使用Service来启动悬浮窗。
来电时显示悬浮窗,点击悬浮窗可移除,拖拽悬浮窗可移动,接通或挂断移除悬浮窗,注意悬浮窗不要来一个电话显示一个弹窗。
public class FloatingButtonService extends Service {
public static boolean isStarted = false;
private WindowManager windowManager;
private WindowManager.LayoutParams layoutParams;
private Button button;
private String content;
@Override
public void onCreate() {
super.onCreate();
isStarted = true;
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
layoutParams = new WindowManager.LayoutParams();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else {
layoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
}
layoutParams.format = PixelFormat.RGBA_8888;
layoutParams.gravity = Gravity.LEFT | Gravity.TOP;
layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
layoutParams.width = 500;
layoutParams.height = 100;
layoutParams.x = 300;
layoutParams.y = 300;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
content = intent.getStringExtra("content");
int state = intent.getIntExtra("state", 0);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
removeFloating();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
removeFloating();
break;
case TelephonyManager.CALL_STATE_RINGING:
showFloatingWindow();
break;
}
return super.onStartCommand(intent, flags, startId);
}
private void removeFloating() {
if(button != null){
windowManager.removeView(button);
}
}
private void showFloatingWindow() {
if(button != null){
windowManager.removeView(button);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (Settings.canDrawOverlays(this)) {
button = new Button(getApplicationContext());
button.setText(content);
button.setTextColor(Color.BLACK);
button.setBackgroundColor(Color.WHITE);
button.setOnTouchListener(new FloatingOnTouchListener());
windowManager.addView(button, layoutParams);
}
} else {
button = new Button(getApplicationContext());
button.setText(content);
button.setTextColor(Color.BLACK);
button.setBackgroundColor(Color.WHITE);
button.setOnTouchListener(new FloatingOnTouchListener());
windowManager.addView(button, layoutParams);
}
}
private class FloatingOnTouchListener implements View.OnTouchListener {
private int x;
private int y;
private int clickx;
private int clicky;
@Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
x = (int) event.getRawX();
y = (int) event.getRawY();
clickx = x;
clicky = y;
break;
case MotionEvent.ACTION_MOVE:
int nowX = (int) event.getRawX();
int nowY = (int) event.getRawY();
int movedX = nowX - x;
int movedY = nowY - y;
x = nowX;
y = nowY;
layoutParams.x = layoutParams.x + movedX;
layoutParams.y = layoutParams.y + movedY;
windowManager.updateViewLayout(view, layoutParams);
break;
case MotionEvent.ACTION_UP:
if (clickx == x && clicky == y)
windowManager.removeView(button);
break;
default:
break;
}
return false;
}
}
}
如何触发悬浮窗呢?
BroadcastReceiver使用广播来接收来电状态
在MainActivity.onCreate中注册广播
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String data = intent.getStringExtra("data");
showFloating(data);
}
};
IntentFilter intentFilter = new IntentFilter("android.intent.action.MAIN");
registerReceiver(mReceiver, intentFilter);
}
public void showFloating(String mobile, int state) {
Intent regIntent = new Intent(MainActivity.this, FloatingButtonService.class);
regIntent.putExtra("content", mobile);
regIntent.putExtra("state",state);
startService(regIntent);
}
三、后台监听
来电监听我们不能总让应用在前台运行吧,这时需要后台运行进行监听。
需要把在MainActivity.telephony的方法写到服务里。
public class MyPhoneStateListenService extends Service {
private static final String tag = "MyPhoneStateListenService";
public static final String ACTION_REGISTER_LISTENER = "action_register_listener";
// 电话管理者对象
private TelephonyManager mTelephonyManager;
// 电话状态监听者
private MyPhoneStateListener myPhoneStateListener;
@Override
public void onCreate() {
mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
myPhoneStateListener = new MyPhoneStateListener(this);
mTelephonyManager.listen(myPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
super.onCreate();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
// 取消来电的电话状态监听服务
if (mTelephonyManager != null && myPhoneStateListener != null) {
mTelephonyManager.listen(myPhoneStateListener, PhoneStateListener.LISTEN_NONE);
}
super.onDestroy();
}
}
在MainActivity.onCreate中开启服务
private void registerPhoneStateListener() {
Intent intent = new Intent(this, MyPhoneStateListenService.class);
intent.setAction(MyPhoneStateListenService.ACTION_REGISTER_LISTENER);
startService(intent);
}
四、进程保活
那么问题又来了,在后台服务很容易被杀,那我们就得考虑加入保活方案。
保活方案有很多,采用合适的方案,这里就不细说了。
常见的一些保活方案:
1、一像素保活
2、双进程守护
3、后台播放无声音乐
。。。