You can simply use a service class to show a chat head like button on Screen.
Global access to a service can be enforced when it is declared in its manifest's<service>
tag. By doing so, other applications will need to declare a corresponding
<uses-permission>
element in their own manifest to be able to start, stop, or bind to
the service.Provide this entry as a
<service>
element that's a child of the
<application>
element:
<application android:icon="@drawable/icon" android:label="@string/app_name"> ... <!-- Because android:exported is set to "false", the service is only available to this app. --> <service android:name=".ServiceMovableCameraBtn" android:exported="false"/> ... <application/>The attribute
android:name
specifies the class name of the service.
For example:
public class ServiceMovableCameraBtn extends Service implements View.OnTouchListener
{
private WindowManager windowManager;
boolean mHasDoubleClicked = false;
long lastPressTime;
WindowManager.LayoutParams container_params;
private int initialX;
private int initialY;
private float initialTouchX;
private float initialTouchY;
Context con;
android.support.design.widget.FloatingActionButton btnCapture;
private GestureDetector gestureDetector;
@Override
public IBinder onBind(Intent intent)
{
return null;
}
@Override
public void onCreate()
{
super.onCreate();
con = this;
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
setTheme(R.style.AppTheme);
gestureDetector = new GestureDetector(this, new SingleTapConfirm());
{
private WindowManager windowManager;
boolean mHasDoubleClicked = false;
long lastPressTime;
WindowManager.LayoutParams container_params;
private int initialX;
private int initialY;
private float initialTouchX;
private float initialTouchY;
Context con;
android.support.design.widget.FloatingActionButton btnCapture;
private GestureDetector gestureDetector;
@Override
public IBinder onBind(Intent intent)
{
return null;
}
@Override
public void onCreate()
{
super.onCreate();
con = this;
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
setTheme(R.style.AppTheme);
gestureDetector = new GestureDetector(this, new SingleTapConfirm());
// LAYOUT PARAMS
container_params = new WindowManager.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_PHONE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);
btnCapture = new android.support.design.widget.FloatingActionButton(this);
btnCapture.setImageResource(R.mipmap.ic_launcher);
btnCapture.setOnTouchListener(this);
windowManager.addView(btnCapture, container_params);
}
@Override
public void onDestroy()
{
super.onDestroy();
if (btnCapture != null)
{
windowManager.removeView(btnCapture);
}
}
Toast toast;
@Override
public boolean onTouch(View view, MotionEvent event)
{
WindowManager.LayoutParams paramsF = container_params;
if (gestureDetector.onTouchEvent(event))
{
// single tap
onSingleClick();
return true;
}
else
{
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
long pressTime = System.currentTimeMillis();
if (pressTime - lastPressTime <= 300)
{
generateNotification();
mHasDoubleClicked = true;
}
else
{
mHasDoubleClicked = false;
}
lastPressTime = pressTime;
initialX = paramsF.x;
initialY = paramsF.y;
initialTouchX = event.getRawX();
initialTouchY = event.getRawY();
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_MOVE:
paramsF.x = initialX + (int) (event.getRawX() - initialTouchX);
paramsF.y = initialY + (int) (event.getRawY() - initialTouchY);
windowManager.updateViewLayout(btnCapture, paramsF);
break;
}
}
return false;
}
public void showToast(final String text)
{
new Handler().post(new Runnable()
{
@Override
public void run()
{
if (toast != null)
{
toast.cancel();
}
toast = Toast.makeText(ServiceMovableCameraBtn.this, text, Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
});
}
private class SingleTapConfirm extends GestureDetector.SimpleOnGestureListener
{
@Override
public boolean onSingleTapConfirmed(MotionEvent event)
{
return true;
}
For example:
@Override
public boolean onSingleTapUp(MotionEvent e)
{
return true;
}
}
public void onSingleClick()
{
//perform some task..
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void generateNotification()
{
ServiceMovableCameraBtn.this.stopSelf();
Context context = ServiceMovableCameraBtn.this;
String message = "Tap to open SpyCam.";
Intent notificationIntent = new Intent(ServiceMovableCameraBtn.this, MainActivityG.class);
int icon = R.mipmap.ic_launcher;
try
{
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
String title = "Title";
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if (defaultSound == null)
{
defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
if (defaultSound == null)
{
defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
}
}
Notification.Builder builder = new Notification.Builder(context).setContentTitle(title)
container_params = new WindowManager.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_PHONE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);
btnCapture = new android.support.design.widget.FloatingActionButton(this);
btnCapture.setImageResource(R.mipmap.ic_launcher);
btnCapture.setOnTouchListener(this);
windowManager.addView(btnCapture, container_params);
}
@Override
public void onDestroy()
{
super.onDestroy();
if (btnCapture != null)
{
windowManager.removeView(btnCapture);
}
}
Toast toast;
@Override
public boolean onTouch(View view, MotionEvent event)
{
WindowManager.LayoutParams paramsF = container_params;
if (gestureDetector.onTouchEvent(event))
{
// single tap
onSingleClick();
return true;
}
else
{
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
long pressTime = System.currentTimeMillis();
if (pressTime - lastPressTime <= 300)
{
generateNotification();
mHasDoubleClicked = true;
}
else
{
mHasDoubleClicked = false;
}
lastPressTime = pressTime;
initialX = paramsF.x;
initialY = paramsF.y;
initialTouchX = event.getRawX();
initialTouchY = event.getRawY();
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_MOVE:
paramsF.x = initialX + (int) (event.getRawX() - initialTouchX);
paramsF.y = initialY + (int) (event.getRawY() - initialTouchY);
windowManager.updateViewLayout(btnCapture, paramsF);
break;
}
}
return false;
}
public void showToast(final String text)
{
new Handler().post(new Runnable()
{
@Override
public void run()
{
if (toast != null)
{
toast.cancel();
}
toast = Toast.makeText(ServiceMovableCameraBtn.this, text, Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
});
}
private class SingleTapConfirm extends GestureDetector.SimpleOnGestureListener
{
@Override
public boolean onSingleTapConfirmed(MotionEvent event)
{
return true;
}
For example:
@Override
public boolean onSingleTapUp(MotionEvent e)
{
return true;
}
}
public void onSingleClick()
{
//perform some task..
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void generateNotification()
{
ServiceMovableCameraBtn.this.stopSelf();
Context context = ServiceMovableCameraBtn.this;
String message = "Tap to open SpyCam.";
Intent notificationIntent = new Intent(ServiceMovableCameraBtn.this, MainActivityG.class);
int icon = R.mipmap.ic_launcher;
try
{
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
String title = "Title";
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if (defaultSound == null)
{
defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
if (defaultSound == null)
{
defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
}
}
Notification.Builder builder = new Notification.Builder(context).setContentTitle(title)
.setContentText(message)
.setContentIntent(intent)
.setSmallIcon(icon)
.setLights(Color.GREEN, 500, 600)
.setAutoCancel(true)
.setSound(defaultSound);
builder.setOngoing(true);
Notification not = new Notification.BigTextStyle(builder).bigText(message).build();
if (defaultSound == null)
{
not.defaults |= Notification.DEFAULT_SOUND;
}
notificationManager.notify(1, not);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
builder.setOngoing(true);
Notification not = new Notification.BigTextStyle(builder).bigText(message).build();
if (defaultSound == null)
{
not.defaults |= Notification.DEFAULT_SOUND;
}
notificationManager.notify(1, not);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
You can use this service class to display anything floating on the screen,like in this example we use FAB button,but you can use ImageView etc,.according to your requirement.
0 comments:
Post a Comment