Android - Support 1.6+ and support for obsolete code in 1.5?

I am trying to create an application that uses the SmsMessage class, but there are two versions depending on the device API level:

android.telephony.gsm.SmsMessage (deprecated for 1.6 and above)

android.telephony.SmsMessage (new class for 1.6 and above)

I want to target 1.5, but another class (android.telephony.SmsMessage) works on devices with 1.6 or higher. How to do it?

I'm already tired of this: http://devtcg.blogspot.com/2009/12/gracefully-supporting-multiple-android.html , but I could not get it to work (the author does not mention how he / she handles different imports, precise api level settings, etc.)

Thanks.

import java.util.Date; import com.apps.myapp.Utilities; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.telephony.gsm.SmsMessage;//*NOTE* depreciated in v1.6+ public class OfflineSMSReceiver extends SMSReceiver { @Override public void onReceive(Context context, Intent intent) { System.out.println("SMS_RECEIVED"); System.out.println(Utilities.getNow()); //---get the SMS message passed in--- Bundle bundle = intent.getExtras(); SmsMessage[] msgs = null; Date date; long timeStamp; String time; String str = ""; if (bundle != null) { //---retrieve the SMS message received--- Object[] pdus = (Object[]) bundle.get("pdus"); msgs = new SmsMessage[pdus.length]; for (int i=0; i<msgs.length; i++){ msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]); timeStamp = msgs[i].getTimestampMillis(); date = new Date(timeStamp); time = this.getTime(date.getHours(),date.getMinutes(),date.getSeconds()); str += "SMS from " + msgs[i].getOriginatingAddress(); str += " :"; str += msgs[i].getMessageBody().toString(); str += "\n"; str += "TIME: "+time+"\t"+this.getNowDate(); } System.out.println(str); } } } 
+4
source share
2 answers

You need to target android-4 or higher, otherwise the new class will not exist.

Regarding the download in the correct version, you can use the conditional class download shown in this sample project for two releases of the content provider contacts. Also, this article is what Google has to say on this.

+1
source

Using the CommonsWare example, I was able to create this (which works):

[manifest settings]

1.Set the target SDK to 4 (or higher) (Android 1.6+)

2. Set the minimum SDK to 3 (Android 1.5)

[OfflineSMSReceiver.java]

 import java.util.Date; import com.apps.myapp.Utilities; import com.apps.myapp.SmsMessageBridge; import android.content.Context; import android.content.Intent; import android.os.Bundle; public class OfflineSMSReceiver extends SMSReceiver { @Override public void onReceive(Context context, Intent intent) { System.out.println("SMS_RECEIVED"); System.out.println(Utilities.getNow()); //---get the SMS message passed in--- Bundle bundle = intent.getExtras(); SmsMessageBridge[] msgs = null; Date date; long timeStamp; String time; String str = ""; if (bundle != null) { //---retrieve the SMS message received--- Object[] pdus = (Object[]) bundle.get("pdus"); msgs = new SmsMessageBridge[pdus.length]; for (int i=0; i<msgs.length; i++){ msgs[i] = SmsMessageBridge.INSTANCE.createFromPdu((byte[])pdus[i]); timeStamp = msgs[i].getTimestampMillis(); date = new Date(timeStamp); time = this.getTime(date.getHours(),date.getMinutes(),date.getSeconds()); str += "SMS from " + msgs[i].getOriginatingAddress(); str += " :"; str += msgs[i].getMessageBody().toString(); str += "\n"; str += "TIME: "+time+"\t"+this.getNowDate(); } System.out.println(str); } } } 

[SmsMessageBridge.java]

 import android.os.Build; public abstract class SmsMessageBridge { public abstract SmsMessageBridge createFromPdu(byte[] pdu); public abstract long getTimestampMillis(); public abstract String getOriginatingAddress(); public abstract String getMessageBody(); public static final SmsMessageBridge INSTANCE = getBridge(); private static SmsMessageBridge getBridge() { final int sdkVersion = new Integer(Build.VERSION.SDK).intValue(); if(sdkVersion>3) { return new NewSmsMessage(); } else { return new OldSmsMessage(); } } } 

[OldSmsMessage.java]

 import android.telephony.gsm.SmsMessage;//*NOTE* depreciated in v1.6+ @SuppressWarnings("deprecation") public class OldSmsMessage extends SmsMessageBridge { private SmsMessage myMSG; @Override public SmsMessageBridge createFromPdu(byte[] pdu) { myMSG = SmsMessage.createFromPdu(pdu); return this; } @Override public long getTimestampMillis() { return myMSG.getTimestampMillis(); } @Override public String getOriginatingAddress() { return myMSG.getOriginatingAddress(); } @Override public String getMessageBody() { System.out.println("v1.5"); return myMSG.getMessageBody(); } } 

[NewSmsMessage.java]

 import android.telephony.SmsMessage; public class NewSmsMessage extends SmsMessageBridge { private SmsMessage myMSG; @Override public SmsMessageBridge createFromPdu(byte[] pdu) { myMSG = SmsMessage.createFromPdu(pdu); return this; } @Override public String getMessageBody() { //System.out.println("v1.6+"); return myMSG.getMessageBody(); } @Override public String getOriginatingAddress() { return myMSG.getOriginatingAddress(); } @Override public long getTimestampMillis() { return myMSG.getTimestampMillis(); } } 

Thanks again to CommonsWare.

0
source

Source: https://habr.com/ru/post/1314643/


All Articles