我想知道要求用户进行权限检查的最佳做法以及如果用户拒绝该特定权限访问则要运行的代码.
最近在新的Android Marshmallow上遇到了很多关于权限请求对话框的问题。它们听起来像:
如何自定义 Android 权限对话框?
如何为 Android 权限对话框提供自定义文本?
为什么我不能自定义Android权限对话框?
所以这里是所有这些问题的完整答案。
我面临一个奇怪的问题.我遵循本教程,以便在运行时为我构建的应用程序请求权限.问题在于,即使我检查设备是否使用Android M并且我在运行时请求权限,它也不显示对话框.
代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_configuration);
spinner = (Spinner)findViewById(R.id.database_selection_spinner);
button = (Button)findViewById(R.id.proceedBtn);
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(ConfigurationActivity.this, Manifest.permission.INTERNET) == PackageManager.PERMISSION_GRANTED){
accessWebService();
}else {
if (shouldShowRequestPermissionRationale(Manifest.permission.INTERNET)){
StringGenerator.showToast(ConfigurationActivity.this, "Internet Permission needs to be granted");
}
requestPermissions(new String[]{Manifest.permission.INTERNET}, AppConstants.PERMISSION_REQ_CODE);
}
}else {
accessWebService();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == AppConstants.PERMISSION_REQ_CODE){
if (grantResults[0] == PackageManager.PERMISSION_GRANTED){
accessWebService();
}else {
StringGenerator.showToast(ConfigurationActivity.this, "LOL");
}
}else { …Run Code Online (Sandbox Code Playgroud) android android-permissions android-6.0-marshmallow runtime-permissions
android android-intent android-permissions android-6.0-marshmallow
我正在尝试将我的数据库导出为CSV文件.我已经适用了相应的权限AndroidManifest.xml.我只是想知道为什么我不能在我的(实际设备)Nexus 5X API级别23中编写/创建文件,其中相同的代码在我的模拟器Nexus 7 API级别22中工作.权限是否不够?
这是代码:
private void writeCsv() {
File fileDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS).getAbsolutePath(),
File.separator + "Example");
if (!fileDir.exists()) {
try {
fileDir.mkdirs();
} catch (Exception e) {
e.printStackTrace();
}
}
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS).getAbsolutePath(),
File.separator
+ "Example"
+ File.separator
+ (new Date()) + ".csv");
if (!file.exists()) {
try {
file.createNewFile(); // THIS IS WHERE THE EXCEPTION POINTS OUT
} catch (IOException e) {
e.printStackTrace();
}
}
if (file.exists()) {
try {
FileWriter fileWriter …Run Code Online (Sandbox Code Playgroud) android ioexception android-permissions android-6.0-marshmallow
我在我的android应用程序中使用https://github.com/dlazaro66/QRCodeReaderView(QR码扫描程序)
我的主要权限如下:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus"/>
Run Code Online (Sandbox Code Playgroud)
在Gradle中我有以下代码:
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.gurkhatech.schoolmanagement"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
Run Code Online (Sandbox Code Playgroud)
我已经用Java实现了代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qr);
mQrCodeReaderView = (QRCodeReaderView)findViewById(R.id.qrdecoderview);
mQrCodeReaderView.setOnQRCodeReadListener(this);
}
@Override
public void onQRCodeRead(String text, PointF[] points) {
Toast.makeText(getApplicationContext(),text,Toast.LENGTH_LONG).show();
}
@Override
public void cameraNotFound() {
}
@Override
public void QRCodeNotFoundOnCamImage() {
}
@Override
protected void onResume() {
super.onResume();
mQrCodeReaderView.getCameraManager().startPreview();
}
@Override
protected void onPause() {
super.onPause(); …Run Code Online (Sandbox Code Playgroud) 我正在开发一个以编程方式更改屏幕亮度的Android应用程序.我有一个适用于5.1.1的设置亮度方法,但是当我在6.0上运行应用程序时,它会出错并关闭应用程序.请帮忙.
以下是我的方法:
public void setBrightness(View view,int brightness){
//Just a loop for checking whether the brightness changes
if(brightness <46)
brightness = 255;
else if(brightness >150)
brightness=45;
ContentResolver cResolver = this.getApplicationContext().getContentResolver();
Settings.System.putInt(cResolver, Settings.System.SCREEN_BRIGHTNESS, brightness);
}
public void setDisplay(View view)
{
ContentResolver cResolver = getContentResolver();
Window window = getWindow();
int brightness=0;
try
{ Settings.System.putInt(cResolver,Settings.System.SCREEN_BRIGHTNESS_MODE
Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
brightness = Settings.System.getInt(cResolver, Settings.System.SCREEN_BRIGHTNESS);
System.out.println("Current Brightness level " + brightness);
}
catch (Exception e)
{
Log.e("Error", "Cannot access system brightness");
e.printStackTrace();
}
setBrightness(view,brightness);
}
Run Code Online (Sandbox Code Playgroud) 当我在棉花糖设备上测试我的应用程序时,我遇到了一个奇怪的问题,我的标签页标题没有显示。当我在所有其他设备上测试它时,标题显示得很好。在调试时,设备会遇到所有相同的点,包括我的适配器内的“getPageTitle”方法。我已经阅读了许多使用 android 的 TabLayout 和 ViewPager 的教程,只是为了确保我没有遗漏任何东西,而且据我所知,我没有做错任何事情。
这是我主要活动中的代码,
// setup viewPager
final ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
setupViewPager(viewPager);
// setup tabLayout
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(viewPager);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
Run Code Online (Sandbox Code Playgroud)
我的 setUpViewPager() 方法,
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getFragmentManager());
viewPager.setAdapter(adapter);
}
Run Code Online (Sandbox Code Playgroud)
和我的适配器类,它扩展了 FragmentPagerAdapter
private class ViewPagerAdapter extends FragmentPagerAdapter {
private static final int NUM_FRAGMENTS = 2;
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0: return AllFeaturesFragment.newInstance();
case 1: …Run Code Online (Sandbox Code Playgroud) android android-viewpager android-tablayout android-6.0-marshmallow
我的手机安装了两个语音搜索:谷歌应用程序和S语音应用程序.默认应用程序是S-voice app,如下图所示.我的问题是,如何在Android 6.0中使用编程方式获取默认语音应用程序.先感谢您
这就是我做的
private boolean isMyAppLauncherDefault(String myPackageName) {
final IntentFilter filter = new IntentFilter(Intent.ACTION_MAIN);
filter.addCategory(Intent.CATEGORY_HOME);
List<IntentFilter> filters = new ArrayList<IntentFilter>();
filters.add(filter);
List<ComponentName> activities = new ArrayList<ComponentName>();
final PackageManager packageManager = (PackageManager) getPackageManager();
packageManager.getPreferredActivities(filters, activities, null);
for (ComponentName activity : activities) {
Log.d(TAG,"======packet default:==="+activity.getPackageName());
}
for (ComponentName activity : activities) {
if (myPackageName.equals(activity.getPackageName())) {
return true;
}
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
当我输入时,上面的函数总是返回true com.samsung.voiceserviceplatform.在另一方面,默认应用程序始终返回com.google.android.googlequicksearchbox(表示谷歌语音)
android google-voice android-intent android-settings android-6.0-marshmallow
下面我有我的布局xml,将包含3 TextView和它们对齐left,centre并right分别。
<RelativeLayout
android:id="@+id/relRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="@string/contact"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/colon"
android:id="@+id/colonRow1"/>
<TextView
android:id="@+id/txtPrimaryContact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_toEndOf="@+id/colonRow3"
android:layout_toRightOf="@+id/colonRow3"
android:ellipsize="end"
android:gravity="end"
android:maxLines="1"
android:text="Joe SmithMartinWilliamCathcsdsdsdadas"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
我现在面临的问题是,ellipsize使用Android版本<6.0的android设备无法正常运行或无法正常工作。我的意思是与设备Marshmallow的版本,ellipsize更是打破了文本一旦达到最大宽度,但是对于有设备lollipop或kitkat版本,可能会过低,在,它不为长文本打破。ellipsize发生故障后的空间在文本和显示ellipses
这是小于android版本的外观Marshmallow:
考虑名称为FName LName
联系人:FName ...
这是大于android版本的外观Marshmallow:
联系人:FName LNa ...
这是较低版本或预期行为中的错误吗?还是有其他方法可以做到这一点?我想要Marshmallow所有设备中的版本文本。反正我能做到这一点?
更新资料
原来,@ Charu发表评论后,我的假设是错误的。实际上,它会隐藏设备版本<中的长文本Marshmallow …
android textview android-4.4-kitkat android-5.0-lollipop android-6.0-marshmallow