String url = "http://grupovrt.ddns.net:81/v4.2.apk";
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription("Virtual Romania Tv UPDATE");
request.setTitle("UPDATE Virtual Romania Tv");
// in order for this if to run, you must use the android 3.2 to compile your app
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
if (!checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE,
R.string.title_activity_categori))
{
Toast.makeText(getBaseContext(), "Not allowed to save files", Toast.LENGTH_SHORT).show();
} else {
// try to save the file
request.setDestinationInExternalPublicDir("/update", "v4.2.apk");
}
// get download service and enqueue file
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
错误:错误:(108,49)错误:找不到符号变量WRITE_EXTERNAL_STORAGE
编辑。
如果获得写作许可,我确实寻求过。 但如果不是,我不知道如何征求许可。 请帮助我,我尝试了我在谷歌上找到的所有东西,但不能。我是初学者。
if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getBaseContext(), "Descarc noi actualizari!", Toast.LENGTH_SHORT).show();
String url = "http://grupovrt.ddns.net:81/v4.2.apk";
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription("Virtual Romania Tv UPDATE");
request.setTitle("UPDATE Virtual Romania Tv");
// in order for this if to run, you must use the android 3.2 to compile your app
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
// get download service and enqueue file
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
Toast.makeText(getBaseContext(), "Descarc noi actualizari!", Toast.LENGTH_SHORT).show();
}
request.setDestinationInExternalPublicDir("/update", "v4.2.apk");
}else {
Toast.makeText(getBaseContext(), "Nu ai permisiunea sa descarci!", Toast.LENGTH_SHORT).show();
}
答案 0 :(得分:2)
这对我有用:
int permissionCheck = ContextCompat.checkSelfPermission(context, android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(context, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
Toast.makeText(context, "Trebuie sa oferi acces la spatiul de stocare!", Toast.LENGTH_SHORT).show();
ActivityCompat.requestPermissions(context, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 110);
ActivityCompat.requestPermissions(context, new String[]{android.Manifest.permission.RECORD_AUDIO}, 110);
} else {
Toast.makeText(context, "Descarc noi actualizari!", Toast.LENGTH_SHORT).show();
}
}
答案 1 :(得分:0)
为了简化代码,我建议您使用下一个代码:
创建OnPermissionRequested接口:
public interface OnPermissionRequested {
void onGranted();
}
在活动/片段中添加下一个检查权限的方法。您还可以创建主要活动和片段并进行扩展以避免重复的代码:
OnPermissionRequested mPermissionRequest;
protected void requestPermission(String permission, OnPermissionRequested listener) {
mPermissionRequest = listener;
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(this, permission)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
Toast.makeText(this, "Permission denied, try again later please.", Toast.LENGTH_SHORT).show();
} else {
// request the permission.
ActivityCompat.requestPermissions(this,
new String[]{permission}, 2456);
}
} else {
mPermissionRequest.onGranted();
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (mPermissionRequest != null)
mPermissionRequest.onGranted();
}
}
调用requestPermission方法,您需要检查权限,并在授予权限时使用您要执行的操作设置第二个参数。
requestPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE,
new OnPermissionRequested() {
@Override
public void onGranted() {
// what you want to do
createPdf();
}
});
我希望你觉得它很有用。祝你好运