最近剛接到一個需求,爲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" />
Android 9 來電監聽incomingNumber爲空
拿到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、後臺播放無聲音樂
。。。