[070] implementation code for Android-related functions

Source: Internet
Author: User
. --- <Directory> -----
  1. Click back twice to exit the program.
  2. Click the button to display the notification implementation in the status bar
  3. Use code to layout controls
. --- <001. Click back twice to exit the program> -----
1 private long exitTime = 0; 2 3 @ Override 4 public boolean onKeyDown (int keyCode, KeyEvent event) {5 if (keyCode = KeyEvent. KEYCODE_BACK 6 & event. getAction () = KeyEvent. ACTION_DOWN) {7 8 if (System. currentTimeMillis ()-exitTime)> 2000) {9 Toast. makeText (getApplicationContext (), "exit the program again", 10 Toast. LENGTH_SHORT ). show (); 11 exitTime = System. currentTimeMillis (); 12} else {13 finish (); 14 System. exit (0); 15} 16 return true; 17} 18 return super. onKeyDown (keyCode, event); 19}
. --- <002. click the button to display the notification implementation in the status bar> -----

Effect:

  1. Click the "Display Notification" button in the first figure, and a Notification appears in the status bar, as shown at the top of the screen.
  2. Pull down the status bar to see the information displayed in the second figure.
  3. Click the notification in the second picture to jump to the activity in the third picture.

Implementation:

NotificationActivity. java

 1 public class NotificationsActivity extends Activity { 2     int notificationID = 1; 3      4     /** Called when the activity is first created. */ 5     @Override 6     public void onCreate(Bundle savedInstanceState) { 7         super.onCreate(savedInstanceState); 8         setContentView(R.layout.main); 9     }10     11     public void onClick(View view) {12         displayNotification();13     }14     15     protected void displayNotification()16     {17         //---PendingIntent to launch activity if the user selects18         // this notification---19         Intent i = new Intent(this, NotificationView.class);20         i.putExtra("notificationID", notificationID);21 22         PendingIntent pendingIntent =23             PendingIntent.getActivity(this, 0, i, 0);24 25         NotificationManager nm = (NotificationManager)26             getSystemService(NOTIFICATION_SERVICE); 27 28         Notification notif = new Notification(29             R.drawable.ic_launcher, 30             "Reminder: Meeting starts in 5 minutes",31             System.currentTimeMillis());32 33         CharSequence from = "System Alarm";34         CharSequence message = "Meeting with customer at 3pm...";35         36         notif.setLatestEventInfo(this, from, message, pendingIntent);37 38         //---100ms delay, vibrate for 250ms, pause for 100 ms and39         // then vibrate for 500ms---40         notif.vibrate = new long[] { 100, 250, 100, 500};41         nm.notify(notificationID, notif);        42     }43 }

Icationicationview. java

 1 public class NotificationView extends Activity 2 { 3     @Override 4     public void onCreate(Bundle savedInstanceState)  5     { 6         super.onCreate(savedInstanceState); 7         setContentView(R.layout.notification); 8              9         //---look up the notification manager service---10         NotificationManager nm = (NotificationManager) 11             getSystemService(NOTIFICATION_SERVICE);12 13         //---cancel the notification that we started--- 14         nm.cancel(getIntent().getExtras().getInt("notificationID"));15     }16 }

Download source code: icationications.zip

. --- <003. Use code to layout controls> -----
 1 public class UICodeActivity extends Activity { 2     /** Called when the activity is first created. */ 3     @Override 4     public void onCreate(Bundle savedInstanceState) { 5         super.onCreate(savedInstanceState); 6         //setContentView(R.layout.main); 7         //---param for views--- 8         LayoutParams params =  9             new LinearLayout.LayoutParams(10                 LayoutParams.FILL_PARENT,11                 LayoutParams.WRAP_CONTENT);12 13         //---create a layout---14         LinearLayout layout = new LinearLayout(this);15         layout.setOrientation(LinearLayout.VERTICAL);16 17         //---create a textview---18         TextView tv = new TextView(this);19         tv.setText("This is a TextView");20         tv.setLayoutParams(params);21 22         //---create a button---23         Button btn = new Button(this);24         btn.setText("This is a Button");25         btn.setLayoutParams(params);26                         27         //---adds the textview---28         layout.addView(tv);29 30         //---adds the button---31         layout.addView(btn);32 33         //---create a layout param for the layout---34         LinearLayout.LayoutParams layoutParam = 35             new LinearLayout.LayoutParams(36                     LayoutParams.FILL_PARENT,37                     LayoutParams.WRAP_CONTENT );38 39         this.addContentView(layout, layoutParam);40     }41 }

Source code download: UICode.zip

. --- <004. click back twice to exit the program> -----. --- <005. click back twice to exit the program> -----. --- <006. click back twice to exit the program> -----

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.