I am developing an application on Android. My application in essence is a call log. What I want is for my application to display three buttons above all other activities when the device receives an incomming call.
What I have right now is a BroadcastReceiver which receives PHONE_STATE intents and when an incomming call is detected, an activity is started. The started activity contains the buttons I want to be displayed.
When the OverlayActivity appears, all activities underneath stop receiving touch events, that is I can touch only the OverlayActivity. How do I make them receive the events?
Right now inside my BroadCastReceiver I start the activity:
//PhonecallReceiver.java
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(myContext, OverlayActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("phone_number", number);
context.startActivity(i);
}
The actuall activity
//OverlayActivity.java:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.CENTER | Gravity.TOP;
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
LayoutInflater inflater = getLayoutInflater();
mLayout = (LinearLayout) inflater
.inflate(R.layout.overlay_layout, null);
wm.addView(mLayout, params);
}
@DJClayworh
Thanks for the tip :)
I managed to solve my problem by using the exam same code, but instead of an Activity, I used a Service. You just have to start the service to display the overlay. Here is an example:
public class OverlayService extends Service {
private WindowManager windowManager;
private View layout;
@Override
public void onCreate() {
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
// very important, this sends toush events to underlying views
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
// Layout gravity
params.gravity = Gravity.TOP | Gravity.CENTER;
params.x = 0; params.y = 100;
LayoutInflater li = (LayoutInflater) getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Your overlay layout
layout = li.inflate(R.layout.overlay, null;
// This displays overlay on the screen
windowManager.addView(layout, params);
}
@Override
public void onDestroy() {
super.onDestroy();
// Error will occur if this is missing.
if (layout != null)
windowManager.removeView(layout);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With