【问题标题】:Apk working well on Lollipop, but not on Jelly Bean and MarshmallowApk 在 Lollipop 上运行良好,但在 Jelly Bean 和 Marshmallow 上运行良好
【发布时间】:2018-10-16 09:42:04
【问题描述】:

当我使用带有 android Lollipop 的手机时,我的所有功能都运行良好,但是,当我使用 Android Marshmallow 或 Jelly Bean 时,应用程序崩溃了。

以下是 app 模块的构建 gradle 文件:

apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion '23.0.1'
defaultConfig {
    applicationId "com.example.k.sms"
    minSdkVersion 14
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
productFlavors {
}
}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
}

这是我在模拟器中使用 android Marshmallow 测试我的应用程序时的 logcat

12-26 19:10:41.860 18000-18000/com.example.k.sms D/AndroidRuntime: Shutting down VM
   12-26 19:10:41.860 18000-18000/com.example.k.sms E/AndroidRuntime: FATAL EXCEPTION: main
                                                                      Process: com.example.k.sms, PID: 18000
                                                               java.lang.SecurityException: Sending SMS message: uid 10057 does not have android.permission.SEND_SMS.
                                                                   at android.os.Parcel.readException(Parcel.java:1599)
                                                                   at android.os.Parcel.readException(Parcel.java:1552)
                                                                   at com.android.internal.telephony.ISms$Stub$Proxy.sendTextForSubscriber(ISms.java:768)
                                                                   at android.telephony.SmsManager.sendTextMessageInternal(SmsManager.java:310)
                                                                   at android.telephony.SmsManager.sendTextMessage(SmsManager.java:293)
                                                                   at com.example.k.sms.MainActivity$3.onClick(MainActivity.java:149)
                                                                   at android.view.View.performClick(View.java:5198)
                                                                   at android.view.View$PerformClick.run(View.java:21147)
                                                                   at android.os.Handler.handleCallback(Handler.java:739)
                                                                   at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                   at android.os.Looper.loop(Looper.java:148)
                                                                   at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

【问题讨论】:

  • 堆栈跟踪说什么?
  • 这里一切正常。可能是其他方面的问题。
  • "is crashed" -- 使用 LogCat 检查与您的崩溃相关的 Java 堆栈跟踪:stackoverflow.com/questions/23353173/…
  • 我的应用使用 android.permission.SEND_SMS。 ,棉花糖会认为它很危险吗?
  • java.lang.SecurityException:发送 SMS 消息:uid 10057 没有 android.permission.SEND_SMS。从你的 logcat 来看,这是一个危险的权限,你应该弄清楚如何处理它:developer.android.com/guide/topics/permissions/requesting.html 检查一下

标签: java android android-6.0-marshmallow android-4.1-jelly-bean


【解决方案1】:

targetSdkVersion 23

java.lang.SecurityException:发送 SMS 消息:uid 10057 没有 android.permission.SEND_SMS。

看来你刚刚碰到targetSdk 没有意识到后果。 Marshmallow 引入了运行时权限模型,该模型在您以 API23 或更高版本为目标时启动,并且您的应用程序只需要支持新的运行时权限模型,因为在这种情况下,清单声明的权限不再足够。

快速解决方案是将targetSdk 设置为22(或更低),因为只有这样运行时权限才会启动。引用docs:

在所有版本的 Android 上,您的应用都需要声明正常 以及它在其应用清单中所需的危险权限,如 在声明权限中描述。然而,这样做的效果 声明因系统版本和您的 应用的目标 SDK 级别:

  • 如果设备运行的是 Android 5.1 或更低版本,或者您应用的目标 SDK 为 22 或更低:如果您在您的 manifest,用户必须在安装时授予权限 应用程序;如果他们不授予权限,则系统不会安装 应用程序。
  • 如果设备运行的是 Android 6.0 或更高版本,并且 您应用的目标 SDK 为 23 或更高版本:应用必须列出 清单中的权限,并且它必须请求每个危险 应用程序运行时所需的权限。用户可以授予或 拒绝每个权限,应用程序可以在受限的情况下继续运行 即使用户拒绝权限请求,也可以使用。

如果您需要旧 API 中不可用的任何东西并且必须保持 targetSdk 23 或更高,那么您必须支持运行时权限(有一些外部库可以帮助解决此问题)。

【讨论】:

    【解决方案2】:

    在清单中为棒棒糖和其他低版本提供 sms 权限,并为高于棒棒糖的版本添加 Marshmallow 权限检查,如 android 中的运行时权限中所述

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多