Reputation: 18649
I have this code which enables sharing the app:
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
try
{
if ( android.os.Build.VERSION.SDK_INT >= 14 )
{
getMenuInflater().inflate(R.layout.menu, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
myShareActionProvider = (ShareActionProvider)item.getActionProvider();
myShareActionProvider.setShareHistoryFileName(
ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);
myShareActionProvider.setShareIntent(createShareIntent());
return true;
}
}
catch ( Exception e )
{
}
return false;
}
private Intent createShareIntent()
{
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT,
"I am using mobile apps for starting a business from http://www.myurl.com");
return shareIntent;
}
// Somewhere in the application.
public void doShare(Intent shareIntent)
{
try
{
if ( android.os.Build.VERSION.SDK_INT >= 14 )
{
// When you want to share set the share intent.
myShareActionProvider.setShareIntent(shareIntent);
}
}
catch ( Exception e )
{
}
}
But when I make the build target 2.2 I get a compile error for this code saying this is code level of Android version 14, but my app accepts lower Android versions.
Is there a way not to have this kind of a compile error? Or is there a way around this issue with slightly different code that works more universally?
Thanks!
Upvotes: 0
Views: 19
Reputation: 1007434
But when I make the build target 2.2
When you set your build target to API Level 8, you fail to compile, as you are referring to classes that did not exist.
I am going to assume that you are not setting your build target (e.g., Project > Properties > Android in Eclipse) to API Level 8, but instead to something 14 or higher. And I will assume that you really meant that you are setting your android:minSdkVersion
to be something like 8.
I get a compile error for this code saying this is code level of Android version 14, but my app accepts lower Android versions.
Correct. That is Lint, telling you that you need to ensure that your code is taking this stuff into account.
Is there a way not to have this kind of a compile error?
Add @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
to your methods where you are checking the Build.VERSION.SDK_INT
and comparing it to Build.VERSION_CODES.ICE_CREAM_SANDWICH
.
The @TargetApi()
annotation says, in effect, "for the scope of this method, please consider my android:minSdkVersion
to be this higher number, not the one normally used". The compile errors will disappear... until such time as you start using something newer than API Level 14 in those methods.
Upvotes: 1