Here's a bit of code I've been playing with that may give you some
inspiration:
package com.example.floatingwindowtest;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.os.IBinder;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;
public class FloatingWindowService extends Service {
protected boolean foreground = false;
protected boolean cancelNotification = false;
private Notification notification;
private View myView;
protected int id = 0;
private WindowManager wm;
private WindowManager.LayoutParams params;
private int Xdown, Xstart;
private int Ydown, Ystart;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
// System.exit(0);
Toast.makeText(getBaseContext(),"onCreate",
Toast.LENGTH_SHORT).show();
params = new
WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
|
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH |
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);
params.gravity=Gravity.TOP|Gravity.LEFT;
wm = (WindowManager) getSystemService(WINDOW_SERVICE);
DisplayMetrics dm = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
final int size = (int) ((double)dm.heightPixels/2.5);
params.x = (dm.widthPixels-size)/2;
params.y = (dm.heightPixels-size)/2;
params.height = size;
params.width = size;
inflateview();
foregroundNotification(1);
//moveToForeground(1,n,true);
}
@Override
public void onDestroy() {
super.onDestroy();
((NotificationManager)
getSystemService(NOTIFICATION_SERVICE)).cancel(0);
Toast.makeText(getBaseContext(),"onDestroy",
Toast.LENGTH_SHORT).show();
if(myView != null)
{
((WindowManager)
getSystemService(WINDOW_SERVICE)).removeView(myView);
myView = null;
}
}
protected Notification foregroundNotification(int notificationId)
{
notification = new Notification(R.drawable.ic_launcher, "my
Notification", System.currentTimeMillis());
notification.flags = notification.flags |
Notification.FLAG_ONGOING_EVENT | Notification.FLAG_ONLY_ALERT_ONCE;
notification.setLatestEventInfo(this, "my Notification", "my
Notification", notificationIntent());
((NotificationManager)
getSystemService(NOTIFICATION_SERVICE)).notify(id,
notification);
return notification;
}
private PendingIntent notificationIntent() {
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pending = PendingIntent.getActivity(this, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
return pending;
}
public void inflateview()
{
LayoutInflater inflater = (LayoutInflater)
getSystemService(LAYOUT_INFLATER_SERVICE);
myView = inflater.inflate(R.layout.activity_popup, null);
myView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// Toast.makeText(getBaseContext(),"ACTION_DOWN",
Toast.LENGTH_SHORT).show();
Xdown=(int) event.getRawX();
Ydown=(int) event.getRawY();
Xstart= params.x;
Ystart= params.y;
break;
case MotionEvent.ACTION_UP:
// Toast.makeText(getBaseContext(),"ACTION_UP",
Toast.LENGTH_SHORT).show();
break;
case MotionEvent.ACTION_MOVE:
int x_cord = (int) event.getRawX();
int y_cord = (int) event.getRawY();
int x_movement=x_cord-Xdown;
int y_movement=y_cord-Ydown;
params.x=Xstart+x_movement;
params.y=Ystart+y_movement;
myView.setLayoutParams(params);
FloatingWindowService.this.wm.updateViewLayout(myView, params);
break;
default:
break;
}
return true;
}
});
final Button OKButton = (Button)
myView.findViewById(R.id.OK_Button);
OKButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
FloatingWindowService.this.stopSelf();
}
});
// Drawable MyBG =
Drawable.createFromPath("content://media/external/images/media/100741");
// myView.setBackground(MyBG);
// Add layout to window manager
wm.addView(myView, params);
}
}
You need the permission <uses-permission
android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
Just add the appropriate layout file and if desired make it more like a
toast by adding a timer to automatically dismiss it after so many seconds.
On Fri, Jan 17, 2014 at 9:44 PM, Glenn Powell <[email protected]> wrote:
> We are building a plugin library, which will be integrated into
> third-party applications. It requires the ability to show Toast-like
> notifications, which when tapped, will open our fullscreen widget Dialog.
>
> From my research it appears that Toasts were designed to explicitly ignore
> any touch input. The next best option seems to be one of either a
> PopupWindow, a Fragment, another Dialog with a transparent background, or
> about 25 different solutions. We don't want to trap touches on the
> entire screen, only over our notification popup.
>
> I am new to Android, so I'm curious to learn what the optimal solution
> would be. I have seen other plugin integrations do similar things, so I
> think there is probably an obvious way to tackle this. If you've
> implemented something like this, can you please give us some pointers?
> Thanks.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Android Developers" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/groups/opt_out.
>
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
---
You received this message because you are subscribed to the Google Groups
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.