Two launcher activities creating two APKs - android

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

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

How to make activity launcher for different api version?

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?

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
}
}

NoClassDefFoundError in SplashScreen

In my App, it opens a Splash Screen then MainActivity. I wrote the following code
SplashActivity.java
public class SplashActivity extends Activity {
private final int SPLASH_DISPLAY_LENGHT = 2000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
/* Create an Intent that will start the Menu-Activity. */
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
}, SPLASH_DISPLAY_LENGHT);
}
}
MainActiviy.java
here
And I added both MainActivity and SplashActivity to manifest as following:
<activity
android:name="com.emy.healthytips.SplashActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation|screenSize"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.emy.healthytips.MainActivity"
android:label="#string/app_name"
android:configChanges="keyboardHidden|orientation|screenSize"
android:launchMode="singleTop">
<meta-data
android:name="android.app.default_searchable"
android:value=".MainActivity" />
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="com.emy.healthytips.MainActivity"
android:scheme="oauth" />
</intent-filter>
</activity>
But it gives me the following Exception
FATAL EXCEPTION: main
java.lang.NoClassDefFoundError: com.emy.healthytips.MainActivity
at com.emy.healthytips.SplashActivity$1.run(SplashActivity.java:20)
In this line
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
How can I fix this? Hope anyone can help me.
Thanks in advance.
Multiple things...
1) The way you are doing it defeats the purpose of a splash screen. Splash screens are supposed to give the user a pretty picture while the app loads in the background. All you are doing is adding an extra 2s delay. Take a look at this post: Android SplashScreen
2) Many people say that this method does not work on <4.0. Not sure why, but just a heads up (https://stackoverflow.com/a/5486970/2066079)
3) instead of:
startActivity(intent);
finish();
you should use:
SplashActivity.this.startActivity(intent);
SplashActivity.this.finish();
You want to use the activity's version of startActivity() instead of the Runnable's. This might be unneccessary, but if it doesn't help, atleast it's good practice.
4) Also, like I mentioned in my comment, using android:name=".MainActivity" instead of android:name="com.emy.healthytips.MainActivity" in the xml is preferred to eliminate possible unchecked typo errors.

How to start a new application on Android

How can I start a new application on Android? I have done a new applications NewHomeScreen and Hello and on NewHomeScreen I wrote this code.
#Override public void onCreate(Bundle state) {
super.onCreate(state);
setContentView(R.layout.main);
Intent mainIntent = new Intent(this,Hello.class);
startActivity(mainIntent);
}
However, it does not start Hello application. Debugger says that state has value null but what should it be? I also wrote this to Manifest:
<activity android:name="Hello">
<intent-filter>
<action android:name="android.intent.action.HELLO" />
<category android:name="android.intent.category.HELLO"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Try getting rid of the intent-filter in your Home activity. You don't need it anyway, if it's not your main screen.
I think you forgot to specify the main Activity in your AndroidManifest.xml file:
<application android:icon="#drawable/icon">
<activity android:name="NewHomeScreen" android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.HELLO" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

Categories

Resources