Created
November 19, 2016 08:24
-
-
Save kevalpatel2106/ea66370ae2d1c53665f0fd0212b65367 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Intent; | |
import android.net.Uri; | |
import android.os.Build; | |
import android.os.Bundle; | |
import android.provider.Settings; | |
import android.support.v7.app.AppCompatActivity; | |
import android.view.View; | |
import android.widget.Toast; | |
public class MainActivity extends AppCompatActivity { | |
private static final int CODE_DRAW_OVER_OTHER_APP_PERMISSION = 2084; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
//Check if the application has draw over other apps permission or not? | |
//This permission is by default available for API<23. But for API > 23 | |
//you have to ask for the permission in runtime. | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(this)) { | |
//If the draw over permission is not available open the settings screen | |
//to grant the permission. | |
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, | |
Uri.parse("package:" + getPackageName())); | |
startActivityForResult(intent, CODE_DRAW_OVER_OTHER_APP_PERMISSION); | |
} else { | |
initializeView(); | |
} | |
} | |
/** | |
* Set and initialize the view elements. | |
*/ | |
private void initializeView() { | |
findViewById(R.id.notify_me).setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
startService(new Intent(MainActivity.this, ChatHeadService.class)); | |
finish(); | |
} | |
}); | |
} | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
if (requestCode == CODE_DRAW_OVER_OTHER_APP_PERMISSION) { | |
//Check if the permission is granted or not. | |
if (resultCode == RESULT_OK) { | |
initializeView(); | |
} else { //Permission is not available | |
Toast.makeText(this, | |
"Draw over other app permission not available. Closing the application", | |
Toast.LENGTH_SHORT).show(); | |
finish(); | |
} | |
} else { | |
super.onActivityResult(requestCode, resultCode, data); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment