Application is the base class for maintaining global application state. You can provide your own implementation by creating a subclass and specifying the name of this subclass as the android:name
attribute in <application>
of AndroidManifest.xml
. The Application class, or your subclass of the Application class, is instantiated before any other class when the process for your application/package is created.
Application Subclass
Below example shows an example of subclass of Application class. This class create application shortcut based on android version.
public class App extends Application { @Override public void onCreate() { super.onCreate(); if (Build.VERSION.SDK_INT >= 25) { createShortcut(); if(Build.VERSION.SDK_INT >=26){ pinShortcut(); } }else{ removeShortcuts(); } } @TargetApi(25) private void createShortcut() { ShortcutManager sM = getSystemService(ShortcutManager.class); Intent intent1 = new Intent(getApplicationContext(), MainActivity.class); intent1.setAction(MyUtils.ACTION_START_CAPTURE_NOW); ShortcutInfo shortcut1 = new ShortcutInfo.Builder(this, "shortcut_capture_now") .setIntent(intent1) .setShortLabel("Start Now") .setLongLabel("Start Service Now") .setDisabledMessage("Permission Denied, open app to resolve it") .setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher)) .build(); sM.setDynamicShortcuts(Arrays.asList(shortcut1)); } @RequiresApi(api = Build.VERSION_CODES.O) private void pinShortcut() { ShortcutManager shortcutManager = getSystemService(ShortcutManager.class); if (shortcutManager.isRequestPinShortcutSupported()) { ShortcutInfo pinShortcutInfo = new ShortcutInfo .Builder(getApplicationContext(), "shortcut_capture_now") .build(); Intent pinnedShortcutCallbackIntent = shortcutManager.createShortcutResultIntent(pinShortcutInfo); PendingIntent successCallback = PendingIntent.getBroadcast(getApplicationContext(), 0, pinnedShortcutCallbackIntent, 0); shortcutManager.requestPinShortcut(pinShortcutInfo, successCallback.getIntentSender()); } } @TargetApi(25) private void removeShortcuts() { ShortcutManager shortcutManager = getSystemService(ShortcutManager.class); shortcutManager.disableShortcuts(Arrays.asList("shortcut_capture_now")); shortcutManager.removeAllDynamicShortcuts(); } }
Subclass defined above must be declared in AndroidManifest.xml
. Following example illustrate how to declare application subclass in manifest file.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.aphalaprepsunaa.recorder"> <application android:name=".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/AppTheme"> <activity android:name=".ui.activities.MainActivity" android:configChanges="keyboardHidden|orientation" android:windowSoftInputMode="adjustPan"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
android:label
is the name that will show up on emulators and real devices as you’ve observed. android:name
is the name of a class (here App
) implementing the android.app.Application for this package. This attribute is used when you have a custom class that extends Application that you want to use.