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?

4
  • 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, 2013 at 4:56
  • @ajb You're right, it'd be getText() for the older API but it was just for example.
    – whitfin
    Sep 27, 2013 at 4: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, 2013 at 5: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, 2013 at 5:11

1 Answer 1

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()
 {

 }  
12
  • Ok, I realised my question wasn't clear because of this answer. Let me edit it.
    – whitfin
    Sep 27, 2013 at 4: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. Sep 27, 2013 at 4:34
  • It's my bad, I didn't show that ClipboardManager is imported from different packages each time. Check updated question.
    – whitfin
    Sep 27, 2013 at 4:35
  • But how do I override the fact that I declared Clipboard to be an instance of a different class?
    – whitfin
    Sep 27, 2013 at 4:37
  • @Zackehh9lives in that case you need to have two variables and chekc your condition in both if part and else part Sep 27, 2013 at 4:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.