sistema - quitar notificacion sin servicio de datos android
Mostrar icono mientras la aplicación está en segundo plano. Androide (2)
Usa este código
floatingBubble.java
package com.likith.floatingbubble;
import com.likith.gesturelauncherPro.R;
import com.likith.gesturelauncherPro.gestureDrawer;
import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.widget.ImageView;
public class FloatingFaceBubbleService extends Service
{
private WindowManager windowManager;
private ImageView floatingFaceBubble;
@SuppressLint("RtlHardcoded")
public void onCreate()
{
super.onCreate();
floatingFaceBubble = new ImageView(this);
floatingFaceBubble.setImageResource(R.drawable.ic_launcher);
windowManager = (WindowManager)getSystemService(WINDOW_SERVICE);
final LayoutParams myParams = new WindowManager.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT,
LayoutParams.TYPE_PHONE,
LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
myParams.gravity = Gravity.TOP | Gravity.LEFT;
myParams.x=this.getResources().getDisplayMetrics().widthPixels;
myParams.y=0;
windowManager.addView(floatingFaceBubble, myParams);
try
{
floatingFaceBubble.setOnTouchListener(new View.OnTouchListener()
{
@SuppressWarnings("unused")
WindowManager.LayoutParams paramsT = myParams;
private int initialX;
private int initialY;
private float initialTouchX;
private float initialTouchY;
private long touchStartTime = 0;
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouch(View v, MotionEvent event)
{
if(System.currentTimeMillis()-touchStartTime>ViewConfiguration.getLongPressTimeout() && initialTouchX== event.getX())
{
windowManager.removeView(floatingFaceBubble);
stopSelf();
return false;
}
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
touchStartTime = System.currentTimeMillis();
initialX = myParams.x;
initialY = myParams.y;
initialTouchX = event.getRawX();
initialTouchY = event.getRawY();
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_MOVE:
myParams.x = initialX + (int) (event.getRawX() - initialTouchX);
myParams.y = initialY + (int) (event.getRawY() - initialTouchY);
windowManager.updateViewLayout(v, myParams);
break;
}
return false;
}
});
floatingFaceBubble.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
//Toast.makeText(getApplicationContext(), "Clicked",Toast.LENGTH_LONG).show();
Intent myNewActivity = new Intent(getBaseContext(),gestureDrawer.class);
myNewActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(myNewActivity);
}
});
} catch (Exception e){
e.printStackTrace();
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
LittleIcon.java
package com.likith.floatingbubble;
import com.likith.gesturelauncherPro.R;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
public class LittleIcon extends View
{
private float viewX;
private float viewY;
private Paint mPaint;
private Bitmap androidIcon;
public LittleIcon(Context context)
{
super(context);
mPaint = new Paint();
mPaint.setColor(Color.BLACK);
androidIcon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
}
@Override
public void onDraw(Canvas cvs)
{
cvs.drawBitmap(androidIcon, viewX - androidIcon.getWidth() / 2, viewY - androidIcon.getHeight() / 2, mPaint);
}
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent event)
{
Toast.makeText(getContext(), "Clicked", Toast.LENGTH_LONG).show();
boolean touchedX = Math.abs(viewX - event.getX()) > androidIcon.getWidth();
boolean touchedY = Math.abs(viewY - event.getY()) > androidIcon.getHeight();
boolean isValidTouch = !touchedX && !touchedY;
if (isValidTouch)
{
if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE|| event.getAction() == MotionEvent.ACTION_UP)
{
viewX = event.getX();
viewY = event.getY();
}
invalidate();
return true;
}
else
return false;
}
}
permisos
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<service android:name="com.likith.floatingbubble.FloatingFaceBubbleService" />
Uso
Intent intent = new Intent(mainActivity.this, com.likith.floatingbubble.FloatingFaceBubbleService.class);
startService(intent);
Mi objetivo es crear una aplicación que, incluso si está en segundo plano. para ser más claro, te muestro algunos ejemplos de lo que obtendría. Es posible de hacer.
Puede agregar vista utilizando el método addView del administrador de ventanas, como la forma en que Facebook chathead se implementó. Consulte el enlace: what-apis-in-android-is-facebook-using-to-create-chat-heads