1

I have implemented a lockscreen and in it i have a check button which opens a calculator like UI and when the user types his secret no this unlocks the complete lockscreen where calculator is a dialog over my lockscreen.

Service class that gives the calculator UI:

 public class caclservice extends Service {
private int[] numericButtons = {R.id.btnZero, R.id.btnOne, R.id.btnTwo, R.id.btnThree, R.id.btnFour, R.id.btnFive, R.id.btnSix, R.id.btnSeven, R.id.btnEight, R.id.btnNine};
private static final String TAG = PopupService.class.getSimpleName();
WindowManager mWindowManager;
QuestionAdapter qa;
View mView; String username;
TextView ques;
private Session session;
String question;
private TextView txtScreen;
// Represent whether the lastly pressed key is numeric or not
private boolean lastNumeric;
// Represent that current state is in error or not
private boolean stateError;
// If true, do not allow to add another DOT
private boolean lastDot;
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    session=new Session(this);
    qa=new QuestionAdapter(this);
    username=session.getusename();
    question=qa.fetchreco(username);
    showDialog();
    setNumericOnClickListener();
    return super.onStartCommand(intent, flags, startId);
}

private void showDialog() {
    PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock mWakeLock = pm.newWakeLock((PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "caclservice");
    mWakeLock.acquire();
    mWakeLock.release();

    mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    mView = View.inflate(getApplicationContext(), R.layout.calculator, null);
    mView.setTag(TAG);

    int top = getApplicationContext().getResources().getDisplayMetrics().heightPixels / 2;

    LinearLayout dialog = (LinearLayout) mView.findViewById(R.id.lin);
   // if you want to set params
    //        android.widget.LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams) dialog.getLayoutParams();
      //        lp.topMargin = top;
       //        lp.bottomMargin = top;
        //        mView.setLayoutParams(lp);
   txtScreen = (TextView) mView.findViewById(R.id.txtScreen);
    // ques.setText(question);

    // final EditText etMassage = (EditText) mView.findViewById(R.id.ans);
    //etMassage.setText("");
   /* ImageButton imageButtonSend = (ImageButton) mView.findViewById(R.id.imageButtonSendInPopupMessageReceived);
     //        lp = (LayoutParams) imageButton.getLayoutParams();
     //        lp.topMargin = top - 58;
    //        imageButton.setLayoutParams(lp);
    imageButtonSend.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

    //                mView.setVisibility(View.INVISIBLE);
            if(!etMassage.getText().toString().equals(""))
            {

                etMassage.setText("");
            }
        }
    });*/


    Button close = (Button) mView.findViewById(R.id.btnEqual);
    // close.setText("Cancel");
    close.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            String ans = txtScreen.getText().toString();
            if (ans.trim().equals(question.trim())) {
                hideDialog();
                android.os.Process.killProcess(android.os.Process.myPid());
                hideDialog();
               // int flags =  WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON;
               // getWindow().addFlags(flags);

            }
        }
    });


    final WindowManager.LayoutParams mLayoutParams = new WindowManager.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT, 0, 0,
            WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
            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.setVisibility(View.VISIBLE);
    mWindowManager.addView(mView, mLayoutParams);
    mWindowManager.updateViewLayout(mView, mLayoutParams);

}
private void setNumericOnClickListener() {
    // Create a common OnClickListener
    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Just append/set the text of clicked button
            Button button = (Button) v;
            if (stateError) {
                // If current state is Error, replace the error message
            txtScreen.setText(button.getText());
                stateError = false;
            } else {
                // If not, already there is a valid expression so append to it
              txtScreen.append(button.getText());
            }
            // Set the flag
            lastNumeric = true;
        }
    };
    // Assign the listener to all the numeric buttons
    for (int id : numericButtons) {
        mView.findViewById(id).setOnClickListener(listener);
    }
}
private void hideDialog(){
    if(mView != null && mWindowManager != null){
        mWindowManager.removeView(mView);
        mView = null;
    }
}

@Override
public void onDestroy() {
    super.onDestroy();
}
}

This class gives me the UI like this Calculator ui in which = button should unlock the device Here this completely works fine but the problem is after unlocking this calculator dialog again comes in front like this UI in home screen which i dont want. So how to avoid this UI from popping out again and again.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
vishwa
  • 27
  • 5
  • According to the answer [here](https://stackoverflow.com/questions/14182014/android-oncreate-or-onstartcommand-for-starting-service) , onStartCommand() is called every time a client starts the service. So i'd say have a look at your implementation for that command. – Ellisan Oct 20 '17 at 12:12
  • i gave showdialog(); in oncreate but the same result – vishwa Oct 21 '17 at 05:42
  • Then it appears your service gets called every time which is causing it to be recreated and thus showing the dialog. Can you post your implementation of how you initiate the service? – Ellisan Oct 21 '17 at 10:48

0 Answers0