对于想了解如何在AndroidMarshmallow上使用旧版ApacheHTTP客户端?的读者,本文将提供新的信息,并且为您提供关于Androidaddednewpermissionmodelfor
对于想了解如何在Android Marshmallow上使用旧版Apache HTTP客户端?的读者,本文将提供新的信息,并且为您提供关于Android added new permission model for Android 6.0 (Marshmallow).、Android AnimatorSet.cancel() 在 Marshmallow 版本设备上不起作用、Android Marshmallow“无法播放此视频”错误、Android marshmallow中“身份”的App权限在哪里的有价值信息。
本文目录一览:- 如何在Android Marshmallow上使用旧版Apache HTTP客户端?
- Android added new permission model for Android 6.0 (Marshmallow).
- Android AnimatorSet.cancel() 在 Marshmallow 版本设备上不起作用
- Android Marshmallow“无法播放此视频”错误
- Android marshmallow中“身份”的App权限在哪里
如何在Android Marshmallow上使用旧版Apache HTTP客户端?
背景
在Android Marshmallow上,Google已完全删除了对Apache HTTP客户端的支持(链接此处),因为与其他替代品相比,它的性能不佳。
这也可能是导致Android Marshmallow上许多应用程序崩溃的原因。
问题
通过将以下行添加到gradle文件中,Google允许你仍然使用此API,而不是内置API:
useLibrary ''org.apache.http.legacy''
所以,这就是我所做的:
dependencies { classpath ''com.android.tools.build:gradle:1.3.0''}
和:
android { compileSdkVersion ''android-MNC'' buildToolsVersion "23.0.0 rc3" useLibrary ''org.apache.http.legacy'' defaultConfig { applicationId "com.example.user.androidmtest" minSdkVersion ''MNC'' targetSdkVersion ''MNC'' versionCode 1 versionName "1.0" }
当我尝试它时,它可以很好地编译(没有错误显示,并且我可以运行概念验证应用程序,因为它没有任何特殊的代码),但是当我尝试使用一些我知道的类时,是旧版API(例如“ HttpClient”类)的一部分,我发现它不允许我这样做。
我知道不建议你使用此解决方案,但我们必须至少暂时准备好该应用程序,直到我们100%应对Android Marshmallow应当更改的所有事情,并且我们不希望出现意外情况崩溃的形式。
答案1
小编典典Android Studio
抱怨org.apache.http
类
org.apache.http.NameValuePairorg.apache.http.client.utils.URLEncodedUtils
不见了。
所以我org.apache.http.legacy.jar
在 Android/Sdk/platforms/android-23/optional
文件夹中添加了app/libs
我也将此行添加到我的app.gradle文件中
compile files(''libs/org.apache.http.legacy.jar'')
但是,如果你使用更多的库,则可以使用这种方式
compile fileTree(dir: ''libs'', include: [''*.jar''])
这解决了我所有由于Google删除了对Apache HTTP客户端的支持而导致的错误。
Android added new permission model for Android 6.0 (Marshmallow).
http://www.captechconsulting.com/blogs/runtime-permissions-best-practices-and-how-to-gracefully-handle-permission-removal
What Are Runtime Permissions?
With Android 6.0 Marshmallow, Google introduced a new permission model that allows users to better understand why an application may be requesting specific permissions. Rather than the user blindly accepting all permissions at install time, the user is now prompted to accept permissions as they become necessary during application use. As you probably already know, such a change requires efforts on the part of the application developer, but what happens if you do not implement this new model? Will your application still run? What changes do you have to make to be sure your application will run smoothly on all the versions you support?
This blog will step you through a sample application that demonstrates implementing the new permissions model all while answering commonly asked questions about the new model and how it affects your application.
When to Implement the New Model
The Good
The big question for many is: will my existing applications still work on Marshmallow devices? The good news is the new model has backwards compatibility- i.e. it doesn’t require full support until you choose to target version 23 in your application. If you are targeting version 22 or below, your application will request all permissions at install time just as it would on any device running an OS below Marshmallow.
The Bad
Just because we can fall back on the old model, it does not mean that we can just avoid the new model by never targeting Marshmallow. A user with a Marshmallow device will now have the ability to revoke dangerous permissions via the application settings (we will get into what a dangerous permission is later). This means that even though the user accepted all your permissions at install time, they can later decide to take some of those permissions away from you.
The Ugly
If you have chosen not to implement the new permissions model, the revocation of permissions can cause unwanted user experiences and in some cases application crashes. The OS attempts to handle these scenarios as gracefully as possibly but developers should not rely on the OS to handle this scenario perfectly. In my experience, most permission revocations will not cause an application crash, but will absolutely degrade user experience. However, the side-effects of permission revocation are specific to an application’s implementation and I highly suggest at the very least running your application on a Marshmallow emulator and testing your application with permission revocation.
How To Implement – Best Practices
Backwards Compatibility
When implementing the new permissions model it is important to do so in such a way that your application supports Marshmallow and pre-Marshmallow devices. For example, devices running a pre-Marshmallow OS do not support asking for permissions at runtime, thus we need to be smart about when we do make these requests. In the example application, I use the method below to determine if the user’s device is a Marshmallow device or not. If the device they are using is not a Marshmallow device, we know the permissions are granted at install time and we do not need to ask to use the feature.
return(Build.VERSION.SDK_INT>Build.VERSION_CODES.LOLLIPOP_MR1);
}
Dangerous vs. Non-Dangerous Permissions
Just like the old permission model, the new permission model requires all permissions to be included in the applications manifest. This is required regardless of what OS you are targeting or running. If you forget to register the permission in your applications manifest, the application will not be able to ask for the runtime permission later.
Android defines some permissions as “dangerous” and some permissions as “normal.” Both are required in your application’s manifest but only dangerous permissions require a runtime request. Normal permissions are accepted at install time and cannot be revoked later. An example of a normal permission is android.permission.INTERNET
. Dangerous permissions are grouped into categories that make it easier for the user to understand what they are allowing the application to do. If the user accepts one permission in a group/category they accept the entire group. The opposite is true as well, if the user denies one permission in a group, the entire group in denied. The example application demonstrates this with both FINE_LOCATION
and COARSE_LOCATION
of the group LOCATION
. You will notice that once the user has granted permission for one, the application is automatically granted permission for the other without the need to ask. The table below lists all the current dangerous permissions and their respective groups.
How to Ask
To simplify requests for features that may need more than one dangerous permission, you may ask for multiple permissions at a given time. Below you will find a code snippet from the attached example application where I am requesting access to both the CAMERA
and RECORD_AUDIO
permissions. Such a request might be necessary if you are creating a custom movie recorder, for example. In some cases you might use this when prompting the user with a "warm welcome" where you ask for some permissions at app launch that support the basic functionalities of the application.
int permsRequestCode = 200;
requestPermissions(perms, permsRequestCode);
The method requestPermissions(String[] permissions, int requestCode);
is a public method found inside of the Android Activity class. Note that in this scenario above both the audio and camera permissions are of different groups and will prompt the user with two different permission dialogs to accept or decline. This means that the user can choose to accept one but not the other. If these two permissions were of the same group, the user would only see one permission prompt. You will receive the results of your request in the method onRequestPermissionResult
as shown below.
public void onRequestPermissionsResult( int permsRequestCode, String[] permissions, int[] grantResults){
switch(permsRequestCode){
case 200:
boolean audioAccepted = grantResults[0]==PackageManager.PERMISSION_GRANTED;
boolean cameraAccepted = grantResults[1]==PackageManager.PERMISSION_GRANTED;
break;
}
}
After receiving the results, you will need to handle them appropriately. In the case of the request above, the feature about to be used needs both the camera and audio permissions. If the user denied either of the permissions, the feature will be unusable and an appropriate message should be displayed.
Don’t Be Annoying
Once you have made a permission request to the user, you should not do so again unless the user requests you to do so. In the attached application I use the below method to determine if a specific permission has already been granted.
if(canMakeSmores()){
return(checkSelfPermission(permission)==PackageManager.PERMISSION_GRANTED);
}
return true;
}
You should use the above method each time your application is about enter a feature that requires the use of a dangerous permission. Even if the permission has been previously granted it is necessary to check again to be sure that the user did not later revoke that permission. The above method leverages the method checkSelfPermission(String perm)
. This method is located in the Activity class and is used to return an integer value of PERMISSION_GRANTED
or PERMISSION_DENIED
.
In the event that you have already requested permission to access a specific feature and the permission was either denied or later revoked, you should not prompt the user again. Rather, you should notify the user in some way that you need access to a specific permission in order to continue. In the example application, I use a snackbar
to let the user know that the permission was previously denied and should be granted to move forward. From the snackbar
, I allow the user to request to be asked again. Remember to try and keep permission prompts to a minimum. If you do choose to allow the user to be prompted more than once, the second time you prompt the user for that same permission they should have the ability to choose not to be asked again in the future. If the user chooses to not be asked again, the application will not have the ability to ask the user for that permission again.
Unfortunately, there is nothing out of the box with the new permissions model that stores whether you have previously asked the user for permission or not. You will need to do so on your own by leveraging the SharedPreferences
. In the attached example application, I use the methods below to check whether I have previously prompted the user for a permission or not and to record when I do so.
return (sharedPreferences.getBoolean(permission, true));
}
private void markAsAsked(String permission){
sharedPreferences.edit().putBoolean(permission, false).apply;
}
Don’t Double Check
At times, just like the example we have been working with, multiple permissions are needed in order to access one feature. In the example of a movie recorder, both the camera and audio permissions are necessary. However, if the user has previously accepted either the camera or the audio permission, we should only prompt the user for the remaining permission. In the attached example application I use the below method to filter out the permissions already requested in the past to be sure we do not ask the user about a permission they have previously been prompted for. This does not mean that the permission was granted but rather that the permission was previously requested. Again, determining how many times to prompt a user for a permission they have previously denied is specific to your application but it is never necessary to prompt the user for a permission you already have access to.
ArrayList result = new ArrayList<~>();
for(String perm : wanted){
if(!hasPermission(perm) && shouldWeAsk(perm)){
result.add(perm);
}
}
return result;
}
Suggested Permissions Flow
The below diagram visually represents the implementation described above and in the attached example application. Note that this is a suggestion based on the Google guideline to keep repeated permission prompts minimal but your implementation may require a slightly different approach.
Closing
The new permissions model is definitely a step in the right direction for both the user and the developer. Users will soon have a better understanding of exactly why you need access to their contacts, which means less Play Store complaints and hopefully a higher download rate. The example application referred to in this blog can be found here. I give you permission to download and use the code!
Android AnimatorSet.cancel() 在 Marshmallow 版本设备上不起作用
如何解决Android AnimatorSet.cancel() 在 Marshmallow 版本设备上不起作用?
AnimatorSet animatorSet = new AnimatorSet();
ValueAnimator anim1 = ValueAnimator.ofFloat(0f,1f);
ValueAnimator anim2 = ValueAnimator.ofFloat(1f,0f);
~~~
animatorSet.play(anim1).after(anim2);
animatorSet.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationCancel(Animator animation) {
Log.d("Testing","cancel");
}
}
animatorSet.start();
button.setonclickListener((v) -> {
animatorSet.cancel();
})
当 Button
被点击时,取消监听器运行良好。但是,不仅在 cancel()
版本 (API23
) 中调用 Marshmallow
。
有什么问题?
解决方法
尝试将您的 cancel()
方法修改为:
public void cancel() {
if (animatorSet != null) {
animatorSet.cancel();
}
if (next != null) {
next.cancel();
next = null;
}
}
它也适用于 API 23。如果没有,请分享您项目的更多代码以重建它并找到其他解决方案。
Android Marshmallow“无法播放此视频”错误
我正在将应用程序升级到android 6.0.除了尝试播放来自远程源的某些视频外,一切似乎都没问题.它们在我们的非Marshmallow设备上播放得很好(具体来说,我在4.1.2 Jelly Bean上有一个galaxy S3,播放完全相同的视频就好了,没问题).我们已经在6.0上使用多个设备进行了测试,问题似乎仅限于此操作系统.
当尝试在简单的视频视频中播放视频时,我会收到一条提示“无法播放此视频”.
我过去在4.1.2上的其他视频遇到了一些问题,并认为它可能是编解码器问题,我认为这可能是罪魁祸首.不会播放的视频都有以下共同点:
> AAC
> H.264
> MPEG-4 SDSM
> MPEG-4 ODSM
当我尝试使用videoview启动活动时,这就是logcat吐出的内容.
201-801/? D/audio_hw_primary﹕ out_set_parameters: enter: usecase(1: low-latency-playback) kvpairs: routing=2
201-800/? D/audio_hw_primary﹕ select_devices: out_snd_device(2: speaker) in_snd_device(0: none)
201-800/? D/msm8974_platform﹕ platform_send_audio_calibration: sending audio calibration for snd_device(2) acdb_id(15)
201-800/? D/audio_hw_primary﹕ enable_snd_device: snd_device(2: speaker)
201-800/? D/audio_hw_primary﹕ enable_audio_route: apply and update mixer path: low-latency-playback
777-1257/? I/MediaFocusControl﹕ AudioFocus requestAudioFocus() from android.media.AudioManager@f9138b2 req=1flags=0x0
4591-4591/? W/MediaPlayer﹕ Couldn't open file on client side; trying server side: java.io.FileNotFoundException: No content provider: http://videopath.mp4
201-5605/? D/NuPlayer﹕ onSetVideoSurface(0xb48c2700,no video decoder)
777-811/? I/ActivityManager﹕ displayed com.example.i.PityTheFooActivity: +108ms
1139-1139/? I/Keyboard.Facilitator﹕ onFinishinput()
201-5606/? E/GenericSource﹕ Failed to init from data source!
201-5605/? D/NuPlayerDriver﹕ notifyListener_l(0xb60986a0),(100,1,-2147483648)
4591-4605/? E/MediaPlayer﹕ error (1,-2147483648)
4591-4591/? E/MediaPlayer﹕ Error (1,-2147483648)
4591-4591/? D/VideoView﹕ Error: 1,-2147483648
这是Marshmallow的一个已知问题,或者视频可能有问题吗?
我认为这是由于Quicktime支持问题,可能需要在Android Marshmallow中作为缺陷打开,但Google也可能会这样做.
Android marshmallow中“身份”的App权限在哪里
我正在尝试使用Android Studio附带的模拟器,以查看用户可以从此设备启用/禁用的权限.
虽然开发人员可以声明他需要访问“身份”或联系人卡片,但似乎在模拟器中,没有选项可以显示已禁用“身份”访问权限的应用程序.
这是否意味着用户没有拒绝访问身份的选项?或者只是它不会出现在模拟器中,除非您创建一个请求身份的应用程序?
解决方法:
如果您使用的是API 23仿真程序,则新的runtime permissions model的Android权限分组已更改.危险权限及其关联组的完整列表可在on this table中找到.
您将注意到旧身份存储桶中的大多数权限已被完全删除或移至“联系人”组(与GET_ACCOUNTS的情况一样).
今天关于如何在Android Marshmallow上使用旧版Apache HTTP客户端?的分享就到这里,希望大家有所收获,若想了解更多关于Android added new permission model for Android 6.0 (Marshmallow).、Android AnimatorSet.cancel() 在 Marshmallow 版本设备上不起作用、Android Marshmallow“无法播放此视频”错误、Android marshmallow中“身份”的App权限在哪里等相关知识,可以在本站进行查询。
本文标签: