confusedcoder
confusedcoder

Reputation: 13

How to use system_alert_window popup in java

I need to have my app draw over other apps, and I tried getting SYSTEM_ALERT_WINDOW permission shown below.

public class excessiveusageactivity extends AppCompatActivity {

    static final int REQUEST_OVERLAY_PERMISSION = 1; //request code
    TextView result;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        result = findViewById(R.id.result);
     if (requestCode == REQUEST_OVERLAY_PERMISSION) {
         if (Settings.canDrawOverlays(this)) {
             Toast permissionok = Toast.makeText(this, "permission granted!", Toast.LENGTH_SHORT);
             permissionok.show();
             //permission granted do stuff
             result.setText("yay");
         }else{
             Toast permissionnotgrantedtoast = Toast.makeText(this,"permission request failed, try again!", Toast.LENGTH_LONG);
             permissionnotgrantedtoast.show();
             //permission not granted
             result.setText("not granted");
         }
     }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_excessiveusageactivity);

        if(
                Settings.canDrawOverlays(this)
        ) {
            // ask for overlay permission
            Intent requestoverlaypermissionintent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                    Uri.parse("package:" + getPackageName()));
            startActivityForResult(requestoverlaypermissionintent, REQUEST_OVERLAY_PERMISSION);
            result.setText("here");
        }
    }
}

I got this code from here by user Jayman Jani.

I wish to make a custom system alert popup when the user uses an app for X minutes/hour consecutively, but I can't seem to find any information on how to set up this popup. I am quite new to all this so it would be great if someone can explain it mostly step-by-step. Thank you!

Upvotes: 0

Views: 297

Answers (0)

Related Questions