How to make activity launcher for different api version? - android

I'm building an kiosk App for two different versions: Android 6 and before.
When app started I have StartActivity and 2 Activities for different APK lvl, start this:
public class StartActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
startActivity(new Intent(this, MainActivityNew.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
} else {
startActivity(new Intent(this, MainActivityOld.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
}
finish();
}
}
MainActivityNew and MainActivityOld must be launchers (when button home is pressed he must call)
In Manifest file i write:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ng.lockergks">
<application
android:name="com.gks.locker.App"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.AppCompat.Light.NoActionBar.FullScreen">
<activity android:name="ru.gks.locker.ui.activity.StartActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".MainActivityOld"
android:launchMode="singleInstance"
android:screenOrientation="portrait"
android:stateNotNeeded="true">
<intent-filter>
<category android:name="android.intent.category.HOME"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".MainActivityNew"
android:launchMode="singleInstance"
android:screenOrientation="portrait"
android:stateNotNeeded="true">
<intent-filter>
<category android:name="android.intent.category.HOME"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".AppListActivity"/>
</application>
</manifest>
That is, when the application starts, check the version of the API, and depending on it, runs one way or the other activity which should be a launcher. On both versions it does not work. How to define such behavior?

Related

Android 12 Device Owner Provisioning

I have an application, that can be successfully setup as Device Owner on devices up to Android 12 via QR code from JSON below:
{
"android.app.extra.PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME":
"package.CustomDeviceAdminReceiver",
"android.app.extra.PROVISIONING_DEVICE_ADMIN_PACKAGE_CHECKSUM":
"actual_checksum",
"android.app.extra.PROVISIONING_DEVICE_ADMIN_PACKAGE_DOWNLOAD_LOCATION":
"https://Site/APK_Link",
"android.app.extra.PROVISIONING_LEAVE_ALL_SYSTEM_APPS_ENABLED": true
}
App contains declared receiver:
<receiver
android:name=".deviceadmin.CustomDeviceAdminReceiver"
android:description="#string/app_name"
android:label="#string/app_name"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data
android:name="android.app.device_admin"
android:resource="#xml/enterprise_device_admin" />
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
<action android:name="android.app.action.PROFILE_PROVISIONING_COMPLETE" />
</intent-filter>
</receiver>
For Android 12 (as described here https://source.android.com/devices/tech/admin/provision) I added 2 activities:
<activity
android:name=".deviceadmin.AdminPolicyComplianceActivity"
android:screenOrientation="portrait"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<intent-filter>
<action android:name="android.app.action.ADMIN_POLICY_COMPLIANCE"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".deviceadmin.ProvisioningModeActivity"
android:screenOrientation="portrait"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<intent-filter>
<action android:name="android.app.action.GET_PROVISIONING_MODE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
1st one:
public class ProvisioningModeActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_provisioning_mode);
Intent intent = getIntent();
int provisioningMode = 1;
List<Integer> allowedProvisioningModes = intent.getIntegerArrayListExtra(DevicePolicyManager.EXTRA_PROVISIONING_ALLOWED_PROVISIONING_MODES);
if (allowedProvisioningModes.contains(DevicePolicyManager.PROVISIONING_MODE_FULLY_MANAGED_DEVICE))
provisioningMode = DevicePolicyManager.PROVISIONING_MODE_FULLY_MANAGED_DEVICE;
else if (allowedProvisioningModes.contains(DevicePolicyManager.PROVISIONING_MODE_MANAGED_PROFILE))
provisioningMode = DevicePolicyManager.PROVISIONING_MODE_MANAGED_PROFILE;
Intent resultIntent = new Intent();
resultIntent.putExtra(DevicePolicyManager.EXTRA_PROVISIONING_MODE, provisioningMode);
setResult(RESULT_OK, resultIntent);
finish();
}
}
and 2nd one (almost empty):
public class AdminPolicyComplianceActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin_policy_compliance);
setResult(RESULT_OK);
finish();
}
}
but I got error while enrollment: "Can't setup device. Can't use the admin app. It's missing components or corrupted".
Can somebody find that I missed please?
From Android 12 we are supposed to have components exported safely. Since your activities ProvisioningModeActivity and AdminPolicyComplianceActivity uses intent filter, we have to set the exported flag.
<activity
android:name=".deviceadmin.AdminPolicyComplianceActivity"
android:screenOrientation="portrait"
android:permission="android.permission.BIND_DEVICE_ADMIN"
android:exported="true">
<intent-filter>
<action
android:name="android.app.action.ADMIN_POLICY_COMPLIANCE"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".deviceadmin.ProvisioningModeActivity"
android:screenOrientation="portrait"
android:permission="android.permission.BIND_DEVICE_ADMIN"
android:exported="true">
<intent-filter>
<action android:name="android.app.action.GET_PROVISIONING_MODE"
/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
This way firmware will be able to recognize the activity and launch it.
Also, regarding android:testOnly="false", this flag must be false if you are going to do QR code provisioning. If its true then, you will be able to remove admin via android settings.
#faz the reason this code doesn't work on Android 11 and below is because the ProvisioningModeActivity intent is not fired with an extra integer array of DevicePolicyManager.EXTRA_PROVISIONING_ALLOWED_PROVISIONING_MODES. To overcome this add a null check to the allowedProvisioningModes

Two launcher activities creating two APKs

I recently added a splash-screen to my app and made it as launcher activity to display it when the app started, the manifest looks like,
<activity android:name=".LaucherActivity" android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
And I have the MainActivity which is already a launcher activity in the manifest shown below,
<activity
android:name=".MainActivity"
android:screenOrientation="portrait"
android:theme="#style/AppTheme.TransparentTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> s
</intent-filter>
</activity>
So, this is actually creating two APKs, one for the splash and the other main activity. But I wanted the SplashActivity and MainActivity in the same APK running one after the other. How can I achieve that? I found many related questions but none of them are working for me.
Use <intent-filter> only once in manifest. like this
<activity android:name=".LaucherActivity" android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:screenOrientation="portrait"
android:theme="#style/AppTheme.TransparentTheme">
</activity>
public class LaucherActivity extends Activity {
private static int SPLASH_TIME_OUT = 3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent i = new Intent(LaucherActivity.this, MainActivity.class);
startActivity(i);
finish();
}
}, SPLASH_TIME_OUT);
}
}
Remove this from you MainActivity.
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Now call your MainActivity from your Splash Activity

App is installed twice

I have a shoppinglist App coded in Android-Studios. My app does have a splash screen. When I install the app, it is installed twice. When I uninstall one, the other one uninstalls too. I tried to delete the first intent filter on splashscreen, but then I did not have a splash screen anymore. I want my splashscreen to be remain. How to solve that? My manifest looks like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.projects.buylist">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".SplashScreen"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
The app is only one.
You have simply two activities (and then 2 icons) that can work as launcher.
If you don't want, you have to remove this part in one Activity
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<category android:name="android.intent.category.LAUNCHER" /> this is telling android you want the activity to be visible from the app launcher. To solve it, remove the intent-filter from MainActivity.
Delete your main intent in XML and create something like this, which will run splashscreen and then open your MainActivity
public class SplashsScreen extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
Intent mainIntent = new Intent(SplashsScreen.this, MainActivity.class);
SplashsScreen.this.startActivity(mainIntent);
SplashsScreen.this.finish();
}
}, 1500); // 1500 ms = 1.5 s
}
}

Manifest for home intent and launcher intent

What I have done is made a home screen application. Aka when you press home, it goes back to my app.
What I am struggling with though, is adding a normal launcher side to it, aka making it appear as an app in the apps menu. My intention is to create a simple screen that tells you and provides button access to change the home default settings. A wizard/setup guide in effect.
However, I am not sure how to have both together in one app, as it is something new to me.
The home bit works, it is the second activity that doesnt.
Manifest:
<activity
android:name=".HomeActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name"
android:launchMode="singleTask"
android:theme="#style/FullscreenTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.HOME"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
<activity android:name=".LauncherActivity">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
Add the android.intent.action.MAIN inside the LauncherActivity Intent filter so it looks like this
<activity
android:name=".HomeActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name"
android:launchMode="singleTask"
android:theme="#style/FullscreenTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.HOME"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
<activity android:name=".LauncherActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>

App's application created but main activity is not create

My app is a Launcher.
start an app on launcher.
when my launcher is in background, from logcat I can see that Application is created but the main activity is not created.
public class LauncherApplication extends Application {
#Override
public void onCreate()
{
super.onCreate();
......
Log.d("TAG", "onCreated");
}
}
public class MyLauncher extends Activity {
.....
}
Manifest.xml
<application
android:name="com.launcher.LauncherApplication"
android:label="#string/application_name"
android:hardwareAccelerated="true"
android:process="android.process.acore"
android:largeHeap="true"
android:persistent="true" >
<activity
android:name="com.launcher.MyLauncher"
android:configChanges="mcc|mnc|keyboardHidden|keyboard|orientation|screenLayout|screenSize|smallestScreenSize|navigation|uiMode"
android:launchMode="singleTask"
android:clearTaskOnLaunch="true"
android:stateNotNeeded="true"
android:screenOrientation="portrait"
android:persistent="true"
android:allowBackup="true"
android:windowSoftInputMode="stateUnspecified|adjustPan" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.HOME"/>
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.MONKEY" />
</intent-filter>
</activity>
......
</Application>
MyLauncher is the main activity.
My question is:
when log prints "onCreated", that means my launcher is killed bo OS in background and LauncherApplication is restarted ?

Categories

Resources