402Exam
402Exam
*Package.in android.te dot SMS manager, get default() (object of sms manager)
access manager object of
* sMS manager class- it is used to send messages messages of different type like
text message data message, multimedia message the function specified in SMS manager
class.
* we have to import android.telephony. SMS manager package.
* there are different functions using which we can send messages as send text
message().
Q-write a program to send a sms message using edit text and button control?
To create an Android application that sends an SMS message using an EditText and a
Button control, We need to follow these steps:
Types of Services
Foreground Service:
Runs in the foreground and is noticeable to the user.
Must display a notification.
Example: Music player that shows a notification with playback controls.
Background Service:
Started Service:
onCreate(): Called when the service is first created. Used to initialize the
service.
onStartCommand(): Called every time a client starts the service using
startService(). The service starts and can run indefinitely.
onDestroy(): Called when the service is no longer used and is being destroyed. Used
to clean up resources.
Bound Service:
#Types of Broadcasts:-
System Broadcasts: Sent by the Android system when certain system events occur
(e.g., device boot completed, battery low, etc.).
Custom Broadcasts: Sent by applications to communicate within the app or with other
apps.
Creating a Broadcast Receiver
To create a Broadcast Receiver, We need to:
To create a Service and use it in your Android app, follow these steps:
activity_main.xml:
Summary
This example shows how to create a basic service in an Android app, register it in
the AndroidManifest.xml, and control it from an activity using start and stop
buttons. When the service starts and stops, it shows toast messages to indicate its
state. This basic structure can be extended to perform more complex background
operations as needed.
Option Menu:-
The Option Menu is the primary collection of menu items for an activity. It is
typically used for actions and options that are relevant to the current activity.
The Option Menu appears in the app bar (or toolbar) and can overflow into a
dropdown menu.
Characteristics:-
Displayed in the app bar (toolbar) or as an overflow menu.
Contains actions that have a global impact on the activity.
Example:-
1)Define the Menu in XML (res/menu/menu_main.xml):
2)Inflate the Menu in Activity (MainActivity.java):
Context Menu:-
A Context Menu is a floating menu that appears when the user performs a long-click
(press and hold) on an element. It is used for providing actions related to a
specific item in the UI.
Characteristics:
Triggered by a long-click on a View.
Provides contextual actions for the specific item.
Example:
1)Register the Context Menu (MainActivity.java):
2)Define the Context Menu in XML (res/menu/context_menu.xml):
Popup Menu
A Popup Menu is a modal menu that anchors to a particular view and displays a list
of items in a vertical list. Unlike the Context Menu, a Popup Menu is not triggered
by a long-click but is typically shown upon clicking a button or other UI element.
Characteristics:
Anchored to a specific view.
Provides a simple, lightweight list of actions.
Example:
1)Create and Show the Popup Menu (MainActivity.java):
2)Define the Popup Menu in XML (res/menu/popup_menu.xml):
Option Menu: For global actions related to the activity, usually shown in the app
bar.
Context Menu: For contextual actions related to a specific item, shown on long-
click.
Popup Menu: For a simple list of actions anchored to a view, shown on a click.
Here's a step-by-step guide to create and implement tabs using TabLayout and
ViewPager2:
1. Add Dependencies
Ensure you have the necessary dependencies in your build.gradle file:
2. Create Layout for Tabs.Create a layout file for your activity with a TabLayout
and ViewPager2 component.
activity_main.xml:
3. Create Fragments for Tabs
Create fragment classes for the different tabs you want to display. For example:
FirstFragment.java:
4. Create a PagerAdapter
Create an adapter to manage the fragments in the ViewPager.
ViewPagerAdapter.java:
5. Set Up TabLayout and ViewPager in Activity
In your main activity, set up the TabLayout and ViewPager2 to work together.
MainActivity.java:
Summary-
Dependencies: Add material and viewpager2 dependencies.
Layout: Create a layout with TabLayout and ViewPager2.
Fragments: Create fragments for each tab.
Adapter: Create a FragmentStateAdapter to manage the fragments.
Activity: Set up the TabLayout and ViewPager2 in your activity.