android - 没有响应提示用户为 SMS 设置默认处理程序的意图
问题描述
我目前无法让应用程序向用户发送对话以在 API 29 上设置 SMS 的默认处理程序。
我最初关注https://developer.android.com/guide/topics/permissions/default-handlers。
它没有用,所以我环顾四周,看到了这个:How to set Default SMS prompt for KitKat。
所以我按照https://android-developers.googleblog.com/2013/10/getting-your-sms-apps-ready-for-kitkat.html并将代码段添加到我的清单 XML 中(没有实际实现,如 StackOverflow用户在帖子中说)。不幸的是,每次我单击启动ACTION_CHANGE_DEFAULT
意图的按钮时,除了他登录之外什么都没有发生。
我的主要活动有以下 onCreate:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var btn = findViewById<Button>(R.id.button3)
btn.setOnClickListener {
Log.d("TAG", "Sending change default intent")
val setSmsAppIntent = Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT)
setSmsAppIntent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, packageName)
startActivityForResult(setSmsAppIntent, CHANGE_SMS_DEFAULT_RESULT)
}
}
显现:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testapplication">
<uses-permission android:name="android.permission.READ_SMS" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<service
android:name=".FetchSMS"
android:exported="false"></service>
<activity
android:name=".DisplayMessageActivity"
android:parentActivityName=".MainActivity" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- These are ONLY used to set app as default SMS -->
<receiver android:name=".SmsReceiver"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_DELIVER" />
</intent-filter>
</receiver>
<!-- BroadcastReceiver that listens for incoming MMS messages -->
<receiver android:name=".MmsReceiver"
android:permission="android.permission.BROADCAST_WAP_PUSH">
<intent-filter>
<action android:name="android.provider.Telephony.WAP_PUSH_DELIVER" />
<data android:mimeType="application/vnd.wap.mms-message" />
</intent-filter>
</receiver>
<!-- Activity that allows the user to send new SMS/MMS messages -->
<activity android:name=".ComposeSmsActivity" >
<intent-filter>
<action android:name="android.intent.action.SEND" />
<action android:name="android.intent.action.SENDTO" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="sms" />
<data android:scheme="smsto" />
<data android:scheme="mms" />
<data android:scheme="mmsto" />
</intent-filter>
</activity>
<!-- Service that delivers messages from the phone "quick response" -->
<service android:name=".HeadlessSmsSendService"
android:permission="android.permission.SEND_RESPOND_VIA_MESSAGE"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.RESPOND_VIA_MESSAGE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="sms" />
<data android:scheme="smsto" />
<data android:scheme="mms" />
<data android:scheme="mmsto" />
</intent-filter>
</service>
</application>
</manifest>
日志:
将不胜感激一些帮助。提前致谢。
我minSdkVersion
是 19
编辑:我刚刚在 KitKat(API 19)上的模拟器上尝试过这个并且它有效 - 但它在 Q(API 29)上没有
解决方案
试试下面的代码。
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
RoleManager roleManager = mContext.getSystemService(RoleManager.class);
// check if the app is having permission to be as default SMS app
boolean isRoleAvailable = roleManager.isRoleAvailable(RoleManager.ROLE_SMS);
if (isRoleAvailable){
// check whether your app is already holding the default SMS app role.
boolean isRoleHeld = roleManager.isRoleHeld(RoleManager.ROLE_SMS);
if (isRoleHeld){
Intent roleRequestIntent = roleManager.createRequestRoleIntent(RoleManager.ROLE_SMS);
startActivityForResult(roleRequestIntent,requestCode);
}
}
} else {
Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, defaultSystemApp);
startActivityForResult(intent, requestCode);
}
添加此权限是清单
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.app.role.SMS"/>
</intent-filter>
推荐阅读
- mongoose-schema - 如何通过猫鼬将嵌套数组对象保存到 mongo db?
- github - github repo 中的文件夹。打不开
- c# - switch-case-if 或 if-switch-case
- html - 由于 HTML 签名,Apple 邮件背景为白色
- html - css 异常行为:悬停之前
- bash - 如何远程杀死进程ID Bash
- r - 如何在R中为边缘着色并向桑基图添加标签
- python - 在 Keras 自定义损失函数中有效使用 SciPy 函数?
- azure-devops - YAML 使用变量在参数上设置值
- google-sheets - Google 表格 - 基于标准的列表中的最佳组合