Android Forground Service for device Orientation and surfaceview

This service start in foreground with a view in windowmanager which change the orientation of the device and a surfaceview which can be used by camera for recording even when phone locked and screen off. Thanks to pallab.


public class SurfaceViewService extends Service {
    private WindowManager windowManager;
    final WindowManager.LayoutParams myParams = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);
    private static SurfaceView mSurfaceView = null;
    
    @Override
    public void onCreate() {
        super.onCreate();
  
        windowManager = (WindowManager) getApplicationContext().getSystemService(WINDOW_SERVICE);
        myParams.gravity = Gravity.TOP | Gravity.CENTER;
        myParams.height=1;
        myParams.width=1;
        myParams.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
        mSurfaceView = new SurfaceView(this);
        windowManager.addView(mSurfaceView, myParams);
        
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setContentTitle("gg")
                .setTicker("gg")
                .setContentText("gg is running")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.drawable.logo);
        
        Notification cur_notification = builder.build();
        
        startForeground(1, cur_notification);

        
    }
    
    public static SurfaceView getSurfaceView(){
        if(mSurfaceView==null) {
            mLogger.error("surfaceview service not started");
        }
        return mSurfaceView;
    }
    
    @Override
    public void onDestroy() {
        super.onDestroy();
        windowManager.removeView(mSurfaceView);
    }
    
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

}
Advertisements
Android Forground Service for device Orientation and surfaceview

One thought on “Android Forground Service for device Orientation and surfaceview

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s