Toast: http://developer.android.com/reference/android/widget/Toast.html
When an edited message is sent to a mobile phone, a message or similar message is displayed below the mobile phone: "message sent successfully, generally, it disappears automatically after a short stop. This is a typical Toast application;
There are multiple types of message prompt boxes, for example, Dialog (which will be described in later sections ). Toast is different from Dialog: Dialog is displayed in an exclusive manner. In other words, if you do not close it, Dialog is always displayed on the current interface, while Toast is automatically closed after a short stay;
Toast implementation is actually very simple. Here we will introduce three methods:
The makeText method is called and displayed in text mode. Because it loads a TextView by default, the third parameter can be set through the two static fields LENGTH_LONG/LENGTH_SHORT of Toast; the reason why Toast is returned is to facilitate expansion, such as calling the setDuration method;
2. Add some desired results. I personally think it is a bit like rewriting the makeText method.
// LayoutInflater inflater = (LayoutInflater) YouAcvity.= getLayoutInflater().inflate(your_layout_file,= Toast(YouActivity.
3. The Toast message prompt box will automatically disappear after a certain period of time. What should I do if I want to stay forever? Can you do this?
First of all, I will tell you how to implement it if you stay forever?
View the source code and find out, Toast. the show method uses a queue for display. The system extracts a Toast and displays it in sequence. after a certain period of time, it disappears automatically, and the system extracts the next Toast, there is no Toast directly in the entire queue; there is a private static internal class in the Toast class called: TN; Because TN is private, how should we access it? The answer is reflection.
Field field = toast. getClass (). getDeclaredField ("mTN ");
Field. setAccessible (); Object object = Method method = object. getClass (). getDeclaredMethod ("show", 5 // The object is finally defined in the class. This method is used multiple times, for example, Toast method is disabled. invoke (object, Object [0 // new Object [0] can be replaced by null, but because it is a variable parameter, it is best to use new Object [0] 7 }}
If Toast is disabled, you can use (// method = object. getClass (). getDeclaredMethod ("hide", null); at the same time, Toast provides another method, such as setGravity;
Notification: http://developer.android.com/reference/android/app/Notification.html
- Theoretical Basis:
- Status Bar and status bar: It may be clearer if you use a diagram!
- Status Bar:
- Status Bar: You need to perform a drop-down operation to view the details of the entire status bar.
- Link: Status Bar included!
- Notification has these components:
-
- Content title
- Large icon: usually displayed on the left
- Content text
- Content info: it should be better to translate the remarks, Anyway, you can believe your choice.
- Small icon
- Timestamp
- How to use Notification
- Get icationicationmanager object
- Get Notification object
- Set Notification attributes
- Set PendingIntent
- Invoke y Method
There are five steps in total !!!
The Notification design philosophy is: Manage Notification objects, Notification entity objects, Build objects (internal classes), and PendingIntent!
Manage notification objects: icationicationmanager: provides notification functions such as release, update, and cancellation.
Notification object: It has multiple object attributes.
Build object: used to create notification object and set relevant properties
PendingIntent: If you want to start a new Activity, startActivity and startActivityForResult are usually called. PendingIntent further encapsulates Intent, so that we can transfer the corresponding Activity when viewing the corresponding action of Notification.
Notification objects created under Andorid 3.0 are usually directly used: Notification building functions, and the Android OS update and upgrade usually use the other two support classes (internal classes): icationicationcompat. builder, Notification. builder has similar functions and the main difference is: icationicationcompat. the Builder is compatible with API level 4 and is provided by SupportLibrary.
Real-code drill: A small Demo is provided.
Layout file:
View Code
Code
Icationicationtest Activity nManager = demo1 = demo2 = demo2_1 = demo3 = demo4 = demo5 = builder = icationicationcompat. builder (listener (demo2.setOnClickListener (demo3.setOnClickListener (demo4.setOnClickListener (demo5.setOnClickListener (listener (@ SuppressWarnings ("deprecation" demo1 (Notification notification = notification. icon = notification. number = 2 <2 notif Ication. when = notification. tickerText = "You have a new message !! Come from Demo1 "notification. defaults = notification. flags = PendingIntent pendingIntent = PendingIntent. getActivity (, 0, getIntent (), 0 notification. setLatestEventInfo (, "Demo1", "First notification Message with Demo1! "Demo2 (. setContentTitle (" Demo2 ". setContentText (" Using 'notificationcompat. builder' "Builder. setDefaults (Notification. DEFAULT_VIBRATE). setTicker (" You have a new message !! Come from Demo2 "Intent intent = Intent (, UsingIntent. taskStackBuilder stackBuilder = TaskStackBuilder. create (stackBuilder. addParentStack (UsingIntent. pendingIntent pendingIntent = stackBuilder. getPendingIntent (0 demo2_1 (. setContentTitle ("Demo2_1 ". setContentText ("Using 'getactive'" builder. setDefaults (Notification. DEFAULT_VIBRATE ). setTicker ("You have a new message !! Come from Demo2_1 "Intent intent = Intent (, UsingIntent. intent. setFlags (Intent. FLAG_ACTIVITY_NEW_TASK | PendingIntent pendingIntent = PendingIntent. getActivity (, 0 clear1 (update (builder. setContentTitle ("Update Demo2" builder. setContentText ("Update Demo" builder. setDefaults (Notification. DEFAULT_VIBRATE ). setTicker ("Updte a new message !! Come from Demo2 "Intent intent = Intent (, UsingIntent. TaskStackBuilder stackBuilder = TaskStackBuilder. create (stackBuilder. addParentStack (UsingIntent. PendingIntent pendingIntent = stackBuilder. getPendingIntent (0}View Code
To have a vibration effect, set it in manifest file:
Note: This only provides Demo code and cannot be copied directly. If you want to practice, you need to localize it. In addition, the UsingIntent class is very simple, because it is not provided here.
For android 4.1 and later versions, you can also use BigView. Considering that the penetration rate is not high, I will not explain it here. If you are interested, you can directly go to the official website.
Setting an Activity using PendingIntent is important. There are two ways to set the Activity:
- Create an Intent-enabled Activity for the background stack push
- If you do not create a background push stack, you can set a dedicated Activity.
Demo main interface: