alex_bn_lee

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

【070】Android 中相关功能的实现代码

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

  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 selects
18         // 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 and
39         // 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退出程序 >>-----

 

 

 

posted on   McDelfino  阅读(293)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示