1

I have an app that run as a launcher, in Android 4 in work great but in Android 7 and 8 the select launcher dialog not appear

enter image description here

// 
// Decompiled by Procyon v0.5.36
// 

package com.r7developers.unityplugin;

import android.os.Build;
import android.annotation.TargetApi;
import android.os.Process;
import android.app.AppOpsManager;
import android.content.Intent;
import com.rvalerio.fgchecker.AppChecker;
import android.content.Context;

public class Plugin
{
    static Context mContext;
    static String mBundleIdentifier;
    static AppChecker mAppChecker;

    public static void init(final Context context, final String bundleIdentifier) {
        Plugin.mContext = context;
        Plugin.mBundleIdentifier = bundleIdentifier;
    }

    public static void start() {
        Plugin.mAppChecker = new AppChecker();
        Plugin.mAppChecker.other((AppChecker.Listener)new AppChecker.Listener() {
            public void onForeground(final String packageName) {
                if (packageName != null && !packageName.contains(Plugin.mBundleIdentifier)) {
                    final Intent startHomescreen = new Intent("android.intent.action.MAIN");
                    startHomescreen.addCategory("android.intent.category.HOME");
                    startHomescreen.setFlags(268435456);
                    Plugin.mContext.startActivity(startHomescreen);

                }
            }
        }).timeout(1000).start(Plugin.mContext);
    }

    public static void stop() {
        Plugin.mAppChecker.stop();
    }

    public static void requestUsageStatsPermission() {
        if (needsUsageStatsPermission() && !hasUsageStatsPermission()) {
            Plugin.mContext.startActivity(new Intent("android.settings.USAGE_ACCESS_SETTINGS"));
        }
    }

    @TargetApi(19)
    public static boolean hasUsageStatsPermission() {
        final AppOpsManager appOps = (AppOpsManager)Plugin.mContext.getSystemService("appops");
        final int mode = appOps.checkOpNoThrow("android:get_usage_stats", Process.myUid(), Plugin.mContext.getPackageName());
        final boolean granted = mode == 0;
        return granted;
    }

    public static boolean needsUsageStatsPermission() {
        return Build.VERSION.SDK_INT >= 21;
    }

    public static void openSettings() {
        final Intent intent = new Intent("android.settings.SETTINGS");
        intent.addFlags(268435456);
        Plugin.mContext.startActivity(intent);
    }
}

This code is decompiled from a jar plugin from unity project

Is there anything I'm missing?

Dharman
  • 30,962
  • 25
  • 85
  • 135
JDEV
  • 79
  • 8

1 Answers1

0

Your problem must be caused by some missing line in the manifest. Android evolve really quickly and some application originally made for older version might not work anymore due to changing in the permission system. That must apply to the Android Manifest as well. By luck you got it working on an older device, but it seems that handling newer ones is problematic.

Have you tried to recompile a way smaller version of the code and test it on multiple version ?

Just by looking at some exemple launcher (but made without the Unity Engine), can help you understand if you have miss something.

Here is a link to an Android Launcher I found (the first one) on GitHub: KISS/app/src/main/AndroidManifest.xml.

Try adding the line that you are missing from your, especially the intent-filter that help Android know what your apps should be aware of when showing it in menus, like the Launcher selector.

I am not an expert, but I know that working with Android can be a pain.

Enzo Caceres
  • 519
  • 3
  • 15
  • Thank you for answer, the manifest looks almost the same, it have the intent filter and the needed permissions, the problem is that in android 4 it remove the default launcher and then it will show the launcher dialog to select when you click on home button, but in android 7 -or+ it keep the default launcher that cause the problem that i showed it in this video https://www.youtube.com/watch?v=5fqCkLgjMyo&feature=youtu.be – JDEV Jan 01 '20 at 01:52
  • Ok, now I understand the problem better. Try going into your Settings > Apps > (the default launcher app), here you will find two subcategory, one of them is "Set as default", go in and click "Clear default", now the launcher selection box should appear again when you will press the home button, the second subcategory is "Home screen", go in and select yours. My phone is running on samsung's rom, so some menu/button might not be the same – Enzo Caceres Jan 01 '20 at 12:36
  • yes, that's will fix the problem i know that, but, i want the app to work as a launchers work, when you install a launcher in your device, you will get the "select home app" dialog so you don't need to do it manually by entering the settings and set it as a default launcher or cleaning the default launcher – JDEV Jan 01 '20 at 18:40
  • i literally tried every available code in stackoverflow and all not working :/ i test the codes in a sample activity in android studio, try to do it by yourself – JDEV Jan 01 '20 at 18:41
  • (sorry for late) It seems that Android is now using a "RoleManager" introduced in API 29, take a look at this answer [https://stackoverflow.com/a/23110115](https://stackoverflow.com/a/23110115). On my note 9, this choice was not appearing eather when trying to use another launcher, I was "forced" to click on a "set as default launcher" button proposed by the apps, making a selector appearing. – Enzo Caceres Jan 02 '20 at 19:13
  • i was testing the app in huawei device and oppo device and it looks like both have different way to do it, check this https://stackoverflow.com/questions/54059614/set-default-android-launcher-on-huawei-devices it worked fine in android 7 and android 8 in the emulator Now, i searching for a way to do it for these devices and maybe there's more, also i will try your link and let you know the results, thank you very much – JDEV Jan 03 '20 at 00:12