在下面的代码中,我正在检查可用性以及开发人员是否在清单文件中授予了权限。如果未在清单中授予许可,则应按照代码中的规定进行编程,然后“或据我所知”将其添加到清单中。
我从清单中删除了三个权限,然后启动了该应用程序。但是,该应用程序崩溃了。我希望它可以运行,因为丢失的权限会自动/以编程方式授予。
请让我知道如何修改以下代码,以实现该目标。
**代码:
-D_GNU_SOURCE
答案 0 :(得分:0)
您需要先在清单文件中声明权限,然后才能以编程方式请求它们。
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.snazzyapp">
<uses-permission android:name="android.permission.INTERNET"/>
<!-- other permissions go here -->
<application ...>
...
</application>
</manifest>
请参阅this,以获取更多信息。
答案 1 :(得分:0)
查看是否有帮助
if(ContextCompat.checkSelfPermission(mActivity,Manifest.permission.INETRNET) == PackageManager.PERMISSION_GRANTED){
// Internet permission granted
Toast.makeText(mContext,"Permission granted.",Toast.LENGTH_SHORT).show();
}else {
// Internet permission not granted
Toast.makeText(mContext,"Permission not granted.",Toast.LENGTH_SHORT).show();
}
答案 2 :(得分:0)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_select_language);
requestPermissions();
}
private void requestPermissions() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(getApplicationContext(),
Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.CALL_PHONE,
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.READ_LOGS,
Manifest.permission.SEND_SMS,
Manifest.permission.RECORD_AUDIO,
Manifest.permission.READ_SMS,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.INTERNET,
Manifest.permission.RECEIVE_SMS
}, 0);
}
}
答案 3 :(得分:0)