[Android] 入门笔记

1.上下文

包括当前上下文  Actity 继承自Context

和全局上下文  this.getApplicationContext();

两者生命周期不同;



2.资源的获取

layout文件中 通过@资源类型/资源名

》java类中 通过R.资源类型.资源ID       ===R类是资源的父类



3.UI线程阻塞及解决

通常通过继承AsyncTask类 实现其doInBackground方法

界面控件调用Post方法实现多线程


  1. @Override       
  2.     public void onClick(View view)       
  3. {       
  4.     // 异步执行调用WebService的任务         
  5.         new WSAsyncTask().execute();       
  6.     }  


  1. class WSAsyncTask extends AsyncTask       
  2.     {       
  3.         String result = "";       
  4.         @Override       
  5.         protected Object doInBackground(Object... params)       
  6.         {       
  7.             try       
  8.             {       
  9.                      ............................
  10.             }       
  11.             catch (Exception e)       
  12.             {       
  13.                 result = "调用WebService错误.";       
  14.             }      
  15.          }
  16.     }      



在开发Android应用时必须遵守单线程模型的原则:Android UI操作并不是线程安全的并且这些操作必须在UI线程中执行。在单线程模型中始终要记住两条法则:

  1.不要阻塞UI线程
  2.确保只在UI线程中访问Android UI工具包

  当一个程序第一次启动时,Android会同时启动一个对应的主线程(Main Thread),主线程主要负责处理与UI相关的事件,如:用户的按键事件,用户接触屏幕的事件以及屏幕绘图事件,并把相关的事件分发到对应的组件进行处 理。所以主线程通常又被叫做UI线程。

  比如说从网上获取一个网页,在一个TextView中将其源代码显示出来,这种涉及到网络操作的程序一般都是需要开一个线程完成网络访问,但是 在获得页面源码后,是不能直接在网络操作线程中调TextView.setText()的因为其他线程中是不能直接访问主UI线程成员。

  Android提供了几种在其他线程中访问UI线程的方法。

  java代码:
  Activity.runOnUiThread( Runnable )
  View.post( Runnable )
  View.postDelayed( Runnable, long )

  Hanlder
  这些类或方法同样会使你的代码很复杂很难理解。然而当你需要实现一些很复杂的操作并需要频繁地更新UI时这会变得更糟糕。

  为了解决这个问题,Android 1.5提供了一个工具类:AsyncTask,它使创建需要与用户界面交互的长时间运行的任务变得更简单。不需要借助线程和Handler即可实现。

  AsyncTask是抽象类.AsyncTask定义了三种泛型类型 Params,Progress和Result。
  Params 启动任务执行的输入参数,比如HTTP请求的URL。
  Progress 后台任务执行的百分比。
  Result 后台执行任务最终返回的结果,比如String。

  AsyncTask的执行分为四个步骤,每一步都对应一个回调方法,这些方法不应该由应用程序调用,开发者需要做的就是实现这些方法。

  1) 子类化AsyncTask
  2) 实现AsyncTask中定义的下面一个或几个方法

  onPreExecute(), 该方法将在执行实际的后台操作前被UI thread调用。可以在该方法中做一些准备工作,如在界面上显示一个进度条。

  doInBackground(Params...), 将在onPreExecute 方法执行后马上执行,该方法运行在后台线程中。这里将主要负责执行那些很耗时的后台计算工作。可以调用publishProgress方法来更新实时的任 务进度。该方法是抽象方法,子类必须实现。

  onProgressUpdate(Progress...),在publishProgress方法被调用后,UI thread将调用这个方法从而在界面上展示任务的进展情况,例如通过一个进度条进行展示。

  onPostExecute(Result), 在doInBackground 执行完成后,onPostExecute 方法将被UI thread调用,后台的计算结果将通过该方法传递到UI thread。

  为了正确的使用AsyncTask类,以下是几条必须遵守的准则:

  1) Task的实例必须在UI thread中创建
  2) execute方法必须在UI thread中调用
  3) 不要手动的调用onPreExecute(), onPostExecute(Result),doInBackground(Params...), onProgressUpdate(Progress...)这几个方法
  4) 该task只能被执行一次,否则多次调用时将会出现异常

 

 

 

 

4.Toast 提示消息

5.Notifycation 通知栏消息

Android Notification通知详解
002  
003 根据activity的生命周期,在activity不显示时,会执行onStop函数(比如按下home键),所以你在onStop函数(按退出键除外)里面把notification放在通知栏里,再此显示时,把notification从通知栏里去掉。或者,只要程序在运行就一直显示通知栏图标。
004  
005          
006  
007 下面对Notification类中的一些常量,字段,方法简单介绍一下:
008 常量:
009 DEFAULT_ALL    使用所有默认值,比如声音,震动,闪屏等等
010 DEFAULT_LIGHTS 使用默认闪光提示
011 DEFAULT_SOUNDS 使用默认提示声音
012 DEFAULT_VIBRATE 使用默认手机震动
013 【说明】:加入手机震动,一定要在manifest.xml中加入权限:
014 <uses-permission android:name="android.permission.VIBRATE" />
015 以上的效果常量可以叠加,即通过
016 notification.defaults =DEFAULT_SOUND|DEFAULT_VIBRATE; 
017 notification.defaults |= DEFAULT_SOUND (最好在真机上测试,震动效果模拟器上没有)
018  
019              
020  
021 //设置flag位
022 FLAG_AUTO_CANCEL  该通知能被状态栏的清除按钮给清除掉
023 FLAG_NO_CLEAR     该通知能被状态栏的清除按钮给清除掉
024 FLAG_ONGOING_EVENT 通知放置在正在运行
025 FLAG_INSISTENT 是否一直进行,比如音乐一直播放,知道用户响应
026  
027            
028  
029 常用字段:
030 contentIntent  设置PendingIntent对象,点击时发送该Intent
031 defaults 添加默认效果
032 flags 设置flag位,例如FLAG_NO_CLEAR等
033 icon 设置图标
034 sound 设置声音
035 tickerText 显示在状态栏中的文字
036 when 发送此通知的时间戳
037  
038                  
039  
040 NotificationManager常用方法介绍:
041 public void cancelAll() 移除所有通知(只是针对当前Context下的Notification)
042 public  void cancel(int id) 移除标记为id的通知 (只是针对当前Context下的所有Notification)
043 public  void notify(String tag ,int id, Notification notification) 将通知加入状态栏,标签为tag,标记为id
044 public  void notify(int id, Notification notification) 将通知加入状态栏,标记为id
045  
046               
047  
048 ?
049 package com.ljq.activity;
050   
051 import android.app.Activity;
052 import android.app.Notification;
053 import android.app.NotificationManager;
054 import android.app.PendingIntent;
055 import android.content.Intent;
056 import android.graphics.Color;
057 import android.os.Bundle;
058   
059 public class MainActivity extends Activity {
060     /** Called when the activity is first created. */
061     @Override
062     public void onCreate(Bundle savedInstanceState) {
063         super.onCreate(savedInstanceState);
064         setContentView(R.layout.main);
065         clearNotification();
066     }
067       
068     @Override
069     protected void onStop() {
070         showNotification();
071         super.onStop();
072     }
073       
074     @Override
075     protected void onStart() {
076         clearNotification();
077         super.onStart();
078     }
079       
080     /**
081      * 在状态栏显示通知
082      */
083     private void showNotification(){
084         // 创建一个NotificationManager的引用  
085         NotificationManager notificationManager = (NotificationManager)   
086             this.getSystemService(android.content.Context.NOTIFICATION_SERVICE);  
087           
088         // 定义Notification的各种属性  
089         Notification notification =new Notification(R.drawable.icon,  
090                 "督导系统", System.currentTimeMillis());
091         //FLAG_AUTO_CANCEL   该通知能被状态栏的清除按钮给清除掉
092         //FLAG_NO_CLEAR      该通知不能被状态栏的清除按钮给清除掉
093         //FLAG_ONGOING_EVENT 通知放置在正在运行
094         //FLAG_INSISTENT     是否一直进行,比如音乐一直播放,知道用户响应
095         notification.flags |= Notification.FLAG_ONGOING_EVENT; // 将此通知放到通知栏的"Ongoing"即"正在运行"组中  
096         notification.flags |= Notification.FLAG_NO_CLEAR; // 表明在点击了通知栏中的"清除通知"后,此通知不清除,经常与FLAG_ONGOING_EVENT一起使用  
097         notification.flags |= Notification.FLAG_SHOW_LIGHTS;  
098         //DEFAULT_ALL     使用所有默认值,比如声音,震动,闪屏等等
099         //DEFAULT_LIGHTS  使用默认闪光提示
100         //DEFAULT_SOUNDS  使用默认提示声音
101         //DEFAULT_VIBRATE 使用默认手机震动,需加上<uses-permission android:name="android.permission.VIBRATE" />权限
102         notification.defaults = Notification.DEFAULT_LIGHTS;
103         //叠加效果常量
104         //notification.defaults=Notification.DEFAULT_LIGHTS|Notification.DEFAULT_SOUND;
105         notification.ledARGB = Color.BLUE;  
106         notification.ledOnMS =5000//闪光时间,毫秒
107           
108         // 设置通知的事件消息  
109         CharSequence contentTitle ="督导系统标题"// 通知栏标题  
110         CharSequence contentText ="督导系统内容"// 通知栏内容  
111         Intent notificationIntent =new Intent(MainActivity.this, MainActivity.class);// 点击该通知后要跳转的Activity  
112         PendingIntent contentItent = PendingIntent.getActivity(this0, notificationIntent, 0);  
113         notification.setLatestEventInfo(this, contentTitle, contentText, contentItent);  
114           
115         // 把Notification传递给NotificationManager  
116         notificationManager.notify(0, notification);  
117     }
118 ?
119     //删除通知   
120     private void clearNotification(){
121         // 启动后删除之前我们定义的通知  
122         NotificationManager notificationManager = (NotificationManager) this
123                 .getSystemService(NOTIFICATION_SERVICE);  
124         notificationManager.cancel(0); 
125   
126     }
127 }

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值