0

I'm writing something in Android and in order to tailor for an older SDK, I need to change what a variable becomes. I'm using ClipboardManager which has different versions based on SDK. The issue is to create this variable easily, I have to do it in an if, and my code won't compile after due to the variable not being detected.

Example:

if(android.os.Build.VERSION.SDK_INT >= 11){
    final android.content.ClipboardManager clipboard = (ClipboardManager)context.getSystemService(Context.CLIPBOARD_SERVICE);
} else {
    final android.text.ClipboardManager clipboard = (ClipboardManager)context.getSystemService(Context.CLIPBOARD_SERVICE);
}
if (clipboard.hasPrimaryClip()) {
    // Do stuff
}

Because the instance of clipboard depends on the SDK, if (clipboard.hasPrimaryClip()) complains at me.

Is there any way to do this other than making two variables and checking for null?

whitfin
  • 4,539
  • 6
  • 39
  • 67
  • I'm not an Android expert, but based on the documentation I found, `android.text.ClipboardManager` doesn't have a `hasPrimaryClip` method. So if the version is < 11, then what were you expecting `clipboard.hasPrimaryClip()` to do? – ajb Sep 27 '13 at 04:56
  • @ajb You're right, it'd be `getText()` for the older API but it was just for example. – whitfin Sep 27 '13 at 04:57
  • Well, if you'll have to call a differently named method for the two different branches of the `if`...`else`, then I don't see how you thought you could do the method call outside the `if`. However, if you were going to call some method that **does** exist in both, you could declare it to be `android.text.ClipboardManager` (outside the `if`), because the variable would still be able to hold an `android.content.ClipboardManager` since it's a subclass. – ajb Sep 27 '13 at 05:03
  • Ok I'll replace it. The point is that was an example, I didn't think about that method being that API+ only. – whitfin Sep 27 '13 at 05:11

1 Answers1

0

Declare it as a class member

ClipboardManager clipboard;

Then

if(android.os.Build.VERSION.SDK_INT >= 11){
clipboard = (ClipboardManager)context.getSystemService(Context.CLIPBOARD_SERVICE);
}else {
 clipboard = (ClipboardManager)context.getSystemService(Context.CLIPBOARD_SERVICE);
}
 if (clipboard.hasPrimaryClip()) {
// Do stuff
}

Eidt to the edited question

   if(android.os.Build.VERSION.SDK_INT >= 11){
     final android.content.ClipboardManager clipboard =  (ClipboardManager)context.getSystemService(Context.CLIPBOARD_SERVICE);
       if (clipboard.hasPrimaryClip()) {
        // Do stuff
        doSomething(); 
       }
   } else {
     final android.text.ClipboardManager clipboard =(ClipboardManager)context.getSystemService(Context.CLIPBOARD_SERVICE);
      if (clipboard.hasPrimaryClip()) {
        // Do stuff
        doSomething(); 
       }
   }

Then

 public void doSomething()
 {

 }  
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • Ok, I realised my question wasn't clear because of this answer. Let me edit it. – whitfin Sep 27 '13 at 04:32
  • @Zackehh9lives if i understand correctly you need to declare the variable with class scope so it is available through out the class. no need for two variables. – Raghunandan Sep 27 '13 at 04:34
  • It's my bad, I didn't show that ClipboardManager is imported from different packages each time. Check updated question. – whitfin Sep 27 '13 at 04:35
  • But how do I override the fact that I declared Clipboard to be an instance of a different class? – whitfin Sep 27 '13 at 04:37
  • @Zackehh9lives in that case you need to have two variables and chekc your condition in both if part and else part – Raghunandan Sep 27 '13 at 04:39
  • @Zackehh9lives if the condition is true you intend do dosomething similar for both the cases? – Raghunandan Sep 27 '13 at 04:43
  • Yes, same thing for both - but if moving the stuff to do into the if is better I'll do it that way. I didn't really think it'd be doable. – whitfin Sep 27 '13 at 04:44
  • @Zackehh9lives check `if (clipboard.hasPrimaryClip())` in both if and else then call a method `dosomething` so if any condition satisfies it calls dosomething which is common in both cases. – Raghunandan Sep 27 '13 at 04:45
  • @Zackehh9lives http://stackoverflow.com/questions/14189544/copy-with-clipboard-manager-that-supports-old-and-new-android-versions. – Raghunandan Sep 27 '13 at 04:49
  • Yeah that's the way I'm doing it now, too bad it couldn't just be those few lines :p – whitfin Sep 27 '13 at 04:51
  • FYI, based on the doc I found, `android.content.ClipboardManager` is a direct subclass of `android.text.ClipboardManager`. So if the latter had a `hasPrimaryClip()` method that was overridden in the subclass, it would be simple to have him declare the variable as `android.text.ClipboardManager`. However, `android.text.ClipboardManager` doesn't have a `hasPrimaryClip` method at all, so there's a lot more confusion here than initially appears. – ajb Sep 27 '13 at 04:58
  • ok then just dosomething on inside the if else with out checking the condition. so if less then 11 do something else dosomething. the idea however is the same. – Raghunandan Sep 27 '13 at 05:46