在我的Qt Android(5.6 +,最新的SDK和NDK 10d)应用程序中,我需要在运行时因targetApi 26请求权限。From Google docs我发现我必须做这样的事情:< / p>
import android.Manifest.permission;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
public class MyActivity extends QtActivity implements ActivityCompat.OnRequestPermissionsResultCallback
{
private static final String[] LOCATION_PERMS = {
permission.ACCESS_FINE_LOCATION
};
private static final int LOCATION_REQUEST = 1337;
private boolean hasPermission(String perm)
{
return (PackageManager.PERMISSION_GRANTED == checkSelfPermission(perm));
}
public boolean canAccessLocation()
{
return hasPermission(permission.ACCESS_FINE_LOCATION);
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)
{
//Figure out what was given to us
switch(requestCode)
{
case LOCATION_REQUEST:
if (canAccessLocation())
Log.i(TAG, "Access to location is granted");
else {
Log.e(TAG, "Access to location is denied");
finish();
}
break;
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//Ensure we can run the app
if (!canAccessLocation())
requestPermissions( LOCATION_PERMS, LOCATION_REQUEST );
}
}
}
这是我的build.gradle:
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.0'
}
}
allprojects {
repositories {
jcenter()
google()
}
}
apply plugin: 'com.android.application'
dependencies {
compile "com.android.support:support-v4:27.0.2"
compile "com.android.support:support-v13:27.0.2"
compile fileTree(dir: 'libs', include: ['*.jar'])
}
但是我收到错误:包android.support.v4.app不存在。我也试图在我的工作站上找到android-support-v4.jar,但没有成功。添加
compile 'com.android.support:support-v4:+'
from here没有任何区别。
一件重要的事情:我的项目是在CMake的帮助下从命令行构建的,我不能使用像Android Studio或Eclipse这样的任何IDE。我错过了什么?
谢谢!
答案 0 :(得分:0)
实际上,通过应用Android Studio中的依赖项,更新后的build.gradle
可以使用以下设置:
apply plugin: 'com.android.application'
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:25.0.1'
compile 'com.android.support:design:25.0.1'
}
我还猜你需要导入Activity:
import android.app.Activity;
答案 1 :(得分:0)
我不知道您是否还有这个问题,但是万一有人想知道...
我将可编译SDK版本更改为:27:Android 8.1(奥利奥)是 26:Android 8.0(奥利奥)
并在build.grade
dependencies {
implementation 'com.android.support:appcompat-v7:27.0.0'
implementation 'com.android.support:design:27.0.0'
implementation 'com.android.support:support-v4:27.1.1'
}
显然android.support:support中的27.1.1做到了这一点(27.0.0无效)。 希望对您有所帮助(: