Using Android Beam less API level 14 - android

So, that's my problem, I wanna have one Activity to work on all of the API level I support on my Activity (from API level 8 to current API level).
My problem is that if I want to use Android Beam I need my Activity to implement
NfcAdapter.CreateNdefMessageCallback
and
NfcAdapter.OnNdefPushCompleteCallback
but I cannot make it work if the SDK level is below 14.
Does anyone know if I can do something else to make it work like for example youtube application does or do I need to have two different applications, one for Android less API 14 and another one for Android API 14?

Do not use the interface implementation directly in your activity.
Instead, check the SDK level and set the callback manually if you are above SDK14
e.g:
private class myCallback implements NfcAdapter.CreateNdefMessageCallback {
#Override
public NdefMessage createNdefMessage(NfcEvent nfcEvent) {
// Your callback code
...
}
}
MyNfcAdapter.setNdefPushMessageCallback(new myCallback, activity, activities);

Related

How can I have a class only run on certain API levels?

I'm building a call blocking app that uses Itelephony to block calls. I want my app to work on API levels starting at API 16. In order for my App to work on current API levels, I need to use CallScreeningService to screen phone calls. The issue is that CallScreeningService only works on API levels 24 and up. How can I make it so Itelephony blocks call for API level below 24 and this class blocks calls for API levels 24 and up?
import android.telecom.Call;
import android.telecom.CallScreeningService;
import android.util.Log;
public class CallScreenService extends CallScreeningService {
private static final int REQUEST_ID = 1;
#Override
public void onScreenCall(Call.Details callDetails) {
CallResponse.Builder response = new CallResponse.Builder();
Log.e("CallBouncer", "Call screening service triggered");
}
}
Hit Alt + Enter (on a Mac) on the error to get a quick fix to your issue like this:
Here you see that you can fix the issue by using one of two annotations.
The #RequiresApi marks the class as having to be run on a specific API level or above, despite your min sdk level.
#RequiresApi(Build.VERSION_CODES.N)
public class CallScreenService extends CallScreeningService {
private static final int REQUEST_ID = 1;
#Override
public void onScreenCall(Call.Details callDetails) {
CallResponse.Builder response = new CallResponse.Builder();
Log.e("CallBouncer", "Call screening service triggered");
}
}
This will cause a build error if you try to use it outside of a version check.
Then you can do conditional version checking to use your new class or the old one.
Hope that helps!
if(android.os.Build.VERSION.SDK_INT >= 24){
//do the thing that requires SDK 24 and greater
else{
//do the thing that requires less than SDK 24
}
SDK_INT documentation
SDK_INT is the SDK version of the software currently running on this hardware device. This value never changes while a device is booted, but it may increase when the hardware manufacturer provides an OTA update.
Here is a link to all the SDK codes:
SDK Version Codes

Checking multi window support

I've a problem with checking is device supports Mutli Window Mode. I'm using this function to check it isInMultiWindowMode() but it've added in API 24, and when i'm runs my app on device with lower api version it cause an exception. There is any replacement for this function for lower api versions?
There is any replacement for this function for lower api versions?
Not in the Android SDK. There is no multi-window mode (from the Android SDK's standpoint) prior to API Level 23. And, for whatever reason, Google elected not to add isInMultiWindowMode() to ActivityCompat, perhaps because they cannot support the corresponding event (onMultiWindowModeChanged()).
So, here's a free replacement method:
public static boolean isInMultiWindowMode(Activity a) {
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) {
return false;
}
return a.isInMultiWindowMode();
}
Add that to some utility class somewhere and call it as needed.
Also note that isInMultiWindowMode() suffers from a race condition that makes it unreliable, IMHO.
What #CommonsWare explained is true, it is a race condition. Hence, isInMultiWindowMode() will give actual result if you call it from inside post method:
View yourView = findViewById(R.id.yourViewId);
yourView.post(new Runnable() {
#Override
public void run() {
boolean actualResult = isInMultiWindowMode();
}
});

Android lint: does not show message “Call requires API level 17 (current min is 14)”

In some case, I found the android studio lint does not show the message “Call requires API level xxx (current min is xxx)”.
public class TestActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_enter_leave_wifi);
if(isDestroyed()){
}
}
private class Test {
Test(){
if (isDestroyed()){
}
}
}
I found there is the error message in the isDestroyed() in the onCreate, but there is no such error tips in the constructor of Test
How to solve this issue?
UPDATE:
Current minSdkVersion is 14. That's why the isDestroyed(), which requires 17, in onCreate gives the error tip
The functionality you want to perform requires higher API level access than your current minimum in which that functionality/access is not available.
In order to achieve that, simply change the API level of current minimum to the required one in app:gradle file. And then recompile the project.
Good Luck

Support higher version of android api

I want to launch NFC settings activity, which is done using action ACTION_WIRELESS_SETTINGS prior to API level 16. But in API level 16 and above it is done using ACTION_NFC_SETTINGS.
I am compiling my source using Android 4.0.3 which is API level 15.
How can I support for higher level so that it can open NFC settings in all versions.
Do i need to compile my source with API level 16 or higher and make min sdk version 15?
You would typically compile your application with the SDK of the highest API version that you use in your code (or the latest available SDK). In your app's manifest you add the minimum and target SDK versions:
<uses-sdk android:minSdkVersion="..." android:targetSdkVersion="..." />
These values define how your app will be treated on a device in terms of (backwards) compatibility features.
Then, in your code, whenever you use API calles that are not available on a certain API level, you would surround those calles with a check for the Build.VERSION.SDK_INT (as JaKoZo showed).
With regard to the NFC settings, you could do even without that check and instead catch the exception that is thrown when no activity is registered for the ACTION_NFC_SETTINGS intent:
try {
this.startActivityForResult(
new Intent(android.provider.Settings.ACTION_NFC_SETTINGS),
1); // magic number to detect when this startActivityForResult returns
} catch (Exception e) {
try {
this.startActivityForResult(
new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS),
1); // magic number to detect when this startActivityForResult returns
} catch (Exception e1) {
}
}
Instead of the constant ACTION_NFC_SETTINGS, you could also use the hard-coded string "android.settings.NFC_SETTINGS". While I don't really recommend this, you would not need to build against a higher API level (if it's only that constant that causes you to do so).
[...]
this.startActivityForResult(
new Intent("android.settings.NFC_SETTINGS"),
1); // magic number to detect when this startActivityForResult returns
[...]
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
//do stuff
}
u mean this?

Is it possble to target Android 2.1 and use CalendarProvider on devices with Android 4.0 and higher?

Is it possble to target Android 2.1 and use CalendarProvider on devices with Android 4.0 and higher or is it can only be achieved by creating 2 separate APKs?
That depends on what you mean by "target".
If you mean "set targetSdkVersion" to Android 2.1, you can still use whatever APIs you want, so long as you only try calling them when you are running on a device that has them.
If you mean "set the build target" to Android 2.1, you can still use whatever APIs you want, so long as you use reflection to access the ones that are newer than API Level 7. Since CalendarContract is a content provider, that mostly is a matter of accessing various static data members, such as CONTENT_URI. Here is an example of using reflection to get at a CONTENT_URI value:
private static Uri CONTENT_URI=null;
static {
int sdk=new Integer(Build.VERSION.SDK).intValue();
if (sdk>=5) {
try {
Class<?> clazz=Class.forName("android.provider.ContactsContract$Contacts");
CONTENT_URI=(Uri)clazz.getField("CONTENT_URI").get(clazz);
}
catch (Throwable t) {
Log.e("PickDemo", "Exception when determining CONTENT_URI", t);
}
}
else {
CONTENT_URI=Contacts.People.CONTENT_URI;
}
}
(note: this example is designed to run on Android 1.5 and higher -- depending on your minSdkVersion, you could use Build.VERSION.SDK_INT instead of new Integer(Build.VERSION.SDK).intValue()).
If by "target" you mean something else, then we would need clarification of your use of the verb "target".

Categories

Resources