I am attempting to familiarize myself with the android Service, for the purpose of overlaying the screen and drawing to it/interacting with the user outside of the app.
I know services aren't meant to interact with the user, but I can't find another way of accomplishing this.
I have found some resources that were quite a bit of help here on Stack Overflow. Here is another project on GitHub that is an example of something similar, but done in a different way.
As of right now, just because I am learning and want to accomplish this specific useless thing, I want there to be a Toast every time the screen is touched.
From reading and looking, my understanding is I need to start an Intent of a service. I must use the service to add a ViewGroup to the WindowManager. The handling of the touches and what is drawn to the screen is handled on the ViewGroup.
Yes, I have the service and overlay permission properly placed in the AndroidManifest.xml
Here are my 3 classes
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startService(new Intent(this, MyService.class));
finish();
}
}
public class MyService extends Service
{
#Override
public IBinder onBind(Intent p1)
{
// TODO: Implement this method
return null;
}
private MyView view;
#Override
public void onCreate()
{
// TODO: Implement this method
super.onCreate();
view = new MyView(this);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.RIGHT | Gravity.TOP;
params.setTitle("Load Average");
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(view, params);
}
}
public class MyView extends ViewGroup
{
public MyView(Context context){
super(context);
setLayoutParams(new LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
#Override
protected void onLayout(boolean p1, int p2, int p3, int p4, int p5)
{
// TODO: Implement this method
}
#Override
public boolean onTouchEvent(MotionEvent event)
{
// TODO: Implement this method
Toast.makeText(getContext(), "Touched", Toast.LENGTH_SHORT).show();
return true;
}
}
This code compiles and runs with no errors, and while messing with it, I CAN draw things to the screen in the draw method, but the toast in onTouchEvent never appears on the screen. Why won't it show up?
Edit: Using setOnTouchListener(...): and creating the toast in there didn't work either.
Your service creation and drawing is OK, as is your toast code. Throw a breakpoint on the Toast.makeText() and see if it is even getting called. My suspicion is that it is not.
Try creating a OnTouchListener and binding it to the ViewGroup, rather than using OnTouchEvent. This has worked for me in the past.
Edit: Corrected to OnTouchListner.
Related
I want to detect pressing of a back button in service. I've just tried this code but it didn't show me any log. Can somebody explain me why? And what should I do to make it work?
The whole idea of doing this was was taken from this tutorial http://www.kpbird.com/2013/03/android-detect-global-touch-event.html
public class MyService extends Service implements View.OnKeyListener{
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
LinearLayout touchLayout = new LinearLayout(this);
// set layout width 30 px and height is equal to full screen
LayoutParams lp = new LayoutParams(30, LayoutParams.MATCH_PARENT);
touchLayout.setLayoutParams(lp);
touchLayout.setBackgroundColor(Color.RED);
touchLayout.setOnKeyListener(this);
WindowManager mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
// set layout parameter of window manager
WindowManager.LayoutParams mParams = new WindowManager.LayoutParams(
30, // width of layout 30 px
WindowManager.LayoutParams.MATCH_PARENT, // height is equal to full screen
WindowManager.LayoutParams.TYPE_PHONE, // Type Phone, These are non-application windows providing user interaction with the phone (in particular incoming calls).
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, // this window won't ever get key input focus
PixelFormat.TRANSLUCENT);
mParams.gravity = Gravity.LEFT | Gravity.TOP;
mWindowManager.addView(touchLayout, mParams);
}
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK){
Log.v("Point","KeyCode_Back");
return false;
}
return false;
}
}
Your Service is not a View, implementing a View.OnKeyListener does not deliver your desired functionality.
A Service is intended to be an "Activity without UI" which runs in the background of your application. You can use Binders/Broadcasts to communicate with your service but UI interaction is best left to Activity/Fragments.
Annex:
I guess you are trying to build a overlay like in the link you posted in the comment. This Tutorial is from 2013 so things have changed.
In general the Android system discourages App beheaviour like the below described method. Coding like this, goes into the category Lockscreen/Kiosk-App behaviour which is considered as malware.
If you want to accomplish a little side menu inside your app you can do this perfectly fine without using such a service. Outside your App you still have the options of using widgets, which are more user friendly than hardcoding something on the screen.
Is there a way on android to keep the status bar while disabling all interaction you can do with it, like pulling it down?
I want to keep the information this bar gives, but I don't want users to interact with it.
This is the method that I like to use. You can unwrap it from the method and place it inside a base Activity instead. iirc, I got this from StackOverflow as well, but I didn't make a note of it so I'm not sure where the original post is.
What it basically does is place a transparent overlay over the top bar that intercepts all touch events. It's worked fine for me thus far, see how it works for you.
You MAY need to put this line in the AndroidManifest:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
I have it in my project, but I can't remember if it's because of this or something else. If you get a permission error, add that in.
WindowManager manager;
CustomViewGroup lockView;
public void lock(Activity activity) {
//lock top notification bar
manager = ((WindowManager) activity.getApplicationContext()
.getSystemService(Context.WINDOW_SERVICE));
WindowManager.LayoutParams topBlockParams = new WindowManager.LayoutParams();
topBlockParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
topBlockParams.gravity = Gravity.TOP;
topBlockParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|
// this is to enable the notification to recieve touch events
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
// Draws over status bar
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
topBlockParams.width = WindowManager.LayoutParams.MATCH_PARENT;
topBlockParams.height = (int) (50 * activity.getResources()
.getDisplayMetrics().scaledDensity);
topBlockParams.format = PixelFormat.TRANSPARENT;
lockView = new CustomViewGroup(activity);
manager.addView(lockView, topBlockParams);
}
and CustomViewGroup is
private class CustomViewGroup extends ViewGroup {
Context context;
public CustomViewGroup(Context context) {
super(context);
this.context = context;
}
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
Log.i("StatusBarBlocker", "intercepted by "+ this.toString());
return true;
}
}
Also! You also have to remove this view when your activity ends, because I think it will continue to block the screen even after you kill the application. Always, always ALWAYS call this onPause and onDestroy.
if (lockView!=null) {
if (lockView.isShown()) {
//unlock top
manager.removeView(lockView);
}
}
I'm currently developing an Android App, using a personal SurfaceView and double buffering. However I'm facing a little problem with my code.
In one hand I have an xml view, based on LinearLayout hierarchy. When I instantiate my activity, I set my contentView on this xml. The problem is then that my double buffering don't works anymore. Thread is running but nothing is displayed.
In the other hand, I set my contentView with a new personal SurfaveView element and display works fine. But of course, I cannot access anymore to the other elements of my LinearLayout.
Actually, I would like to set my contentView on my xml view AND keep my display working.
I hope I was clear enough, thank you for your answers!
Here is my activity:
public class MainController extends Activity {
#Override
protected void onCreate(final Bundle p_savedInstanceState) {
super.onCreate(p_savedInstanceState);
setContentView(R.layout.main_controller_activity);
this.drawableView = (MCustomDrawableView) findViewById(R.id.drawingBox);
...
}
...
}
My surface view:
public class MCustomDrawableView extends SurfaceView {
public MCustomDrawableView(Context p_context, AttributeSet p_attributes) {
super(p_context, p_attributes);
this.getHolder().addCallback(new SurfaceHolder.Callback() {
#Override
public void surfaceCreated(final SurfaceHolder p_holder) {
try {
// Instantiating a new thread
thread = new MBufferingThread(getInstance());
thread.start();
} catch (Exception exception) {
exception.printStackTrace();
}
}
#Override
public void surfaceChanged(final SurfaceHolder p_holder, int p_format, int p_width, int p_height) {...}
#Override
public void surfaceDestroyed(final SurfaceHolder p_holder) {...}
});
// Setup drawing options
setupDrawing();
}
...
}
And my xml view:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:background="#color/app_background"
tools:context=".MainController">
<com.iskn.calligraphy.models.draw.MCustomDrawableView
android:id="#+id/drawingBox"
style="#style/DrawingBox" />
<SeekBar
android:id="#+id/brushSize"
style="#style/SizeSeekBar" />
<SeekBar
android:id="#+id/brushOpacity"
style="#style/OpacitySeekBar" />
</LinearLayout>
EDIT:
After further researches and analyses, it appears clearly that:
setContentView(R.layout.main_controller_activity): in this case I get all the elements from my activity, but the MCustomDrawableView display nothing.
setContentView(new MCustomDrawableView(getApplicationContext())): on that case, MCustomDrawableView is working well (it displays what I want), but I don't have the others View from my main_controller_activity
In both cases:
my thread is running and works well.
my drawing function is called as well, with the holder.lockCanvas() and holder.unlockCanvasAndPost(bufferCanvas) methods.
Well, I found a functionnal solution :
I think the problem was coming from the MCustomDrawableView initialization . I created a new instance of that class from the onCreate method , and not from the xml file . Then, I set it to the contentView:
protected void onCreate(Bundle p_savedInstanceState) {
super.onCreate(p_savedInstanceState);
setContentView(R.layout.main_controller_activity);
// Instantiate drawable object & set style properties
this.drawableView = new MCustomDrawableView(getApplicationContext());
FrameLayout.LayoutParams style = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT);
// Add the drawable view to the main_activity
addContentView(this.drawableView, style);
}
But this way raises a new problem. The added view is on the top, which means that all the others View are hided. I solved this with bringToBack(this.drawableView) below method:
private void bringToBack(View p_view) {
// Get parent from the current view
ViewGroup viewGroup = ((ViewGroup) p_view.getParent());
int childrenCount = viewGroup.indexOfChild(p_view);
for(int cpt = 0; cpt < childrenCount; cpt++) {
// Move the child to the top
viewGroup.bringChildToFront(viewGroup.getChildAt(cpt));
}
}
It brings back the provided view to the background position. I also had to set the LinearLayout's alpha to 0.
I'm still open to other solutions!
I'm currently working on a small AR app for Android and am facing the problem of integrating Unity3d into an activity. The requirements indicate that I need to be able to present some Android UI - e.g. menus and an action bar - and a camera view that will display a model created in Unity3d when the target is detected.
I found a link that helped me a lot: Unity3d forums. One of the users there asked the same question I have now but never got any proper answer -that's why I'm posting here.
Problem:
I got a small Unity3d project that is essentially a white cube and am trying to display it in one of my Android activities. The model looks fine when activity doesn't have setContentView() in its onCreate() method but then I can't specify my layout in an XML file.
When I do add the setContentView() method, I can see the cube but it's very small and there doesn't seem to be any way of actually making it change its size.
The XML file:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/unityView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</FrameLayout>
1st version of the activity implementation:
public class HomeActivity extends UnityPlayerActivity {
UnityPlayer unityPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
And the resulting screenshot:
2nd version of the activity implementation:
public class HomeActivity extends UnityPlayerActivity {
UnityPlayer unityPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
unityPlayer = new UnityPlayer(this);
int glesMode = unityPlayer.getSettings().getInt("gles_mode", 1);
unityPlayer.init(glesMode, false);
FrameLayout layout = (FrameLayout) findViewById(R.id.unityView);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
layout.addView(unityPlayer.getView(), 0, lp);
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
unityPlayer.windowFocusChanged(hasFocus);
}
}
And the resulting screenshot:
Could anyone explain to me why that is and how to fix it?
While I still don't know why it works like that, I've found a way of fixing it.
Instead of simply using setContentView() in onCreate(), extend onResume() and in that method recursively look through all the available views to find the parent view of the UnityPlayer object. Once that's found, layouts and other views can be inflated and added to that parent view.
Here's the link with a code example - I've used this to make my app work: https://developer.vuforia.com/resources/dev-guide/extending-unity-android-activity-and-adding-custom-views-eclipse
Edit: Here's a code snippet showing my solution.
#Override
public void onResume() {
super.onResume();
if (unityPlayer == null) {
View rootView = findViewById(android.R.id.content);
unityPlayer = findUnityPlayerView(rootView);
if (unityPlayer != null) {
ViewGroup unityPlayerParentView = (ViewGroup)(unityPlayer.getParent());
View mainHomeView = getLayoutInflater().inflate(R.layout.activity_main, null);
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
unityPlayerParentView.addView(mainHomeView, layoutParams);
}
}
}
and
private UnityPlayer findUnityPlayerView(View view) {
if (view instanceof UnityPlayer) {
return (UnityPlayer) view;
}
if (view instanceof ViewGroup) {
ViewGroup childrenViews = (ViewGroup) view;
for (int i = 0; i < childrenViews.getChildCount(); i++) {
UnityPlayer foundView = findUnityPlayerView(childrenViews.getChildAt(i));
if (foundView != null) {
return foundView;
}
}
}
return null;
}
We start with an activity that starts a service then becomes idle until the service calls the activity again. the activity goes to onPause(i believe) and isn't started back up until the its BroadcastReceiver gets its call to start. My service creates a WindowManager as a system overlay and when the user triggers it, it is supposed to call the activity and make the activity make a new view with some stuff in it. Well everything goes through without a hitch, no errors popup and all my logs suggest that the code runs through smoothly, but the new view that is supposed to be made never comes to view. Interestingly enough though, if you click the app to start it again, up pops the view i wanted earlier. so I'm curious as to why it doesn't happen when i want it to, but instead it remains invisible. I've tried setting the view to visible, tried bringToFront(), and i even tried some flags on the intent to bring the activity to the top of the stack but none of those work. relevant code is below.
im not here to hear about why you don't think my app is good for the user or that you don't think it follows androids base "design" i just want help debugging this interesting bug.
MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(MainActivity.this, MyService.class);
startService(intent); //if i comment out these lines and add the call
initService(); //to figureOutParam below it works perfectly
sendUserHome(); //this one as well
figureOutParam("UPDATE",0); //this is the call that i use to get through to
//the method though right now neither param is important
}
public void figureOutParam(String param, int top){
if(param.equals("UPDATE")){
View myView = new View(this);
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
myView = inflater.inflate(R.layout.activity_main, null);
myView.setBackgroundColor(Color.BLUE);
myView.bringToFront();
setContentView(myView);
}
}
MyService (only important part involving the call back to service)
#Override
public void onCreate() {
super.onCreate();
WindowManager window = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
Display d = window.getDefaultDisplay();
int width=d.getWidth();
int height=d.getHeight();
height= (int)(height*.025);
params = new WindowManager.LayoutParams(WindowManager.LayoutParams.FILL_PARENT,height,
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.BOTTOM;
wm = (WindowManager) getSystemService(WINDOW_SERVICE);
}
public void updateView(String params){
Intent i = new Intent(MY_INTENT);
i.putExtra("y", this.y);
i.putExtra("param", params);
sendBroadcast(i);
}
Manifest because, well because
Try to inserting the figureOutParam("UPDATE",0); after the super.onCreate(savedInstanceState);.