Use of Notification to notify notification
Create a icationicationtest project and modify the code in activity_main.xml as follows:
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: orientation = "vertical">
<Button
Android: id = "@ + id/send_notice"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: text = "Send notice"
/>
</LinearLayout>
The layout file is very simple. There is only one Send notice button in it to Send a notification. Next, modify
The code in MainActivity is as follows:
Public class MainActivity extends Activity implements OnClickListener {
Private Button sendNotice;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
SendNotice = (Button) findViewById (R. id. send_notice );
SendNotice. setOnClickListener (this );
}
@ Override
Public void onClick (View v ){
Switch (v. getId ()){
Case R. id. send_notice:
Icationicationmanager manager = (NotificationManager) getSystemService (NOTIFICATION_SERVICE );
Notification notification = new Notification (R. drawable. ic_launcher, "This is ticker text", System. currentTimeMillis ());
Notification. setLatestEventInfo (this, "This is content title", "This is content text", null );
Manager. Y (1, notification );
Break;
Default:
Break;
}
}
}