问题
I want to get an application menu bar on the left hand side of the screen without disturbing the task that is already running for eg.a live wallpaper or video.How can it be achieved?
回答1:
Please use this service class. by starting this service from your activity you will get an image in your mobile screen. by adjusting that you can set any where in the mobile. and you can remove it by stopping the service any time.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.os.Environment;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewDebug;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class HUD extends Service {
HUDView mView;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
mView = new HUDView(this);
mView.setId(R.id.button);
WindowManager.LayoutParams paramsOrg = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(100, 100, 2007, 8, -3);
Button bb=new Button(this);
bb.setText("Button");
bb.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
System.out.println("Clicked----><<<<<<<");
}
});
bb.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
System.out.println("Touched =----- > ");
return false;
}
});
params.gravity = Gravity.RIGHT | Gravity.TOP;
params.setTitle("Load Average");
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(bb, params);
}
@Override
public void onDestroy() {
super.onDestroy();
//Toast.makeText(getBaseContext(),"onDestroy", Toast.LENGTH_LONG).show();
if(mView != null)
{
((WindowManager) getSystemService(WINDOW_SERVICE)).removeView(mView);
mView = null;
}
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
//Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
restartService();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
//Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
restartService();
}
}
private void restartService() {
// TODO Auto-generated method stub
stopService(new Intent(getBaseContext(), HUD.class));
startService(new Intent(getBaseContext(), HUD.class));
}
}
class HUDView extends ViewGroup {
private Paint mLoadPaint;
private Float XMAX=280f;
private Float YMAX=26f;
private int XPOS=0;
public HUDView(Context context) {
super(context);
mLoadPaint = new Paint();
mLoadPaint.setAntiAlias(true);
mLoadPaint.setTextSize(10);
mLoadPaint.setARGB(255, 255, 0, 0);
context.bindService(intents, aslServiceConnection, Context.BIND_AUTO_CREATE);
WindowManager wind = (WindowManager)context.getSystemService(Service.WINDOW_SERVICE);
XPOS=wind.getDefaultDisplay().getWidth();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//canvas.drawText("Hello World", 5, 15, mLoadPaint);
//canvas.drawText("Hello World", 100, 100, mLoadPaint);
Bitmap tileImage = BitmapFactory.decodeResource(getResources(), R.drawable.image);
canvas.drawBitmap(tileImage, XPOS-35, 0, null);
XMAX=Float.parseFloat(""+(XPOS-35));
}
@Override
protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Float x=event.getX();
Float y=event.getY();
System.out.println("Mview has been touched ---> "+x +" x "+y);
if(x > 0 && y > 0)
{
if(x > XMAX && y < YMAX){
TakeMyScreen(getContext());
}
}
return true;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
return super.onKeyDown(keyCode, event);
}
}
回答2:
You can use NewPopupMenu libarary from github.
It will create instant popup menu at any location on screen on any listner.
You just have to download library and extract it, copy the source file into project,
and modify code at this line mPopupWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
and place your menu where ever you wants.
回答3:
Must use the permission into your project manifest otherwise window has not been attach
android:name="android.permission.SYSTEM_ALERT_WINDOW"
来源:https://stackoverflow.com/questions/14229626/how-to-get-application-menu-over-a-currently-running-screen