2

I am right now doing a lockscreen for android i want display it over the default lock screen i trigered service from my activity and from service i called receiver that is called when SCREEN_ON SCREEN _OFF ,ACTION USER PRESENT . every thing is ok untill i press home button so to controll home button i googled and there were many solution i tried everything but nothing worked. How to disable virtual home button in any activity? In this found that i want to make my activity as window manager activity. so searched how to make it. I found this

  WindowManager mWindowManager;
   WindowManager windowManager = (WindowManager)getSystemService(WINDOW_SERVICE);
    //here is all the science of params
    final WindowManager.LayoutParams myParams = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY ,
            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                    | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                    | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
            PixelFormat.TRANSLUCENT
    );

   /* WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.TYPE_APPLICATION_PANEL,
            WindowManager.LayoutParams.FLAG_FULLSCREEN |
                    WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                    WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD,
            PixelFormat.TRANSLUCENT
    );*/
    myParams.gravity = Gravity.TOP;

    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    RelativeLayout mOverlay = (RelativeLayout) inflater.inflate(R.layout.activity_main, (ViewGroup) null);
    mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

    mWindowManager.addView(mOverlay, myParams);

when i added this in my onstartcommand of my service i fount this error

       Caused by: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@fa59ecd -- permission denied for window type 2006

when i googled this i found i need to check canoverlay permission at runtime so i check this in my mainactivity before i trigger myservice but even now i'm not able to controll my homebutton . As mine is a lockscreen it is must. i found many lockscreen apps in playstore that have done this. sO IF IT IS possible how to do it. my code: Activity

 public class MainActivity extends AppCompatActivity {
HomeKeyLocker homeKeyLocker ;
boolean    askedForOverlayPermission;
final private int REQUEST_CODE_ASK_PERMISSIONS = 123;
public final static int OVERLAY_PERMISSION_CODE = -1010101;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    if(Build.VERSION.SDK_INT >= 23) {
        if (!Settings.canDrawOverlays(this)) {
            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                    Uri.parse("package:" + getPackageName()));
            startActivityForResult(intent, 1234);
        }
    }
    else {
        Intent intent = new Intent(this, Myservice.class);
        startService(intent);
    }

}

public void req(View view)
{

        android.os.Process.killProcess(android.os.Process.myPid());
    }
protected void onPause() {
    super.onPause();
    Intent intent = new Intent(this,MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT |Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
} 

my Manifest:

 <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:launchMode="singleTask"
    android:clearTaskOnLaunch="true"
    android:stateNotNeeded="true"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity"

        android:launchMode="singleTask"
        android:excludeFromRecents="true"
        android:screenOrientation="nosensor">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.HOME" />
            <category android:name="android.intent.category.DEFAULT" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver android:name=".Receiverb">
        <intent-filter>
            <action android:name="android.intent.action.USER_PRESENT">
            </action>
            <action android:name="android.intent.action.SCREEN_OFF">
            </action>
            <action android:name="android.intent.action.BOOT_COMPLETED">
            </action>
        </intent-filter>
    </receiver>
    <service android:name=".Myservice"/>
</application>

My Receiver:

public class Receiverb extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    Log.d("receiver", "main");

    if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
        // do whatever you need to do here
        Log.d("receiver", "screen off");
        context.startActivity(new Intent(context,
                MainActivity.class)
                .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
    } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
        // and do whatever you need to do here
        Log.d("receiver", "screen on");
        context.startActivity(new Intent(context,
                MainActivity.class)
                .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
        Log.d("receiver", "aft activity");
    } else if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
        Log.d("receiver", "unlock");

        context.startActivity(new Intent(context,
                MainActivity.class)
                .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
    }
}
MinnuKaAnae
  • 1,646
  • 3
  • 23
  • 35
vishwa
  • 27
  • 5

0 Answers0