Asked  7 Months ago    Answers:  5   Viewed   118 times

With new android API 22 getResources().getDrawable() is now deprecated. Now the best approach is to use only getDrawable().

What changed?

 Answers

18

You have some options to handle this deprecation the right (and future proof) way, depending on which kind of drawable you are loading:


A) drawables with theme attributes

ContextCompat.getDrawable(getActivity(), R.drawable.name);

You'll obtain a styled Drawable as your Activity theme instructs. This is probably what you need.


B) drawables without theme attributes

ResourcesCompat.getDrawable(getResources(), R.drawable.name, null);

You'll get your unstyled drawable the old way. Please note: ResourcesCompat.getDrawable() is not deprecated!


EXTRA) drawables with theme attributes from another theme

ResourcesCompat.getDrawable(getResources(), R.drawable.name, anotherTheme);
Tuesday, June 1, 2021
 
Avicinnian
answered 7 Months ago
73

As stated at Google Maps Android API v2 Documentation:

Note: The info window that is drawn is not a live view. The view is rendered as an image (using View.draw(Canvas)) at the time it is returned. This means that any subsequent changes to the view will not be reflected by the info window on the map. To update the info window later (e.g., after an image has loaded), call showInfoWindow(). Furthermore, the info window will not respect any of the interactivity typical for a normal view such as touch or gesture events. However you can listen to a generic click event on the whole info window as described in the section below.

Perhaps you should try to call showInfoWindow() somewhere again in your code.

Saturday, August 7, 2021
 
andrews_nz
answered 4 Months ago
13

The entire Apache API was removed from SDK 23 since from SDK 22 it was already deprecated.

You can see it here: http://developer.android.com/sdk/api_diff/23/changes.html

You can use Retrofit or Volley to reimplement your network requests.

Sunday, August 15, 2021
 
lewiguez
answered 4 Months ago
40

Depending on the Android version running your application, you will have to use different methods. On Pre-Lollipop devices, it is pretty straight-forward:

String[] result = new String[2];

List<ActivityManager.RunningTaskInfo> runningTasks;
ComponentName componentInfo;

runningTasks = activityManager.getRunningTasks(1);
componentInfo = runningTasks.get(0).topActivity;
result[0] = componentInfo.getPackageName();
result[1] = componentInfo.getClassName();

If you are on a Lollipop or newer device, you have to use UsageStatsManager class, which requires your application to be granted specific permissions

//no inspection ResourceType
UsageStatsManager mUsageStatsManager = (UsageStatsManager)context.getSystemService("usagestats");
long time = System.currentTimeMillis();

// We get usage stats for the last 10 seconds
List<UsageStats> stats = mUsageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000*10, time);

// Sort the stats by the last time used
if(stats != null) {
    SortedMap<Long,UsageStats> mySortedMap = new TreeMap<>();
    for (UsageStats usageStats : stats) {
        mySortedMap.put(usageStats.getLastTimeUsed(),usageStats);
    }
    if(mySortedMap != null && !mySortedMap.isEmpty()) {
        return mySortedMap.get(mySortedMap.lastKey()).getPackageName();
    }
}

return null;

This will tell you if your apps has been granted permissions:

try {
    PackageManager packageManager = context.getPackageManager();
    ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
    AppOpsManager appOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
    int mode = appOpsManager.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, applicationInfo.uid, applicationInfo.packageName);
    return (mode != AppOpsManager.MODE_ALLOWED);
} catch (PackageManager.NameNotFoundException e) {
    return false;
}

And finally this will launch the Android permission granting activity for the user:

Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
activity.startActivity(intent);

Hope that helps

Thursday, August 26, 2021
 
Litty
answered 4 Months ago
21

Declaring your application as a system app should allow you to receive regular broadcasts even if your app is in "stopped state".

Add the following to your manifest and make sure your application is signed by the platform key:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
...
...
android:sharedUserId="android.uid.system">
Sunday, November 14, 2021
 
Maxime Porté
answered 4 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share