星期四, 2月 20, 2014

Hangout 造成發送與接收SMS問題


Google 在Android 4.4改以Hangout來取代原本預設的簡訊App,因此在App如果有寫到傳送或接收SMS功能時,在Kitkat上或是有安裝Hangout的舊版本(Android 4.3及以下)手機上,可能會運作不正常。

在接收SMS時,因為Hangout有可能由使用者在Google Play下載,裝在舊版的手機上,為了避免有兩個預設的SMS App在收到SMS時都提供通知,因此若有安裝Hangout,Hangout似乎會攔住"android.provider.Telephony.SMS_RECEIVED"這個broadcast event,來避免舊的簡訊App也產生notification,因此如果您的App有聆聽此event來獲得收到的簡訊時,在安裝Hangout之後會發現收不到"android.provider.Telephony.SMS_RECEIVED"。此時解決方法是在Broadcast Receiver上加上priority,並將priority設為大於3,如此一來便能解決收不到"android.provider.Telephony.SMS_RECEIVED"的問題。請參考此討論

<receiver android:name="com.google.android.apps.babel.sms.AbortSmsReceiver" android:permission="android.permission.BROADCAST_SMS" android:enabled="false">
    <intent-filter android:priority="3">
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />
    </intent-filter>
</receiver>
在傳送SMS時在Kitkat上Google 已經把簡訊App拿掉,因此已以往Intent的方式來發送SMS可能會遇到找不到預設App的問題,解決方式如下:請參考此討論

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) //At least KitKat
    {
        String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(activity); //Need to change the build to API 19

        Intent sendIntent = new Intent(Intent.ACTION_SEND);
        sendIntent.setType("text/plain");
        sendIntent.putExtra(Intent.EXTRA_TEXT, smsText);

        if (defaultSmsPackageName != null)//Can be null in case that there is no default, then the user would be able to choose any app that support this intent.
        {
            sendIntent.setPackage(defaultSmsPackageName);
        }
        activity.startActivity(sendIntent);

    }
    else //For early versions, do what worked for you before.
    {
        Intent sendIntent = new Intent(Intent.ACTION_VIEW);
        sendIntent.setData(Uri.parse("sms:"));
        sendIntent.putExtra("sms_body", smsText);
        activity.startActivity(sendIntent);
    }

沒有留言: