Can I build an app works for phones with & without google maps capability (allow users to use the map and others to exclude this functionality)
-
02-07-2021 - |
문제
I am building an app that has multiple useful functionalities. One activity uses google maps. Can I make it so that users can use this functionality if they wish, but exclude it from older phones that cant use it (similar to how a video game can be played online or offline)? The real reason im confused is because i have to declare this in the manifest for the entire app (is there a way for me to just exclude a certain activity if users dont have map functionality?). I also will be requiring gps for the map activity, but i dont want to limit this app to only phones with these advanced functionalities since the other activities are much less complex.
해결책
The real reason im confused is because i have to declare this in the manifest for the entire app (is there a way for me to just exclude a certain activity if users dont have map functionality?)
You can put android:required="false"
in your <uses-library>
element. Then, at runtime, check to see if MapActivity
exists. If it does, you have the Google Maps SDK add-on, and you can launch your MapActivity
subclass. If MapActivity
does not exist, do something else.
Here is a sample project that demonstrates this: https://github.com/commonsguy/cw-omnibus/tree/master/Maps/NooYawkMapless
The manifest has:
<uses-library android:name="com.google.android.maps" android:required="false" />
and the launcher activity is:
package com.commonsware.android.maps;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
public class MapDetector extends Activity {
@Override
public void onCreate(Bundle instanceState) {
super.onCreate(instanceState);
try {
Class.forName("com.google.android.maps.MapActivity");
startActivity(new Intent(this, NooYawk.class));
}
catch (Exception e) {
Toast
.makeText(this,
"Google Maps are not available -- sorry!",
Toast.LENGTH_LONG)
.show();
}
finish();
}
}
다른 팁
Yes is the short answer. You can inspect whether a handler is available for a certain intent (e.g. google maps), and if not disable the button that would open that screen.
I'l try to find the code I've used for this before...
Edit:
private boolean isCallable(Intent intent) {
List<ResolveInfo> list = getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
}