针对 Android O 的应用程序在使用服务时有一些新规则,其中之一是当应用程序处于后台时我们无法启动服务。
成为默认 SMS 应用程序的要求之一是:(来自 Telephony.java javadoc)
* <li>In a service, include an intent filter for {@link
* android.telephony.TelephonyManager#ACTION_RESPOND_VIA_MESSAGE}
* (<code>"android.intent.action.RESPOND_VIA_MESSAGE"</code>) with schemas,
* <code>sms:</code>, <code>smsto:</code>, <code>mms:</code>, and <code>mmsto:</code>.
* This service must also require the {@link
* android.Manifest.permission#SEND_RESPOND_VIA_MESSAGE} permission.
* <p>This allows users to respond to incoming phone calls with an immediate text message
* using your app.</p></li>
* </ul>
您可以看到我的问题...因为 ACTION_RESPOND_VIA_MESSAGE 需要具有 SEND_RESPOND_VIA_MESSAGE 权限的服务,如果我们在应用程序处于后台时接到电话并且用户通过短信拒绝来电,则服务将无法启动。
1237-6018 W/Binder: Binder call failed.
java.lang.IllegalStateException: Not allowed to start service Intent { act=android.intent.action.RESPOND_VIA_MESSAGE dat=smsto:xxxxxxxxx cmp=APP_PACKAGE_NAME (has extras) }: app is in background uid null
知道如何解决这个问题吗?
对于普通 Android 来说,情况不应该如此,因为默认实现不会调用 SMS 应用程序来拒绝带有消息的呼叫,而是使用 SmsManager。
参考自RespondViaSmsManager.java http://androidxref.com/8.1.0_r33/xref/packages/services/Telecomm/src/com/android/server/telecom/RespondViaSmsManager.javaAOSP 奥利奥
/**
* Reject the call with the specified message. If message is null this call is ignored.
*/
private void rejectCallWithMessage(Context context, String phoneNumber, String textMessage,
int subId, String contactName) {
if (TextUtils.isEmpty(textMessage)) {
Log.w(RespondViaSmsManager.this, "Couldn't send SMS message: empty text message. ");
return;
}
if (!SubscriptionManager.isValidSubscriptionId(subId)) {
Log.w(RespondViaSmsManager.this, "Couldn't send SMS message: Invalid SubId: " +
subId);
return;
}
SmsManager smsManager = SmsManager.getSmsManagerForSubscriptionId(subId);
try {
smsManager.sendTextMessage(phoneNumber, null, textMessage, null /*sentIntent*/,
null /*deliveryIntent*/);
SomeArgs args = SomeArgs.obtain();
args.arg1 = !TextUtils.isEmpty(contactName) ? contactName : phoneNumber;
args.arg2 = context;
mHandler.obtainMessage(MSG_SHOW_SENT_TOAST, args).sendToTarget();
} catch (IllegalArgumentException e) {
Log.w(RespondViaSmsManager.this, "Couldn't send SMS message: " +
e.getMessage());
}
}
似乎一些原始设备制造商(我在oneplus上经历过这一点)定制了这个并保留了需要启动短信应用程序服务的旧行为。没有记住有限制oreo
关于后台服务。
以下是一些投诉:
- https://www.reddit.com/r/oneplus/comments/8n9w37/cant_send_text_message_when_rejecting_phone_call/ https://www.reddit.com/r/oneplus/comments/8n9w37/cant_send_text_message_when_rejecting_phone_call/
- https://forums.oneplus.com/threads/reject-call-with-sms-is-not-working-after-oreo-update-oxygen-5-0-1-3t.817578/ https://forums.oneplus.com/threads/reject-call-with-sms-is-not-working-after-oreo-update-oxygen-5-0-1-3t.817578/
我在大多数短信应用程序上都经历过这种行为,包括来自 google 的 oneplus 5(安卓版本 8.1)
这仍然没有解释 OEM 如何提供短信应用程序或短信管理器 https://play.google.com/store/apps/details?id=com.microsoft.android.smsorganizer微软的产品适用于同一设备上的此用例。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)