Skip to content

Instantly share code, notes, and snippets.

@xanderblinov
Created December 22, 2015 09:46
Show Gist options
  • Save xanderblinov/583303c7b6942fe4b2c8 to your computer and use it in GitHub Desktop.
Save xanderblinov/583303c7b6942fe4b2c8 to your computer and use it in GitHub Desktop.
Home Screen overlay
package com.arellomobile.wagamamamvp;
import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Handler;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Toast;
/**
* Date: 22-Dec-15
* Time: 15:09
*
* @author Alexander Blinov
*/
public class LockScreenService extends Service
{
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
@Override
public void run()
{
WindowManager mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
View mView = ((LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE)).inflate(R.layout.act_test, null);
WindowManager.LayoutParams mLayoutParams = new WindowManager.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, 0, 0,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
/* | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON */,
PixelFormat.RGBA_8888);
mView.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Log.e("Window","touch");
Toast.makeText(LockScreenService.this, "touch", Toast.LENGTH_LONG).show(); // this stuff doesn't work for unknown reason
}
});
mWindowManager.addView(mView, mLayoutParams);
}
}, 5000);
return super.onStartCommand(intent, flags, startId);
}
@Nullable
@Override
public IBinder onBind(Intent intent)
{
return null;
}
}
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
//strat Service from any place (for example from onReceive)
startService(new Intent(AsyncResultActivity.this, LockScreenService.class));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment