gpt4 book ai didi

android - 更改 targetSDK 版本后我的 android 应用程序显示白屏

转载 作者:行者123 更新时间:2023-11-30 00:37:16 24 4
gpt4 key购买 nike

我正在使用 Android Build Target API 24 (OS 7) 编写 Android 应用程序。对于 API 23 及以上版本,我必须提示 ACCESS_COARSE_LOCATION 的许可。所以我像下面这样更改我的 Android.Manifest。

<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="24" />

在那之后,我的应用程序只显示白屏,没有任何反应。我正在使用 Eclipse IDE。但是,当我将 targetSdkVersion 更改为 22 时,该应用程序在其他进程中工作正常,但不显示 ACCESS_COARSE_LOCATION 的权限提示,定位进程无法使用,并且在 LogCat 中显示以下错误。

Permission denial: Need ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission to get scan results

我应该怎么做?请帮我。我需要将 targetSdkVersion 更改为 24 吗?

已更新

这是我的 Manifest.xml 完整代码。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.company_name.apps.my_app_name"
android:versionCode="16"
android:versionName="2.1.1" android:installLocation="internalOnly">

<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="24" />


<uses-permission android:name="com.android.vending.BILLING" />

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL"/>

<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- end GCM permission -->


<!-- map -->

<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

<!-- External storage for caching. -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

这是我的访问权限代码。

 protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
Log.d("PermissionGAndroid6", "MarshMallow OS6");
if (this.checkSelfPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Log.d("PermissionG1","Permission Granted Prompt");
requestPermissions(new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION);
}
}
else {
setContentView(R.layout.activity_main);

Exchanger.start(this, APPLICATION_CODE, ORIENTATION, TEST_MODE);
// default first fragment is MainFragment
if (findViewById(R.id.container) != null) {
mMainFragment = new MainFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.container, mMainFragment, "MainFragment").commit();
}
LogTracker.getTracker().startUsingApp();
}

}

@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[],
int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_COARSE_LOCATION: {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.d("PermissionG", "coarse location permission granted");
setContentView(R.layout.activity_main);

Exchanger.start(this, APPLICATION_CODE, ORIENTATION, TEST_MODE);
// default first fragment is MainFragment
if (findViewById(R.id.container) != null) {
mMainFragment = new MainFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.container, mMainFragment, "MainFragment").commit();
}
LogTracker.getTracker().startUsingApp();
} else {
requestPermissions(new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION);

}
return;
}
}
}

最佳答案

试试这个,

    private Context mContext=YourActivity.this;

private static final int REQUEST = 112;


if (Build.VERSION.SDK_INT >= 23) {
String[] PERMISSIONS = {android.Manifest.permission.ACCESS_COARSE_LOCATION,android.permission.ACCESS_FINE_LOCATION};
if (!hasPermissions(mContext, PERMISSIONS)) {
ActivityCompat.requestPermissions((Activity) mContext, PERMISSIONS, REQUEST );
} else {
//do here
}
} else {
//do here
}

获取权限结果

    @Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//do here
} else {
Toast.makeText(mContext, "The app was not allowed to access your location.", Toast.LENGTH_LONG).show();
}
}
}
}

检查棉花糖的权限

    private static boolean hasPermissions(Context context, String... permissions) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}

list

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

关于android - 更改 targetSDK 版本后我的 android 应用程序显示白屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43221794/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com