I would like to check whether the device is supporting "Work Profile" pr not via code
Noticed that Native Support for "Work profile" from Android 5.0+ though on HTC device it is not supporting.
Could anyone share how to achieve this...
Android must have the hardware feature android.software.managed_users declared. It can be checked
PackageManager pm = getPackageManager();
if (!pm.hasSystemFeature(PackageManager.FEATURE_MANAGED_USERS)) {
// This device does not support work profiles!
}
https://developer.android.com/reference/android/content/pm/PackageManager.html
https://developers.google.com/android/work/build-dpc
For anyone reading this, I thought that checking FEATURE_MANAGED_USERS was sufficient, but in some devices you need to check too if there is anyone who will resolve the managed provisioning intent.
public boolean isManagedProvisioningAvailable() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
return false;
}
PackageManager pm = getApplicationContext().getPackageManager();
if (!pm.hasSystemFeature(PackageManager.FEATURE_MANAGED_USERS)) {
return false;
}
Intent intent = new Intent(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE);
ComponentName resolved = intent.resolveActivity(pm);
return resolved != null;
}
Related
I am new to android dev and I am not understanding the code below to determine valid intent receiver, so can any one explain me that code?
public static boolean isIntentAvailable(Context ctx,Intent intent) {
final PackageManager mgr = ctx.getPackageManager();
List<ResolveInfo> list = mgr.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
}
I bet you, no one beats Android Documentation in explaining what the above code does :)
https://developer.android.com/reference/android/content/pm/PackageManager.html#queryIntentActivities(android.content.Intent, int)
NOTE: Sharing Android Documentation link to refer more details and adding screenshot so the answer looks formatted.
I've already done this for iPhone app detection, using the meta tag described here:
http://developer.apple.com/library/ios/#documentation/AppleApplications/Reference/SafariWebContent/PromotingAppswithAppBanners/PromotingAppswithAppBanners.html
Now I'm trying to do the same for Droid-app sniffing. Basically, I want to check if the user has the 'sniffing' app installed or not.
How do I detect whether or not a smartphone has an Android app installed?
In order to make code work change "com.your.package.appname.id" with the application id.
id is the app package and also the market url eg. gmail app url at the market is https://play.google.com/store/apps/details?id=com.google.android.gm and the id/packagename is com.google.android.gm
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Bundle;
public class Example extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Put the package name here...
boolean installed = appInstalledOrNot("com.your.package.appname.id");
if(installed)
{
System.out.println("App already installed om your android");
}
else
{
System.out.println("App is not installed om your android");
}
}
private boolean appInstalledOrNot(String uri)
{
PackageManager pm = getPackageManager();
boolean app_installed = false;
try
{
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
app_installed = true;
}
catch (PackageManager.NameNotFoundException e)
{
app_installed = false;
}
return app_installed ;
}
}
From this: How to get a list of installed android applications and pick one to run
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final List pkgAppsList = context.getPackageManager().queryIntentActivities( mainIntent, 0);
Now, from pkgAppList you can see if the app is installed
I mean, how can I check that user's device is not a tablet (or music player) without dialer?
P.S.: My app uses numeric codes entered in dialer, so I want to check it's presence.
It is a always a good approach to check if the intent receiver/activity actually exists before you attempt to invoke it.
Some of the reasons being:
If a non-existent intent, you application will force close.
If the intent receiver is not present, you might want to redirect the user to the market to download the necessary application.
Depending on the existence of an intent you might want to make menu options appear/disappear.
The following snippet contains two functions: isIntentAvailable() and isActivityAvailable() ,which can perform the checking and return a boolean accordingly.
public boolean isIntentAvailable(Context context, String action) {
final PackageManager packageManager = context.getPackageManager();
final Intent intent = new Intent(action);
List resolveInfo =
packageManager.queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
if (resolveInfo.size() > 0) {
return true;
}
return false;
}
Just pass your action string as a parameter to this function before using it to start any third party application.
Seems to be what I've needed:
public static String TelephonyChecker (Context context) {
TelephonyManager manager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
if(manager.getPhoneType() == TelephonyManager.PHONE_TYPE_NONE){
return "Off";
}else{
return "On";
}
}
Is there a way to programmatically check if an Android device is running Google Services? I have an application that utilizes C2DM and want to disable menu options including this if the device (such as Kindle Fire and Nook) do not have the Google Services required.
public static boolean doesContainGsfPackage(Context context) {
PackageManager pm = context.getPackageManager();
List<PackageInfo> list = pm.getInstalledPackages(0);
for (PackageInfo pi : list) {
if(pi.packageName.equals(ACCUWX.GSF_PACKAGE)) return true; // ACCUWX.GSF_PACKAGE = com.google.android.gsf
}
return false;
}
I am not sure exactly what "Google Services" is however in the past I have found this function reliable when testing if a service is running. By replacing "some.package.name.MyService" with the Google Services package name then you should be able to check if it is running or not.
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if ("some.package.name.MyService".equals(service.service.getClassName())) {
return true;
}
}
return false;
}
How can i check whether NFC is enabled or not programmatically? Is there any way to enable the NFC on the device from my program? Please help me
NfcManager manager = (NfcManager) context.getSystemService(Context.NFC_SERVICE);
NfcAdapter adapter = manager.getDefaultAdapter();
if (adapter != null && adapter.isEnabled()) {
// adapter exists and is enabled.
}
You cannot enable the NFC programmatically. The user has to do it manually through settings or hardware button.
This can be done simply using the following code:
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (nfcAdapter == null) {
// NFC is not available for device
} else if (!nfcAdapter.isEnabled()) {
// NFC is available for device but not enabled
} else {
// NFC is enabled
}
Remember that the user can turn off NFC, even while using your app.
Source: https://developer.android.com/guide/topics/connectivity/nfc/nfc#manifest
Although you can't programically enable NFC yourself, you can ask the user to enable it by having a button to open NFC settings like so:
Intent intent
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
intent = new Intent(Settings.ACTION_NFC_SETTINGS);
} else {
Intent intent = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
}
startActivity(intent);
I might be a little late here, but I've implemented a 'complete' example with detection of
NFC capability (hardware), and
Initial NFC state (enabled or disabled in settings), and
Changes to the state
I've also added a corresponding Beam example which uses the
nfcAdapter.isNdefPushEnabled()
method introduced in later Android versions to detect beam state like in 2) and 3).
Use PackageManager and hasSystemFeature("android.hardware.nfc"), matching the <uses-feature android:name="android.hardware.nfc" android:required="false" /> element you should have in your manifest.
Since 2.3.3 you can also use NfcAdapter.getDefaultAdapter() to get the adapter (if available) and call its isEnabled() method to check whether NFC is currently turned on.
mNfcAdapter = NfcAdapter.getDefaultAdapter(this.getApplicationContext());
try {
if (mNfcAdapter != null) {
result = true;
}
}
We can verify using NfcAdapter with context.