Last active
December 3, 2016 09:08
-
-
Save lixingcong/ff1bebc3b0bc0fc8cb463c816e229acc to your computer and use it in GitHub Desktop.
Android M ask for permission
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
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="li.grain2"> | |
<uses-permission android:name="android.permission.CAMERA"/> | |
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> | |
<uses-permission android:name="android.permission.INTERNET"/> | |
<application | |
android:allowBackup="true" | |
android:icon="@mipmap/ic_launcher" | |
android:label="@string/app_name" | |
android:supportsRtl="true" | |
android:theme="@style/AppTheme"> | |
<activity android:name=".MainActivity"> | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN"/> | |
<category android:name="android.intent.category.LAUNCHER"/> | |
</intent-filter> | |
</activity> | |
</application> | |
</manifest> |
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
public class MainActivity extends AppCompatActivity { | |
final public static int REQUEST_CODE_ASK_FOR_PERMISSONS = 0; | |
final private String[] all_permissions=new String[]{ | |
Manifest.permission.CAMERA, | |
Manifest.permission.READ_EXTERNAL_STORAGE, | |
Manifest.permission.WRITE_EXTERNAL_STORAGE, | |
Manifest.permission.INTERNET | |
}; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
ask_for_all_permissions(); | |
if(check_if_permissions_got()==false){ | |
Toast.makeText(getApplicationContext(),"Could not get permissons",Toast.LENGTH_SHORT).show(); | |
}else{ | |
Toast.makeText(getApplicationContext(),"Good permissions",Toast.LENGTH_SHORT).show(); | |
} | |
} | |
public void ask_for_all_permissions(){ | |
if (Build.VERSION.SDK_INT >= 23) { | |
if(check_if_permissions_got()==false){ | |
ActivityCompat.requestPermissions(this,all_permissions, REQUEST_CODE_ASK_FOR_PERMISSONS); | |
}else{ | |
// already permit | |
} | |
} else { | |
// API was lower than 23 | |
Toast.makeText(getApplicationContext(),"API not need to ask for permission",Toast.LENGTH_SHORT).show(); | |
} | |
} | |
private boolean check_if_permissions_got(){ | |
for(String i:all_permissions){ | |
if(ContextCompat.checkSelfPermission(getApplicationContext(),i) == PackageManager.PERMISSION_DENIED) | |
return false; | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment