Friday, February 22, 2019

Send activity to outside lock screen.

We want to send a view to outside lock screen, and when user swipe to login, it disappear. If there no lock screen, that view not show.
We use Broadcast to listen user ‘s actions.
Create a service, declare in to file Manifest.xml.
  <service
            android:name=".myservice"
            android:enabled="true"
            android:exported="false" />
Declare in top of service.
private WindowManager wm;
WindowManager.LayoutParams myParams;
TextView mView;
int count=0;
static IntentFilter s_intentFilter = new IntentFilter();
     static {
     s_intentFilter.addAction(Intent.ACTION_SCREEN_ON);
     s_intentFilter.addAction(Intent.ACTION_USER_PRESENT);
     }

Copy in to onCreate().
registerReceiver(ti, s_intentFilter);
mView = new TextView(this);
          mView.setText(" This is example text! ");
          mView.setTextColor(Color.parseColor("#ffffff"));
          mView.setGravity(Gravity.CENTER);
          mView.setTextSize((float) 13.7);
          mView.setTypeface(null, Typeface.BOLD);
          wm = (WindowManager) getSystemService(WINDOW_SERVICE);
           myParams = new WindowManager.LayoutParams(
                      LayoutParams.WRAP_CONTENT,
                 LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
                 LayoutParams.TYPE_SYSTEM_ERROR,
                 WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                         | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                       
                 /* | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON */,
                 PixelFormat.RGBA_8888);
          myParams.gravity = Gravity.TOP | Gravity.CENTER_VERTICAL;
          myParams.x = 0;
          myParams.y = 50;
          myParams.setTitle("Load Average");

Copy function ti to outside onCreate().

private final BroadcastReceiver ti = new BroadcastReceiver() {
      @Override
      public void onReceive(Context context, Intent intent) {
      final String action = intent.getAction();
      KeyguardManager myKM = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
      if(action.equals(Intent.ACTION_USER_PRESENT)  ){
           if (count==1){
                wm.removeView(mView);
                count=0;
           } 
      }
      if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
           if( myKM.inKeyguardRestrictedInputMode())
         {
                if ( count==0){
           wm.addView(mView, myParams);
           count=1;
                }
         }
      }
      }
      };
Add permission in to file Manifest.
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
Add this to destroy.
@Override
     protected void onDestroy() {
          unregisterReceiver(ti);
          super.onDestroy();
     }
In Main class to open service, use this.
Intent intent = new Intent(this, myservice.class);
          intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
          startService(intent);


Run to see text show on lock screen, swipe in make it disappear.

No comments:

Post a Comment