show text over lock screen - android

How can I show TextView and EditView over the lock screen?
I've tried this and I also add TextView in the layout but nothing has happened
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lock);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
}
then I tried this (in Service) and it didn't work too
#Override
public void onCreate() {
WindowManager.LayoutParams mLayoutParams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_FULLSCREEN
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| PixelFormat.TRANSLUCENT);
TextView textView = new TextView(this);
textView.setId('1');
textView.setText("This is a demo");
textView.setTextSize(15);
textView.setGravity(Gravity.CENTER);
WindowManager mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
mWindowManager.addView(textView, mLayoutParams);
}
Help me please

you need to do it like this:
wm = (WindowManager) getSystemService(WINDOW_SERVICE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
80, 80, WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED|
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
PixelFormat.TRANSLUCENT
);
params.gravity = Gravity.CENTER;
params.setTitle("text params");
wm.addView(YourTextView, params);

Related

Fullscreen service in Android

I have made a service that puts black rectangle on top of every activity and the screen goes black except status/notification bar and navigation bar. My goal is to hide everything. How can I make this black rectangle go fullscreen? I have tried using FLAG_FULLSCREEN and FLAG_LAYOUT_IN_SCREEN, but it didn't work. I have read about disabling the TypeGuard, but even that didn't work. Thanks in advance!
Here is my code
WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
ImageView black = new ImageView(this);
black.setImageResource(R.drawable.black);
LinearLayout layout = new LinearLayout(this);
layout.addView(black);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 0;
params.y = 0;
windowManager.addView(layout, params);
i solved this issue just simply adding one line
layout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
I solved this program
if (null == mInflater) {
mInflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}if (mLockscreenView == null) mLockscreenView = mInflater.inflate(R.layout.view_locokscreen, null);
mLockscreenView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
mWindowManager.addView(mLockscreenView, mParams);
final Handler handler = new Handler();
mLockscreenView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
#Override
public void onSystemUiVisibilityChange(int visibility) {
handler.postDelayed(new Runnable() {
#Override
public void run() {
mLockscreenView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
}
},2000);
}
});
Try this:
assert getSupportActionBar() != null;
getSupportActionBar().hide();
mContentView = findViewById(R.id.fullscreen_content);
mContentView.setSystemUiVisibility(View.GONE | View.SYSTEM_UI_FLAG_FULLSCREEN);
Where fullscreen_content is the parent layout.

Selection text in WebView which created by service

How can longclick to select a text in WebView in a Service. I added a WebView by the WindowManager, when a url is loaded and a longclick to select text is done. The selected text is not visible and can not be selected.
How can I fix it ?
public class BtService extends Service {
#Override
public IBinder onBind(Intent intent) {
return null;
}
public static final int WINDOW_EXPAND_FLAGS = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
| WindowManager.LayoutParams.FLAG_DIM_BEHIND
| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
| WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
#Override
public void onCreate() {
super.onCreate();
WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, WINDOW_EXPAND_FLAGS, PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 0;
params.y = 0;
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.height = WindowManager.LayoutParams.MATCH_PARENT;
LinearLayout view = new LinearLayout(this);
view.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
WebView wv = new WebView(this);
wv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
view.addView(wv);
wv.loadUrl("https://tinhte.vn");
windowManager.addView(view, params);
}}
Also this will require the android.permission.SYSTEM_ALERT_WINDOW permission.
When webview loaded, longclick in text to select text,-> action mode don't show

could not get my window manager screen above locked screen in android (without unlocking the device))

here is the service that i have called to display view above locked screen.i dont want to unlock lock screen i want something that whatsapp and skype use for their calling screen.such that screen is not unlocked.
public class Display extends Service {
private static WindowManager wm;
private static RelativeLayout ly;
private WindowManager.LayoutParams params;
#Override
public void onCreate() {/
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
LayoutInflater vi;
vi = LayoutInflater.from(this);
ly = (RelativeLayout) vi.inflate(R.layout.display, null);
wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT |
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
/*ALSO TRIED USING WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON*/
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.RGBA_8888);
params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 0;
params.y = 300;
ly.setLayoutParams(params);
TextView tv = (TextView) ly.findViewById(R.id.annotationTextView);
tv.setText("check");
wm.addView(ly, params);
return START_NOT_STICKY;
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onDestroy() {
}
}
but the screen was appearing below locked screen.
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.WRAP_CONTENT,
// WindowManager.LayoutParams.TYPE_SYSTEM_ALERT |
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED,
PixelFormat.RGBA_8888);
worked for me but i was not able to touch on it so still unsuccessful

How to remove WindowManager view from screen?

I used
((WindowManager) context.getSystemService(Service.WINDOW_SERVICE)).removeView(view);
but it didn't work..view is still showing on the screen.
so how to remove WindowManager view from screen?
i Created it like this
public void create()
{
windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
layoutParams = new WindowManager.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT |
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSPARENT);
int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,10, context.getResources().getDisplayMetrics());
layoutParams.x = 265;
layoutParams.y = height;
layoutParams.format = PixelFormat.TRANSLUCENT;
LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.ad_dialog, null);
Button bttnOk = (Button) view.findViewById(R.id.bttn_ok);
Button bttnCancel =(Button) view.findViewById(R.id.bttn_cancel);
bttnOk.setOnClickListener(this);
bttnCancel.setOnClickListener(this);
windowManager.addView(view, layoutParams);
}
windowManager.removeViewImmediate(view);
Make sure you added that view before or it will crash your app

onItemClickListener not working in TYPE_SYSTEM_ALERT view

I use Windows Manager to show huge listview. Here is code, that I use:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LayoutInflater ltInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View view = ltInflater.inflate(R.layout.my_layout, null, false);
ListView lv = (ListView) view.findViewById(R.id.ext_fill_list);
mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
| WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.RIGHT | Gravity.TOP;
mWindowManager.addView(view, params);
ArrayList<String>mItems = new ArrayList<String>(Arrays.asList(ITEMS));
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, mItems);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d("ACC", "item clicked");
}
});
}
All works fine, but OnItemClickListener not work. How to solve this problem?
I found solution. Change LayoutParams like this:
mLoginPasswdDialog = inflater.inflate(R.layout.dialog_external_fill_by_copy, null);
WindowManager.LayoutParams lpd_params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT, 10, 10,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FORMAT_CHANGED | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
PixelFormat.RGBA_8888);
lpd_params.gravity = Gravity.CENTER_HORIZONTAL | Gravity.RIGHT;
mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
mWindowManager.addView(mLoginPasswdDialog, lpd_params);

Categories

Resources