. ---<< 目錄 >>-----
- 點擊兩次back退出程式
- 點擊按鈕後在狀態列顯示通知實現
- 用代碼來布局控制項的實現
. ---<< 001. 點擊兩次back退出程式 >>-----
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(), "再按一次退出程式", 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. 點擊按鈕後在狀態列顯示通知實現 >>-----
效果:
- 首先點擊第一幅圖的“Display Notification”按鈕,接著在狀態列出現一條通知,如螢幕頂部所示。
- 將狀態列往下拉,可以看到第二幅圖顯示的資訊。
- 點擊第二幅圖中的通知,會跳轉到第三幅圖中的 activity。
實現:
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 }
NotificationView.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 }
原始碼下載:Notifications.zip
. ---<< 003. 用代碼來布局控制項的實現 >>-----
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 }
原始碼下載:UICode.zip
. ---<< 004. 點擊兩次back退出程式 >>----- . ---<< 005. 點擊兩次back退出程式 >>----- . ---<< 006. 點擊兩次back退出程式 >>-----