. ---<< 目录 >>-----

  1. 点击两次back退出程序
  2. 点击按钮后在状态栏显示通知实现
  3. 用代码来布局控件的实现

. ---<< 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. 点击按钮后在状态栏显示通知实现 >>-----

效果:
  1. 首先点击第一幅图的“Display Notification”按钮,接着在状态栏出现一条通知,如屏幕顶部所示。
  2. 将状态栏往下拉,可以看到第二幅图显示的信息。
  3. 点击第二幅图中的通知,会跳转到第三幅图中的 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退出程序 >>-----

更多相关文章

  1. 【Browser】浏览器横屏时程序崩溃
  2. Android添加多语言
  3. Android(安卓)Studio——点击事件监听
  4. Android超简单的进度条源码
  5. android网络-GoogleMap之GPS定位
  6. Android使用ProgressBar简单实现进度条
  7. 新浪微博Android客户端学习记录三:完成登录界面和授权界面
  8. android 设置程序安装位置
  9. Android:requestWindowFeature应用程序窗体显示状态操作

随机推荐

  1. Android(安卓)2.3 API改变大全
  2. Android第三方登录-----微信登录接入方法
  3. Android比较好的10个开源框架
  4. [Android(安卓)問題] Make/Build Error:
  5. android 驱动文件权限设置
  6. Android(安卓)开发笔记1
  7. Android(安卓)Studio-Gradle项目中添加li
  8. 3G定时开关 for android
  9. Android(安卓)开发者必备的十个工具(上)
  10. android 模拟器中启用相机API支持