2022年最新版Android安卓面试题+答案精选(每日20题,持续更新中)【九】

这篇博客整理了2022年Android面试中的常见问题,涵盖了Android开发中遇到的各种技术挑战,如多 Dex 支持、Skype启动、对象排序、WebView自定义头部、Android Studio配置、文件操作、SSLContext设置等。通过解答这些问题,作者旨在帮助读者提升Android开发技能,为面试做好准备。
摘要由CSDN通过智能技术生成

前言

写在前面:首先是不一次性放出来的原因:资料来之不易,希望大家好好珍惜,每天花一段时间细细的消化这些题目,其次希望大家在阅读题目的时候最好跟着书或者代码一起阅读、一起敲,做到熟稔于心,信手拈来,这样面试的时候才能展现你最自信的一面。

161、无法执行dex:方法ID不在[0,0xffff]中:65536

答案:

Google终于发布了官方说明。

适用于Android的Update 2(10/31/2014) Gradle插件v0.14.0添加了对multi-dex的支持。要启用,只需在build.gradle中声明它即可:

android {
   defaultConfig {
      ...
      multiDexEnabled  true
   }
}

如果您的应用程序支持5.0之前的Android(也就是说,如果您的minSdkVersion年龄为20或更低),则还必须动态修补应用程序ClassLoader,以便它能够从辅助dexes加载类。幸运的是,有一个库可以为您完成此任务。将其添加到应用程序的依赖项中:

dependencies {
  ...
  compile 'com.android.support:multidex:1.0.0'
} 

您需要尽快调用ClassLoader补丁代码。MultiDexApplication该课程的文档提出了三种实现方法(选择其中一种,最方便的一种):

1- MultiDexApplicationAndroidManifest.xml中将类声明为应用程序:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.multidex.myapplication">
    <application
        ...
        android:name="android.support.multidex.MultiDexApplication">
        ...
    </application>
</manifest>

2-让您的Application类扩展MultiDexApplication类:

public class MyApplication extends MultiDexApplication { .. }

3- MultiDex#install通过您的Application#attachBaseContext方法进行调用:

public class MyApplication {
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
        ....
    }
    ....
}

如预期的那样,Android支持库的修订版21中提供了对multidex的支持。您可以在/ sdk / extras / android / support / multidex / library / libs文件夹中找到android-support-multidex.jar。

Multi-dex支持解决了这个问题。dx 1.8已经允许生成多个dex文件。
Android L将原生支持多dex,并且支持库的下一个版本将涵盖API 4之前的较早版本。

Anwar Ghuloum 在此 Android Developers Backstage播客集中对此进行了说明。我已经发布了相关部分的成绩单(和一般的多dex解释)。

162、通过应用程序以编程方式启动Skype,并通过密码-Android

答案:

此代码对我有用,可以在两个Skype用户之间发起呼叫:

Intent sky = new Intent("android.intent.action.VIEW");
sky.setData(Uri.parse("skype:" + user_name));
startActivity(sky);

要找到这个(和其他),请使用apktool打开Skype APK。查看AndroidManifest.xml,您将看到他们所知道的所有意图过滤器。如果要触发这些意图过滤器之一,则需要制定一个与之匹配的意图。这是上面的代码匹配的意图过滤器:

        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:scheme="skype" />
        </intent-filter>

您可以从{ {new Intent()}}免费获得类别“ android.intent.category.DEFAULT”,因此剩下的只是设置操作和URI。

tel:URI的意图过滤器如下所示:

        <intent-filter android:icon="@drawable/skype_blue" android:priority="0">
            <action android:name="android.intent.action.CALL_PRIVILEGED" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:scheme="tel" />
        </intent-filter>

因此,您开始进行操作,并为Intent提供电话:URI和“正确的事情发生了”。发生的情况是Android为tel找到正确的提供程序:URI。可能会得到用户的输入,以在Phone App和Skype之间进行选择。Skype处理tel:URI的优先级为零,这是最低的。因此,如果安装了Phone App,则可能会获得Intent。

163、Android-java-如何按对象内的某个值对对象列表进行排序

答案:

如果你要查找默认排序,则应使用Comparable而不是Comparator。

看到这里,这可能会有帮助- 类何时应该是Comparable和/或Comparator

尝试这个

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class TestSort {

    public static void main(String args[]){

        ToSort toSort1 = new ToSort(new Float(3), "3");
        ToSort toSort2 = new ToSort(new Float(6), "6");
        ToSort toSort3 = new ToSort(new Float(9), "9");
        ToSort toSort4 = new ToSort(new Float(1), "1");
        ToSort toSort5 = new ToSort(new Float(5), "5");
        ToSort toSort6 = new ToSort(new Float(0), "0");
        ToSort toSort7 = new ToSort(new Float(3), "3");
        ToSort toSort8 = new ToSort(new Float(-3), "-3");

        List<ToSort> sortList = new ArrayList<ToSort>();
        sortList.add(toSort1);
        sortList.add(toSort2);
        sortList.add(toSort3);
        sortList.add(toSort4);
        sortList.add(toSort5);
        sortList.add(toSort6);
        sortList.add(toSort7);
        sortList.add(toSort8);

        Collections.sort(sortList);

        for(ToSort toSort : sortList){
            System.out.println(toSort.toString());
        }
    }

}

public class ToSort implements Comparable<ToSort> {

    private Float val;
    private String id;

    public ToSort(Float val, String id){
        this.val = val;
        this.id = id;
    }

    @Override
    public int compareTo(ToSort f) {

        if (val.floatValue() > f.val.floatValue()) {
            return 1;
        }
        else if (val.floatValue() <  f.val.floatValue()) {
            return -1;
        }
        else {
            return 0;
        }

    }

    @Override
    public String toString(){
        return this.id;
    }
}

按照此代码对任何ArrayList进行排序

Collections.sort(myList, new Comparator<EmployeeClass>(){
    public int compare(EmployeeClass obj1, EmployeeClass obj2) {
        // ## Ascending order
        return obj1.firstName.compareToIgnoreCase(obj2.firstName); // To compare string values
        // return Integer.valueOf(obj1.empId).compareTo(Integer.valueOf(obj2.empId)); // To compare integer values

        // ## Descending order
        // return obj2.firstName.compareToIgnoreCase(obj1.firstName); // To compare string values
        // return Integer.valueOf(obj2.empId).compareTo(Integer.valueOf(obj1.empId)); // To compare integer values
        }
    });
164、将自定义标头添加到WebView资源请求-Android

答案:

尝试

loadUrl(String url, Map<String, String> extraHeaders)

要将标头添加到资源加载请求中,请定制WebViewClient并重写:

API 24+:
WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request)
or
WebResourceResponse shouldInterceptRequest(WebView view, String url)
165、如何在Android Studio和Gradle中设置-source 1.7

答案:

在构建工具19中添加了Java 7支持。你现在可以使用诸如菱形运算符,多捕获,尝试资源,开关中的字符串等功能。将以下内容添加到你的中build.gradle

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.0"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 19
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
}

需要Gradle 1.7 +,Android gradle插件0.6。+。

请注意,仅尝试使用资源需要minSdkVersion19。其他功能在以前的平台上有效。

166、Android拆分字符串

答案:

String currentString = "Fruit: they taste good";
String[] separated = currentString.split(":");
separated[0]; // this will contain "Fruit"
separated[1]; // this will contain " they taste good"

你可能要删除第二个字符串的空格:

separated[1] = separated[1].trim();

如果要用特殊字符(例如dot(。))分割字符串,则应在点之前使用转义字符\

例:

String currentString = "Fruit: they taste good.very nice actually";
String[] separated = currentString.split("\\.");
separated[0]; // this will contain "Fruit: they taste good"
separated[1]; // this will contain "very nice actually"

还有其他方法可以做到这一点。例如,你可以使用StringTokenizer类(来自java.util):

StringTokenizer tokens = new StringTokenizer(currentString, ":");
String first = tokens.nextToken();// this will contai
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值