-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add permission check for Android 6.0
- Loading branch information
1 parent
b019787
commit 4c33c50
Showing
7 changed files
with
91 additions
and
24 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
70 changes: 70 additions & 0 deletions
70
app/src/main/java/com/martin/ads/testopencv/HomeActivity.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package com.martin.ads.testopencv; | ||
|
||
import android.Manifest; | ||
import android.content.Intent; | ||
import android.content.pm.PackageManager; | ||
import android.os.Build; | ||
import android.os.Bundle; | ||
import android.support.v4.app.ActivityCompat; | ||
import android.support.v4.content.ContextCompat; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.widget.Toast; | ||
|
||
|
||
public class HomeActivity extends AppCompatActivity { | ||
private static final int REQUEST_PERMISSION = 233; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
if(checkPermission(Manifest.permission.CAMERA,REQUEST_PERMISSION)) | ||
init(); | ||
} | ||
|
||
private void init(){ | ||
startActivity(new Intent(this,MainActivity.class)); | ||
finish(); | ||
} | ||
|
||
private boolean checkPermission(String permission,int requestCode){ | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | ||
if (ContextCompat.checkSelfPermission(this, permission) | ||
!= PackageManager.PERMISSION_GRANTED) { | ||
if (ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) { | ||
showHint("Camera and SDCard access is required, please grant the permission in settings."); | ||
finish(); | ||
} else { | ||
ActivityCompat.requestPermissions(this, | ||
new String[]{ | ||
permission, | ||
Manifest.permission.WRITE_EXTERNAL_STORAGE | ||
}, | ||
requestCode); | ||
} | ||
return false; | ||
}else return true; | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { | ||
switch (requestCode) { | ||
case REQUEST_PERMISSION: | ||
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { | ||
init(); | ||
}else { | ||
showHint("Camera and SDCard access is required, please grant the permission in settings."); | ||
finish(); | ||
} | ||
break; | ||
default: | ||
finish(); | ||
break; | ||
} | ||
} | ||
|
||
private void showHint(String hint){ | ||
Toast.makeText(this,hint , Toast.LENGTH_LONG).show(); | ||
} | ||
} |
This file contains 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
This file contains 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