Android — 支持 1.6+,并支持 1.5 中已弃用的代码?
我正在尝试构建一个使用 SmsMessage 类的应用程序,但有两个版本,具体取决于设备的 API 级别:
android.telephony.gsm.SmsMessage (在 1.6 及更高版本中已弃用)
android.telephony.SmsMessage (新类对于 1.6 及更高版本)
我想以 1.5 为目标,但仍让较新的类 (android.telephony.SmsMessage) 在 1.6 或更高版本的设备上运行。我该怎么做?
我已经厌倦了这个:http://devtcg.blogspot。 com/2009/12/graceively-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);
}
}
}
I'm trying to build a app that uses the SmsMessage class but there are two versions depending on the API level of the device:
android.telephony.gsm.SmsMessage (deprecated for 1.6 and above)
android.telephony.SmsMessage (the new class for 1.6 and up)
I want to target 1.5 and yet have the newer class (android.telephony.SmsMessage) run on devices with 1.6 or higher. How do I do this?
I have already tired this: http://devtcg.blogspot.com/2009/12/gracefully-supporting-multiple-android.html but I couldn't get it to work (the author doesn't mention how he/she handles the different imports, the exact 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);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要以
android-4
或更高版本为目标,否则较新的类将不存在。关于加载正确的版本,您可以使用条件类加载,如 此示例项目适用于联系人内容提供程序的两个版本。另外,这篇文章是 Google 关于这个主题的说法。
You will need to target
android-4
or higher, otherwise the newer class will not exist.With respect to loading in the correct version, you can use conditional class loading, demonstrated in this sample project for the two editions of the contacts content provider. Also, this article is what Google has to say on the subject.
使用CommonsWare的示例,我能够创建这个(有效):
[清单设置]
1.将目标SDK设置为4(或更高)(Android 1.6+)
2.将最小SDK设置为3(Android 1.5)
[OfflineSMSReceiver .java]
[SmsMessageBridge.java]
[OldSmsMessage.java]
[NewSmsMessage.java]
再次感谢 CommonsWare。
Using CommonsWare's 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 min SDK to 3 (Android 1.5)
[OfflineSMSReceiver.java]
[SmsMessageBridge.java]
[OldSmsMessage.java]
[NewSmsMessage.java]
Thanks again to CommonsWare.