Android - 支持1.6+,并支持1.5中的弃用代码?

时间:2010-07-03 20:54:53

标签: android api deprecated

我正在尝试构建一个使用SmsMessage类的应用程序,但有两个版本,具体取决于设备的API级别:

android.telephony.gsm.SmsMessage(已弃用1.6及以上)

android.telephony.SmsMessage(1.6及以上的新类)

我想要定位1.5,然后在1.6或更高版本的设备上运行较新的类(android.telephony.SmsMessage)。我该怎么做?

我已经厌倦了这个:http://devtcg.blogspot.com/2009/12/gracefully-supporting-multiple-android.html但我无法让它工作(作者没有提到他/她如何处理不同的导入,确切的api级别设置等。)

感谢。

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);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您需要定位android-4或更高版本,否则新版本将不存在。

关于加载正确的版本,您可以使用条件类加载,在this sample project中演示两个版本的联系人内容提供商。此外,this article是Google在此主题上所说的话。

答案 1 :(得分:0)

使用CommonsWare的例子,我能够创建它(有效):

[清单设置]

1.将目标SDK设置为4(或更高)(Android 1.6 +)

2.将min SDK设置为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();
    }

}

再次感谢CommonsWare。