We often use notification to notify users of new calls or text messages so as not to interrupt the running of the current program.
The following is a brief toast reminder:
private void baseToast(){ Toast.makeText(getApplicationContext(), "Hello toast!", Toast.LENGTH_SHORT).show(); }
The first parameter is to get the context, the second is the specific content of the reminder, and the third is the reminder time.
Next, let's take a look at how to customize a toast reminder:
// Customize toast private void customtoast () {// obtain the Inflater object and view layoutinflater Inflater = getlayoutinflater (); view layout = Inflater. inflate (R. layout. toast_layout, (viewgroup) findviewbyid (R. id. toast_layout_root); // you can obtain the imageview image = (imageview) Layout control in the view. findviewbyid (R. id. image); image. setimageresource (R. drawable. ic_launcher); textview text = (textview) layout. findviewbyid (R. id. text); text. set Text ("Hello! This is a custom toast! "); // Set Toast = new toast (getapplicationcontext (); toast. setgravity (gravity. center_vertical, 0, 0); toast. setduration (toast. length_short); toast. setview (layout); toast. show ();}
The toast_layout.xml layout file is defined in layout:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/toast_layout_root" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp" android:background="#DAAA" > <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_marginRight="10dp" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="fill_parent" android:textColor="#FFF" /></LinearLayout>
Effect after running:
The custom toast is much better than the default one. Of course, you can customize the more complex and nice toast if you want.
The following is an example of notification:
Private void shownotification () {// get a icationicationmanager object icationicationmanager manager = (icationicationmanager) getsystemservice (context. notification_service); // instantiate a notification int icon = R. drawable. ic_launcher; charsequence text = "Missed Call Notification"; long when = system. currenttimemillis (); Notification = new notification (icon, text, when); // set the latest event message and pendingintent context = getapplicationco Ntext (); charsequence contenttitle = "hello"; charsequence contenttext = "your missed phone number! "; Intent = new intent (intent. action_call, Uri. parse ("Tel: 5554"); pendingintent = pendingintent. getactivity (this, 0, intent, 0); notification. setlatesteventinfo (context, contenttitle, contenttext, pendingintent); // start the reminder manager. Y (1, notification );}
In this example, call is used, so do not forget to add permissions:
<uses-permission android:name="android.permission.CALL_PHONE"/>
Effect after running:
After pulling down the above prompt bar:
Click the corresponding content to make a call: