2
我想在有人打電話給我時顯示一些信息。 我有服務。此服務已註冊廣播接收方。此接收器偵聽android.telephony.TelephonyManager.ACTION_PHONE_STATE_CHANGED。 我可以在我的手機解鎖時顯示烤麪包,但是當我鎖定手機並有人向我打電話時,烤麪包沒有顯示。 有人打電話給我時,顯示某些信息的最佳方式是什麼?來電屏幕上的浮動窗口
更新: 我創建了浮動窗口,當手機響起時我會打開。 這項工作非常好,但當電話被鎖定並且某人正在打電話給我時,窗口不顯示。當我接聽電話時,浮動窗口就在那裏。這裏有一些方法可以顯示鎖屏來電屏幕上的這個啓動窗口嗎? 這是我打開浮動窗口的方式:
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
startService(new Intent(context, FloatingWindow.class));
}
}, 2000);
,這是我的蓮花窗口
public class FloatingWindow extends Service{
private WindowManager wm;
private LinearLayout ll;
private Button btnStop;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
wm = (WindowManager) getSystemService(WINDOW_SERVICE);
ll = new LinearLayout(this);
btnStop = new Button(this);
ViewGroup.LayoutParams btnParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
btnStop.setText("Stop");
btnStop.setLayoutParams(btnParameters);
LinearLayout.LayoutParams llParameters = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
ll.setBackgroundColor(Color.argb(66, 255, 0, 0));
ll.setLayoutParams(llParameters);
final WindowManager.LayoutParams parameters = new WindowManager.LayoutParams(400, 150, WindowManager.LayoutParams.TYPE_PHONE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);
parameters.x = 0;
parameters.y = 0;
parameters.gravity = Gravity.CENTER | Gravity.CENTER;
ll.addView(btnStop);
wm.addView(ll, parameters);
ll.setOnTouchListener(new View.OnTouchListener() {
private WindowManager.LayoutParams updatedParameters = parameters;
int x, y;
float touchedX, touchedY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
x = updatedParameters.x;
y = updatedParameters.y;
touchedX = event.getRawX();
touchedY = event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
updatedParameters.x = (int) (x + (event.getRawX() - touchedX));
updatedParameters.y = (int) (y + (event.getRawY() - touchedY));
wm.updateViewLayout(ll, updatedParameters);
break;
default:
break;
}
return false;
}
});
btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
wm.removeView(ll);
stopSelf();
}
});
}
}