如何在您的代码中支持多个Android版本?

dha*_*val 19 java android

访问android android.jar中的联系人,版本1.6有People.CONTENT_URI用于调用联系人相关信息,而在以后的版本中,我们需要对RawContacts.CONTENT_URI提供api支持.

例如,因为它在Android 2.2中更改了URI,所以访问日历也是如此.

是否有最佳实践来管理所有不同的更改,而无需为每个版本的更改添加额外的应用程序或单独构建?

Nei*_*end 13

对于我的钱,一个非常好的答案是在http://android-developers.blogspot.co.uk/2010/07/how-to-have-your-cupcake-and-eat-it-too.html.但是,这个例子比需要的要复杂得多,所以基于此,这里有一个如何在构建通知时处理它的例子.这种工作的根本原因是java引擎如何解释类​​的结果:它只在需要时查看它们,所以如果你在类中包装特定于版本的代码并且只在你知道使用该版本时才创建它,那么它都可以工作...

据我所知,有两代创建通知的方法,以及第二代中的命名更改.这样就有三种方法可以做到.对于每种方式,创建一个包含通知生成的类:

第一种方法(用于姜饼):

public class MyNotificationBuilderToGingerBread {
Notification notification = null;

MyNotificationBuilderToGingerBread(Context myContext, int icon, String ticker, String title, String info, Long timeStamp, PendingIntent pendingIntent, int flags) {
    notification = new Notification(R.drawable.ic_sb, ticker, timeStamp);
    notification.setLatestEventInfo(myContext, title, info, pendingIntent);
    notification.flags |= flags;        
}

Notification get() {
    return notification;
}
}
Run Code Online (Sandbox Code Playgroud)

第二种方法,Honeycomb到IceCreamSandwich:

public class MyNotificationBuilderHoneyCombToIceCreamSandwich {
Notification.Builder mb = null;

MyNotificationBuilderHoneyCombToIceCreamSandwich(Context myContext, int icon, String ticker, String title, String info, Long timeStamp, PendingIntent pendingIntent, boolean onGoing) {
    mb = new Notification.Builder(myContext);
    mb.setSmallIcon(icon);
    mb.setContentIntent(pendingIntent);
    mb.setContentTitle(title);
    mb.setContentText(info);
    mb.setWhen(timeStamp);
    if (ticker != null) mb.setTicker(ticker);       
    mb.setOngoing(onGoing);
}

Notification get() {
    return mb.getNotification();
}   
}
Run Code Online (Sandbox Code Playgroud)

名字改变的第二代,Jellybean(从目前为止开始......):

public class MyNotificationBuilderJellyBean {

Notification.Builder mb = null;

MyNotificationBuilderJellyBean(Context myContext, int icon, String ticker, String title, String info, Long timeStamp, PendingIntent pendingIntent, boolean onGoing) {
    mb = new Notification.Builder(myContext);
    mb.setSmallIcon(icon);
    mb.setContentIntent(pendingIntent);
    mb.setContentTitle(title);
    mb.setContentText(info);
    mb.setWhen(timeStamp);
    if (ticker != null) mb.setTicker(ticker);       
    mb.setOngoing(onGoing);
}

Notification get() {
    return mb.build();
}   
}
Run Code Online (Sandbox Code Playgroud)

然后,您只需要选择要动态实例化的类:

// System information
private final int sdkVersion = Build.VERSION.SDK_INT;
// If you want to go really old:
    // (actually, there is a question about how this issue should be handled
    // systematically. Suggestions welcome.)
// final int sdkVersion = Integer.parseInt(Build.VERSION.SDK);

    // This is for a permanent notification. Change the final argument (flags or boolean) if it isn't meant ot be
    // For meaning of other variable, see notification documentation on the android website.
    if (sdkVersion < Build.VERSION_CODES.HONEYCOMB) {
        MyNotificationBuilderToGingerBread mnb = new MyNotificationBuilderToGingerBread(myContext, R.drawable.notification_icon, ticketText, title, infoText, timeStampMillis, pendingIntentForTapOnFullNotitifcation, Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR);
        notification = mnb.get();
    }
    else if (sdkVersion < Build.VERSION_CODES.JELLY_BEAN) {
        MyNotificationBuilderHoneyCombToIceCreamSandwich mnb = new MyNotificationBuilderHoneyCombToIceCreamSandwich(myContext, R.drawable.notification_icon, ticketText, title, infoText, timeStampMillis, pendingIntentForTapOnFullNotitifcation, true);
        notification = mnb.get();
    }
    else {
        MyNotificationBuilderJellyBean mnb = new MyNotificationBuilderJellyBean(myContext, R.drawable.notification_icon, ticketText, title, infoText, timeStampMillis, pendingIntentForTapOnFullNotitifcation, true);
        notification = mnb.get();
    }

    // Send the notification.
    notificationManager.notify(idForNotificationManager, notification);
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!


ome*_*med 8

有许多资源可供您用来帮助支持多个版本的android.

  1. 阅读这篇博客文章在这里,然后阅读这一块在这里,他们会帮你解决API级版本支持问题.
  2. 阅读博客文章,了解多屏支持,尤其是如何在res文件夹中解析资产层次结构.这将帮助您了解和设计如何进行资产文件夹结构,以支持不同的屏幕大小/密度和Android版本.
  3. 最后编写自己的自定义ant构建脚本,以便可以使用所有版本的android进行编译.


st0*_*0le 5

老实说,这很痛苦。

通常,我只是隔离不同的代码部分,然后使用抽象类对其进行访问。因此在技术上为不同的操作系统创建不同的版本。

但是还有其他方法。我见过的最好的方法是使用反射。