2.安裝cnpm用cnpm替代npm
地址:http://npm.taobao.org/
PostConstruct注釋用于在完成依賴項(xiàng)注入以執(zhí)行任何初始化之后需要執(zhí)行的方法。必須在類投入使用之前調(diào)用此方法。 所有支持依賴注入的類都必須支持此注釋。即使類沒有請(qǐng)求注入任何資源,也必須調(diào)用使用PostConstruct注釋的方法。 只有一個(gè)方法可以使用此批注進(jìn)行批注。 應(yīng)用PostConstruct注釋的方法必須滿足以下所有條件:除了攔截器之外,方法絕不能有任何參數(shù),在這種情況下它采用Interceptor規(guī)范定義的InvocationContext對(duì)象。 在攔截器類上定義的方法必須具有以下簽名之一: void <METHOD>(InvocationContext)Object <METHOD>(InvocationContext)拋出異常注意: PostConstruct攔截器方法不能拋出應(yīng)用程序異常,但可以聲明它拋出檢查異常,包括java.lang.Exception, 如果相同的攔截器方法除了生命周期事件之外插入業(yè)務(wù)或超時(shí)方法。 如果PostConstruct攔截器方法返回一個(gè)值,容器將忽略它。 在非攔截器類上定義的方法必須具有以下簽名:void <METHOD>()應(yīng)用PostConstruct的方法可以是public,protected,package private或private。 除應(yīng)用程序客戶端外,該方法絕不能是靜態(tài)的。 該方法可能是最終的。如果該方法拋出一個(gè)未經(jīng)檢查的異常,那么該類絕不能投入使用,除非EJB可以處理異常甚至從它們恢復(fù)的EJB
然后就會(huì)思考問題,這個(gè)注釋是修飾初始化之后需要執(zhí)行的方法,那么它和@Autowired、構(gòu)造函數(shù)的執(zhí)行順序是什么呢?(當(dāng)然注釋中已經(jīng)說明了PostConstruct注釋用于在完成依賴項(xiàng)注入之后)
public class BeanA {
@Autowired
private BeanB beanB;
public BeanA() {
System.out.println("這是Bean A 的構(gòu)造方法");
}
@PostConstruct
private void init() {
System.out.println("這是BeanA的 init 方法");
beanB.testB();
}
}
public class BeanB {
@PostConstruct
private void init() {
System.out.println("這是BeanB 的init 方法");
}
public BeanB() {
System.out.println("這是Bean B的 構(gòu)造方法");
}
void testB() {
System.out.println("這是Bean B 的 testB 方法");
}
}
啟動(dòng)后輸出:
這是Bean A 的構(gòu)造方法
這是Bean B的 構(gòu)造方法
這是BeanB 的init 方法
這是BeanA的 init 方法
這是Bean B 的 testB 方法
所以得到結(jié)論: 構(gòu)造方法 > @Autowired > @PostConstruct
2、ApplicationEvent
是個(gè)抽象類,里面只有一個(gè)構(gòu)造函數(shù)和一個(gè)長整型的timestamp。其源碼如下
public abstract class ApplicationEvent extends EventObject {
/** use serialVersionUID from Spring 1.2 for interoperability */
private static final long serialVersionUID = 7099057708183571937L;
/** System time when the event happened */
private final long timestamp;
/**
* Create a new ApplicationEvent.
* @param source the object on which the event initially occurred (never {@code null})
*/
public ApplicationEvent(Object source) {
super(source);
this.timestamp = System.currentTimeMillis();
}
/**
* Return the system time in milliseconds when the event happened.
*/
public final long getTimestamp() {
return this.timestamp;
}
}
3、ApplicationListener
public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {
/**
* Handle an application event.
* @param event the event to respond to
*/
void onApplicationEvent(E event);
}
自定義事件NotifyEvent:
public class NotifyEvent extends ApplicationEvent {
private String email;
private String content;
public NotifyEvent(Object source){
super(source);
}
public NotifyEvent(Object source,String email,String content){
super(source);
this.email = email;
this.content = content;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
定義監(jiān)聽器NotifyListener:
import org.springframework.context.annotation.Configuration;
@Configuration
public class NotifyListener implements ApplicationListener<NotifyEvent>{
@Override
public void onApplicationEvent(NotifyEvent event) {
System.out.println("郵件地址:" + event.getEmail());
System.out.println("郵件內(nèi)容:" + event.getContent());
}
}
單元測試類ListenerTest:
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.context.WebApplicationContext;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ServerLauncher.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ListenerTest {
@Autowired
private WebApplicationContext webApplicationContext;
@Test
public void testListener(){
NotifyEvent event = new NotifyEvent("object","abc@qq.com","This is the content");
webApplicationContext.publishEvent(event);
}
}
之前用戶使用的是3個(gè)注解注解他們的main類。分別是@Configuration,@EnableAutoConfiguration,@ComponentScan。由于這些注解一般都是一起使用,spring boot提供了一個(gè)統(tǒng)一的注解@SpringBootApplication。
@SpringBootApplication = (默認(rèn)屬性)@Configuration + @EnableAutoConfiguration + @ComponentScan。
public class ApplicationMain {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
分開解釋@Configuration,@EnableAutoConfiguration,@ComponentScan。
1、@Configuration:提到@Configuration就要提到他的搭檔@Bean。使用這兩個(gè)注解就可以創(chuàng)建一個(gè)簡單的spring配置類,可以用來替代相應(yīng)的xml配置文件。
<bean id = "car" class="com.test.Car">
<property name="wheel" ref = "wheel"></property>
</bean>
<bean id = "wheel" class="com.test.Wheel"></bean>
</beans>
相當(dāng)于:
public class Conf {
@Bean
public Car car() {
Car car = new Car();
car.setWheel(wheel());
return car;
}
@Bean
public Wheel wheel() {
return new Wheel();
}
}
@Configuration的注解類標(biāo)識(shí)這個(gè)類可以使用Spring IoC容器作為bean定義的來源。@Bean注解告訴Spring,一個(gè)帶有@Bean的注解方法將返回一個(gè)對(duì)象,該對(duì)象應(yīng)該被注冊(cè)為在Spring應(yīng)用程序上下文中的bean。
2、@EnableAutoConfiguration:能夠自動(dòng)配置spring的上下文,試圖猜測和配置你想要的bean類,通常會(huì)自動(dòng)根據(jù)你的類路徑和你的bean定義自動(dòng)配置。
3、@ComponentScan:會(huì)自動(dòng)掃描指定包下的全部標(biāo)有@Component的類,并注冊(cè)成bean,當(dāng)然包括@Component下的子注解@Service,@Repository,@Controller。



btn_search.xml
轉(zhuǎn)載:http://www.cnblogs.com/allenzheng/archive/2013/04/28/3050065.html
當(dāng)應(yīng)用運(yùn)行起來后就會(huì)開啟一條線程,線程中會(huì)運(yùn)行一個(gè)任務(wù)棧,當(dāng)Activity實(shí)例創(chuàng)建后就會(huì)放入任務(wù)棧中。Activity啟動(dòng)模式的設(shè)置在AndroidManifest.xml文件中,通過配置Activity的屬性android:launchMode=""設(shè)置。
1. Standared模式(默認(rèn))
我們平時(shí)直接創(chuàng)建的Activity都是這種模式的Activity,這種模式的Activity的特點(diǎn)是:只要你創(chuàng)建了Activity實(shí)例,一旦激活該Activity,則會(huì)向任務(wù)棧中加入新創(chuàng)建的實(shí)例,退出Activity則會(huì)在任務(wù)棧中銷毀該實(shí)例。
2. SingleTop模式
這種模式會(huì)考慮當(dāng)前要激活的Activity實(shí)例在任務(wù)棧中是否正處于棧頂,如果處于棧頂則無需重新創(chuàng)建新的實(shí)例,會(huì)重用已存在的實(shí)例,否則會(huì)在任務(wù)棧中創(chuàng)建新的實(shí)例。
3. SingleTask模式
如果任務(wù)棧中存在該模式的Activity實(shí)例,則把棧中該實(shí)例以上的Activity實(shí)例全部移除,調(diào)用該實(shí)例的newInstance()方法重用該Activity,使該實(shí)例處於棧頂位置,否則就重新創(chuàng)建一個(gè)新的Activity實(shí)例。
4. SingleInstance模式
當(dāng)該模式Activity實(shí)例在任務(wù)棧中創(chuàng)建后,只要該實(shí)例還在任務(wù)棧中,即只要激活的是該類型的Activity,都會(huì)通過調(diào)用實(shí)例的newInstance()方法重用該Activity,此時(shí)使用的都是同一個(gè)Activity實(shí)例,它都會(huì)處于任務(wù)棧的棧頂。此模式一般用于加載較慢的,比較耗性能且不需要每次都重新創(chuàng)建的Activity。
摘要: android中跨進(jìn)程通訊的4種方式 轉(zhuǎn)自:http://www.cnblogs.com/sevenyuan/archive/2013/03/22/2975122.html由于android系統(tǒng)中應(yīng)用程序之間不能共享內(nèi)存。因此,在不同應(yīng)用程序之間交互數(shù)據(jù)(跨進(jìn)程通訊)就稍微麻煩一些。在android SDK中提供了4種用于跨進(jìn)程通訊的方式。這4種方式正好對(duì)應(yīng)于android系統(tǒng)中4種應(yīng)用... 閱讀全文Android 手機(jī)上的應(yīng)用一般情況下都在一個(gè)進(jìn)程中運(yùn)行。
但是,也可以指定Activity或者Service在Remote 進(jìn)程中執(zhí)行。多數(shù)情況下,只有在用戶認(rèn)為應(yīng)用退出后還需要繼續(xù)后臺(tái)長期運(yùn)行的應(yīng)用,才需要這樣做。此時(shí),該應(yīng)用有兩個(gè)進(jìn)程。
還有一種hack的方式,在apk中通過調(diào)用命令行來啟動(dòng)另外的進(jìn)程。此種方式用戶不可見,也不安全。不提倡。
官網(wǎng)幫助文檔鏈接:
http://developer.android.com/guide/components/fragments.html
主要看兩張圖,和跑代碼
一,F(xiàn)ragment的生命周
二,與Activity生命周期的對(duì)比
場景演示 : 切換到該Fragment
11-29 14:26:35.095: D/AppListFragment(7649): onAttach
11-29 14:26:35.095: D/AppListFragment(7649): onCreate
11-29 14:26:35.095: D/AppListFragment(7649): onCreateView
11-29 14:26:35.100: D/AppListFragment(7649): onActivityCreated
11-29 14:26:35.120: D/AppListFragment(7649): onStart
11-29 14:26:35.120: D/AppListFragment(7649): onResume
屏幕滅掉:
11-29 14:27:35.185: D/AppListFragment(7649): onPause
11-29 14:27:35.205: D/AppListFragment(7649): onSaveInstanceState
11-29 14:27:35.205: D/AppListFragment(7649): onStop
屏幕解鎖
11-29 14:33:13.240: D/AppListFragment(7649): onStart
11-29 14:33:13.275: D/AppListFragment(7649): onResume
切換到其他Fragment:
11-29 14:33:33.655: D/AppListFragment(7649): onPause
11-29 14:33:33.655: D/AppListFragment(7649): onStop
11-29 14:33:33.660: D/AppListFragment(7649): onDestroyView
切換回本身的Fragment:
11-29 14:33:55.820: D/AppListFragment(7649): onCreateView
11-29 14:33:55.825: D/AppListFragment(7649): onActivityCreated
11-29 14:33:55.825: D/AppListFragment(7649): onStart
11-29 14:33:55.825: D/AppListFragment(7649): onResume
回到桌面
11-29 14:34:26.590: D/AppListFragment(7649): onPause
11-29 14:34:26.880: D/AppListFragment(7649): onSaveInstanceState
11-29 14:34:26.880: D/AppListFragment(7649): onStop
回到應(yīng)用
11-29 14:36:51.940: D/AppListFragment(7649): onStart
11-29 14:36:51.940: D/AppListFragment(7649): onResume
退出應(yīng)用
11-29 14:37:03.020: D/AppListFragment(7649): onPause
11-29 14:37:03.155: D/AppListFragment(7649): onStop
11-29 14:37:03.155: D/AppListFragment(7649): onDestroyView
11-29 14:37:03.165: D/AppListFragment(7649): onDestroy
11-29 14:37:03.165: D/AppListFragment(7649): onDetach
比Activity多了一些生命周期,完整和Activity對(duì)接上,大家好好利用。
轉(zhuǎn)載:http://blog.csdn.net/forever_crying/article/details/8238863/
ANR定義:在Android上,如果你的應(yīng)用程序有一段時(shí)間響應(yīng)不夠靈敏,系統(tǒng)會(huì)向用戶顯示一個(gè)對(duì)話框,這個(gè)對(duì)話框稱作應(yīng)用程序無響應(yīng)對(duì)話框(ANR:Application Not Responding),用戶可以選擇“等待”讓應(yīng)用程序繼續(xù)運(yùn)行,也可以選擇“強(qiáng)制關(guān)閉”。所以一個(gè)順暢合理的應(yīng)用程序不會(huì)出現(xiàn)ANR,而讓用戶處理這個(gè)對(duì)話框。因此,在程序里對(duì)響應(yīng)性能的設(shè)計(jì)很重要,這樣系統(tǒng)不會(huì)顯示ANR給用戶。
默認(rèn)情況下,Android的Activity執(zhí)行時(shí)間為5s,BroadcastReceiver的最長執(zhí)行時(shí)間為10s.
第一,什么會(huì)引發(fā)ANR
在Android里,應(yīng)用程序響應(yīng)由Activity Manager和WindowManager系統(tǒng)服務(wù)監(jiān)視的,當(dāng)它監(jiān)聽到一下一種情況時(shí),Android就會(huì)針對(duì)特定的應(yīng)用程序顯示ANR:
1).在5秒內(nèi)沒有響應(yīng)輸入事件(例如,按鍵按下,屏幕觸摸)
2).BroadcastReceiver在10秒內(nèi)沒有執(zhí)行完畢
造成以上兩點(diǎn)多原因有很多,比如在主線程中做非常耗時(shí)的操作,比如下載,IO異常等。
潛在的耗時(shí)操作,例如網(wǎng)絡(luò)或數(shù)據(jù)庫操作或者高耗時(shí)的計(jì)算如改變位圖尺寸,這些操作應(yīng)該放在子線程中(或者以數(shù)據(jù)庫為例,通過異步請(qǐng)求的方式)來完成,然而,不是說你的主線程阻塞在那里等待子線程來完成--也不用調(diào)用Thread.wait()或Thread.sleep();替代的方法是主線程需要為子線程提供一個(gè)handler,以便完成時(shí)能夠交給主線程,以這種方式設(shè)計(jì)你的應(yīng)用程序,將能保證你的主線程保持對(duì)輸入的響應(yīng)性并能避免由于5秒輸入事件的超時(shí)引發(fā)的ANR對(duì)話框。
第二,如何避免ANR
1.運(yùn)行在主線程里的任何方法都盡可能少做事情。特別是,Activity應(yīng)該在它的關(guān)鍵生命周期方法(如onCreate()和onResume())里盡可能少的去做創(chuàng)建操作。(可以采用重新開啟子線程的方式,然后使用Handler+Message的方式做一些操作,比如更新主線程中的ui等)
2.應(yīng)用程序應(yīng)該避免在BroadcastReceiver里做耗時(shí)的操作或計(jì)算。但不再是在子線程里做這些任務(wù)(因?yàn)?BroadcastReceiver的生命周期短),替代的是,如果響應(yīng)Intent廣播需要執(zhí)行一個(gè)耗時(shí)的動(dòng)作的話,應(yīng)用程序應(yīng)該啟動(dòng)一個(gè) Service。(此處需要注意的是可以在廣播接受者中啟動(dòng)Service,但是卻不可以在Service中啟動(dòng)broadcasereciver,關(guān)于原因后續(xù)會(huì)有介紹,此處不是本文重點(diǎn))
3.避免在Intent Receiver里啟動(dòng)一個(gè)Activity,因?yàn)樗鼤?huì)創(chuàng)建一個(gè)新的畫面,并從當(dāng)前用戶正在運(yùn)行的程序上搶奪焦點(diǎn)。如果你的應(yīng)用程序在響應(yīng)Intent廣 播時(shí)需要向用戶展示什么,你應(yīng)該使用Notification Manager來實(shí)現(xiàn)。
總結(jié):anr異常也是在程序中自己經(jīng)常遇到的問題,主要的解決辦法自己最常用的就是不要在主線程中做耗時(shí)的操作,而應(yīng)放在子線程中來實(shí)現(xiàn),比如采用Handler+mesage的方式,或者是有時(shí)候需要做一些和網(wǎng)絡(luò)相互交互的耗時(shí)操作就采用asyntask異步任務(wù)的方式(它的底層其實(shí)Handler+mesage有所區(qū)別的是它是線程池)等,在主線程中更新UI。
java.version
Java 運(yùn)行時(shí)環(huán)境版本
java.vendor
Java 運(yùn)行時(shí)環(huán)境供應(yīng)商
java.vendor.url
Java 供應(yīng)商的 URL
java.home
Java 安裝目錄
java.vm.specification.version
Java 虛擬機(jī)規(guī)范版本
java.vm.specification.vendor
Java 虛擬機(jī)規(guī)范供應(yīng)商
java.vm.specification.name
Java 虛擬機(jī)規(guī)范名稱
java.vm.version
Java 虛擬機(jī)實(shí)現(xiàn)版本
java.vm.vendor
Java 虛擬機(jī)實(shí)現(xiàn)供應(yīng)商
java.vm.name
Java 虛擬機(jī)實(shí)現(xiàn)名稱
java.specification.version
Java 運(yùn)行時(shí)環(huán)境規(guī)范版本
java.specification.vendor
Java 運(yùn)行時(shí)環(huán)境規(guī)范供應(yīng)商
java.specification.name
Java 運(yùn)行時(shí)環(huán)境規(guī)范名稱
java.class.version
Java 類格式版本號(hào)
java.class.path
Java 類路徑
java.library.path
加載庫時(shí)搜索的路徑列表
java.io.tmpdir
默認(rèn)的臨時(shí)文件路徑
java.compiler
要使用的 JIT 編譯器的名稱
java.ext.dirs
一個(gè)或多個(gè)擴(kuò)展目錄的路徑
os.name
操作系統(tǒng)的名稱
os.arch
操作系統(tǒng)的架構(gòu)
os.version
操作系統(tǒng)的版本
file.separator
文件分隔符(在 UNIX 系統(tǒng)中是“/”)
path.separator
路徑分隔符(在 UNIX 系統(tǒng)中是“:”)
line.separator
行分隔符(在 UNIX 系統(tǒng)中是“/n”)
user.name
用戶的賬戶名稱
user.home
用戶的主目錄
user.dir
用戶的當(dāng)前工作目錄
while ((line = reader.readLine()) != null) {
response.append(line).append(
System.getProperty("line.separator"));
}
public class SystemProperty {
public static void main(String args[]) {
System.out.println("java_vendor:" + System.getProperty("java.vendor"));
System.out.println("java_vendor_url:"
+ System.getProperty("java.vendor.url"));
System.out.println("java_home:" + System.getProperty("java.home"));
System.out.println("java_class_version:"
+ System.getProperty("java.class.version"));
System.out.println("java_class_path:"
+ System.getProperty("java.class.path"));
System.out.println("os_name:" + System.getProperty("os.name"));
System.out.println("os_arch:" + System.getProperty("os.arch"));
System.out.println("os_version:" + System.getProperty("os.version"));
System.out.println("user_name:" + System.getProperty("user.name"));
System.out.println("user_home:" + System.getProperty("user.home"));
System.out.println("user_dir:" + System.getProperty("user.dir"));
System.out.println("java_vm_specification_version:"
+ System.getProperty("java.vm.specification.version"));
System.out.println("java_vm_specification_vendor:"
+ System.getProperty("java.vm.specification.vendor"));
System.out.println("java_vm_specification_name:"
+ System.getProperty("java.vm.specification.name"));
System.out.println("java_vm_version:"
+ System.getProperty("java.vm.version"));
System.out.println("java_vm_vendor:"
+ System.getProperty("java.vm.vendor"));
System.out
.println("java_vm_name:" + System.getProperty("java.vm.name"));
System.out.println("java_ext_dirs:"
+ System.getProperty("java.ext.dirs"));
System.out.println("file_separator:"
+ System.getProperty("file.separator"));
System.out.println("path_separator:"
+ System.getProperty("path.separator"));
System.out.println("line_separator:"
+ System.getProperty("line.separator"));
}
轉(zhuǎn)載:http://blog.csdn.net/kongqz/article/details/3987198 當(dāng)某個(gè)activity變得“容易”被系統(tǒng)銷毀時(shí),該activity的onSaveInstanceState就會(huì)被執(zhí)行,除非該activity是被用戶主動(dòng)銷毀的,例如當(dāng)用戶按BACK鍵的時(shí)候。
注意上面的雙引號(hào),何為“容易”?言下之意就是該activity還沒有被銷毀,而僅僅是一種可能性。這種可能性有哪些?通過重寫一個(gè)activity的所有生命周期的onXXX方法,包括onSaveInstanceState和onRestoreInstanceState方法,我們可以清楚地知道當(dāng)某個(gè)activity(假定為activity A)顯示在當(dāng)前task的最上層時(shí),其onSaveInstanceState方法會(huì)在什么時(shí)候被執(zhí)行,有這么幾種情況:
1、當(dāng)用戶按下HOME鍵時(shí)。
這是顯而易見的,系統(tǒng)不知道你按下HOME后要運(yùn)行多少其他的程序,自然也不知道activity A是否會(huì)被銷毀,故系統(tǒng)會(huì)調(diào)用onSaveInstanceState,讓用戶有機(jī)會(huì)保存某些非永久性的數(shù)據(jù)。以下幾種情況的分析都遵循該原則
2、長按HOME鍵,選擇運(yùn)行其他的程序時(shí)。
3、按下電源按鍵(關(guān)閉屏幕顯示)時(shí)。
4、從activity A中啟動(dòng)一個(gè)新的activity時(shí)。
5、屏幕方向切換時(shí),例如從豎屏切換到橫屏?xí)r。
在屏幕切換之前,系統(tǒng)會(huì)銷毀activity A,在屏幕切換之后系統(tǒng)又會(huì)自動(dòng)地創(chuàng)建activity A,所以onSaveInstanceState一定會(huì)被執(zhí)行。
總而言之,onSaveInstanceState的調(diào)用遵循一個(gè)重要原則,即當(dāng)系統(tǒng)“未經(jīng)你許可”時(shí)銷毀了你的activity,則onSaveInstanceState會(huì)被系統(tǒng)調(diào)用,這是系統(tǒng)的責(zé)任,因?yàn)樗仨氁峁┮粋€(gè)機(jī)會(huì)讓你保存你的數(shù)據(jù)(當(dāng)然你不保存那就隨便你了)。
至于onRestoreInstanceState方法,需要注意的是,onSaveInstanceState方法和onRestoreInstanceState方法“不一定”是成對(duì)的被調(diào)用的,onRestoreInstanceState被調(diào)用的前提是,activity A“確實(shí)”被系統(tǒng)銷毀了,而如果僅僅是停留在有這種可能性的情況下,則該方法不會(huì)被調(diào)用,例如,當(dāng)正在顯示activity A的時(shí)候,用戶按下HOME鍵回到主界面,然后用戶緊接著又返回到activity A,這種情況下activity A一般不會(huì)因?yàn)閮?nèi)存的原因被系統(tǒng)銷毀,故activity A的onRestoreInstanceState方法不會(huì)被執(zhí)行。
另外,onRestoreInstanceState的bundle參數(shù)也會(huì)傳遞到onCreate方法中,你也可以選擇在onCreate方法中做數(shù)據(jù)還原。
轉(zhuǎn)載:http://gundumw100.iteye.com/blog/1115080 摘要: 所謂自定義控件(或稱組件)也就是編寫自己的控件類型,而非Android中提供的標(biāo)準(zhǔn)的控件,如TextView,CheckBox等等.不過自定義的控件一般也都是從標(biāo)準(zhǔn)控件繼承來的,或者是多種控件組合,或者是對(duì)標(biāo)準(zhǔn)控件的屬性進(jìn)行改變而得到的自己滿意的控件. 自定義控件可能會(huì)有很多種方法,這里只介紹我要介紹的方法. &nb... 閱讀全文
在Android的聯(lián)機(jī)文檔中,有對(duì)Activity的簡單介紹,現(xiàn)在通過編寫代碼對(duì)Activity的啟動(dòng)模式做一個(gè)深入的理解。
在配置文件AndroidManifest.xml中,activity元素的android:launchMode屬性用來配置對(duì)應(yīng)Activity的啟動(dòng)模式,目前有以下四種啟動(dòng)模式:
1.standard
2.singleTop
3.singleTask
4.singleInstance
如果不對(duì)Activity設(shè)置啟動(dòng)模式,默認(rèn)就是standard模式
一、standard
請(qǐng)看以下代碼,實(shí)現(xiàn)了一個(gè)Activity :
public class A_Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textView=new TextView(this);
textView.setText(this+"");//這里用于打印當(dāng)前Activity的hashcode,可以此判斷Activity實(shí)例是不是同一個(gè)對(duì)象
Button button=new Button(this);
button.setText("Go next activity");
button.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent();
intent.setClass(A_Activity.this, A_Activity.class);//說明發(fā)出Intent與啟動(dòng)的Activity都是A_Activity的實(shí)例
startActivity(intent);
}
});
LinearLayout layout=new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(textView);
layout.addView(button);
setContentView(layout);
}
}
運(yùn)行之,請(qǐng)看下圖:
點(diǎn)擊button后,注意看第一行的textView
由此可知,生成了新的A_Activity對(duì)象,這時(shí)我們按回退鍵,就會(huì)依次回到之前的Activity。點(diǎn)擊button的過程就是壓棧的過程,在standard模式下,就會(huì)不斷生成新的Activity對(duì)象
二、singleTop
singleTop和standard模式都會(huì)將intent發(fā)送給新的Activity實(shí)例,不同的是,如果創(chuàng)建Intent的時(shí)候棧頂有要?jiǎng)?chuàng)建的singleTop模式下的Activity實(shí)例,則將Intent發(fā)送給該實(shí)例,不會(huì)再創(chuàng)建Activity的新實(shí)例。
還是使用之前的代碼,只是設(shè)置A_Activity的啟動(dòng)模式為singleTop:android:launchMode="singleTop",運(yùn)行之,請(qǐng)看下圖:
這個(gè)時(shí)候我們無論點(diǎn)擊多少次button,textView都顯示同一個(gè)Activity實(shí)例,按回退鍵時(shí)會(huì)直接退出程序,表明在singleTop模式下,如果在棧頂存在Intent中那個(gè)目標(biāo)Activity的實(shí)例,就不會(huì)創(chuàng)建新的實(shí)例,而直接使用棧頂?shù)膶?duì)象,對(duì)于資源有限的移動(dòng)設(shè)備來說,也是有實(shí)際意義的。
如果是在不同Activity之間跳轉(zhuǎn),就會(huì)跟standard模式的情形一樣,請(qǐng)看下面代碼:
public class A_Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textView=new TextView(this);
textView.setText(this+"");
Button button=new Button(this);
button.setText("Go B_Activity");
button.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent();
intent.setClass(A_Activity.this, B_Activity.class);//從A跳轉(zhuǎn)到B
startActivity(intent);
}
});
LinearLayout layout=new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(textView);
layout.addView(button);
setContentView(layout);
}
}
public class B_Activity extends Activity {
/**<li>Description: </li>
*
* @param savedInstanceState
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
TextView textView=new TextView(this);
textView.setText(this+"");
Button button=new Button(this);
button.setText("Go A_Activity");
button.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent();
intent.setClass(B_Activity.this, A_Activity.class);//從B跳轉(zhuǎn)到A
startActivity(intent);
}
});
LinearLayout layout=new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(textView);
layout.addView(button);
setContentView(layout);
}
}
運(yùn)行后,如下圖:
點(diǎn)擊button后:
再點(diǎn)擊button后:
這樣每次都會(huì)創(chuàng)建目標(biāo)Activity的新實(shí)例,因?yàn)樵谔D(zhuǎn)時(shí),處于棧頂?shù)膶?duì)象不是目標(biāo)Activity的實(shí)例
三、singleTask
singleTask模式只能創(chuàng)建一個(gè)實(shí)例,當(dāng)發(fā)送一個(gè)Intent,目標(biāo)Activity為singleTask模式時(shí),系統(tǒng)會(huì)檢查棧里面是否已經(jīng)有該Activity的實(shí)例,如果有就直接將Intent發(fā)送給它,還是使用(二)中的代碼,將A_Activity啟動(dòng)模式設(shè)置為singleTask,B_Activity啟動(dòng)模式設(shè)置為standard,啟動(dòng)后如下圖:
點(diǎn)擊button后:
繼續(xù)點(diǎn)擊button:
由此可知,singleTask模式的A_Activity在棧中只有一個(gè)實(shí)例,可以被重復(fù)使用
并且,如果收到Intent生成一個(gè)新實(shí)例,那么用戶可以通過回退鍵回到上一個(gè)狀態(tài),如果是已經(jīng)存在的一個(gè)activity來處理這個(gè)Intent的話,就無法通過回退鍵回到上一個(gè)狀態(tài)(對(duì)singleInstance同樣適用) ,比如剛才最后一步如果再按回退鍵,就會(huì)直接退出程序,而不會(huì)回到上一步的狀態(tài)。
四、singleInstance
這個(gè)模式下的Activity在一個(gè)單獨(dú)的task棧中,這個(gè)棧也只能包含一個(gè)Activity的實(shí)例,將上面代碼稍微改動(dòng)一下,顯示taskId:
A_Activity 中:textView.setText(this.getTaskId()+"");
B_Activity 中:textView.setText(this.getTaskId()+"");
另外將B_Activity 設(shè)置為singleInstance模式,A_Activity 設(shè)置為standard模式,啟動(dòng)后:
點(diǎn)擊button后:
表明啟動(dòng)了新的task
總結(jié)四個(gè)模式的不同:
1、Intent的目標(biāo)Activity由哪個(gè)task持有
standard與singleTop的Activity所在task,與收到的Intent的發(fā)送者所在task相同,除非Intent包括參數(shù)FLAG_ACTIVITY_NEW_TASK,該參數(shù)會(huì)啟動(dòng)Activity到新的task中;singleTask和singleInstance總是把Activity作為一個(gè)task的根元素,它們不會(huì)被啟動(dòng)到其他task里
2、是否允許Activity的多個(gè)實(shí)例
standard與singleTop可以被實(shí)例化多次,可以存在不同task中,并且一個(gè)task可以包括同一activity的多個(gè)實(shí)例
singleTask與singleInstance則在同一個(gè)task中只能允許Activity的一個(gè)實(shí)例,并且是task的根元素
3、在同一個(gè)task棧中,是否允許其他Activity的實(shí)例存在
singleInstance單獨(dú)在一個(gè)task中,其他啟動(dòng)模式允許不同Activity的實(shí)例存在
4、是否每次生成新實(shí)例接收Intent
standard每次啟動(dòng)都會(huì)生成新實(shí)例
singleTop的activity如果在task的棧頂?shù)脑挘瑒t不生成新的activity實(shí)例,直接使用該實(shí)例,否則,就要生成新的實(shí)例
singleInstance在所在棧中是唯一的activity,它每次都會(huì)被重用
singleTask如果task棧中有該模式的Activity,就不生成新的activity實(shí)例,直接使用該實(shí)例,否則,就要生成新的實(shí)例
轉(zhuǎn)載:http://blog.csdn.net/leiswpu/article/details/6248528
1. reference:參考某一資源ID。
(1)屬性定義:
<declare-styleable name = "名稱">
<attr name = "background" format = "reference" />
</declare-styleable>
(2)屬性使用:
<ImageView
android:layout_width = "42dip"
android:layout_height = "42dip"
android:background = "@drawable/圖片ID"
/>
2. color:顏色值。
(1)屬性定義:
<declare-styleable name = "名稱">
<attr name = "textColor" format = "color" />
</declare-styleable>
(2)屬性使用:
<TextView
android:layout_width = "42dip"
android:layout_height = "42dip"
android:textColor = "#00FF00"
/>
3. boolean:布爾值。
(1)屬性定義:
<declare-styleable name = "名稱">
<attr name = "focusable" format = "boolean" />
</declare-styleable>
(2)屬性使用:
<Button
android:layout_width = "42dip"
android:layout_height = "42dip"
android:focusable = "true"
/>
4. dimension:尺寸值。
(1)屬性定義:
<declare-styleable name = "名稱">
<attr name = "layout_width" format = "dimension" />
</declare-styleable>
(2)屬性使用:
<Button
android:layout_width = "42dip"
android:layout_height = "42dip"
/>
5. float:浮點(diǎn)值。
(1)屬性定義:
<declare-styleable name = "AlphaAnimation">
<attr name = "fromAlpha" format = "float" />
<attr name = "toAlpha" format = "float" />
</declare-styleable>
(2)屬性使用:
<alpha
android:fromAlpha = "1.0"
android:toAlpha = "0.7"
/>
6. integer:整型值。
(1)屬性定義:
<declare-styleable name = "AnimatedRotateDrawable">
<attr name = "visible" />
<attr name = "frameDuration" format="integer" />
<attr name = "framesCount" format="integer" />
<attr name = "pivotX" />
<attr name = "pivotY" />
<attr name = "drawable" />
</declare-styleable>
(2)屬性使用:
<animated-rotate
xmlns:android = "
android:drawable = "@drawable/圖片ID"
android:pivotX = "50%"
android:pivotY = "50%"
android:framesCount = "12"
android:frameDuration = "100"
/>
7. string:字符串。
(1)屬性定義:
<declare-styleable name = "MapView">
<attr name = "apiKey" format = "string" />
</declare-styleable>
(2)屬性使用:
<com.google.android.maps.MapView
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
android:apiKey = "0jOkQ80oD1JL9C6HAja99uGXCRiS2CGjKO_bc_g"
/>
8. fraction:百分?jǐn)?shù)。
(1)屬性定義:
<declare-styleable name="RotateDrawable">
<attr name = "visible" />
<attr name = "fromDegrees" format = "float" />
<attr name = "toDegrees" format = "float" />
<attr name = "pivotX" format = "fraction" />
<attr name = "pivotY" format = "fraction" />
<attr name = "drawable" />
</declare-styleable>
(2)屬性使用:
<rotate
xmlns:android = "
android:interpolator = "@anim/動(dòng)畫ID"
android:fromDegrees = "0"
android:toDegrees = "360"
android:pivotX = "200%"
android:pivotY = "300%"
android:duration = "5000"
android:repeatMode = "restart"
android:repeatCount = "infinite"
/>
9. enum:枚舉值。
(1)屬性定義:
<declare-styleable name="名稱">
<attr name="orientation">
<enum name="horizontal" value="0" />
<enum name="vertical" value="1" />
</attr>
</declare-styleable>
(2)屬性使用:
<LinearLayout
xmlns:android = " android:orientation = "vertical"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
>
</LinearLayout>
10. flag:位或運(yùn)算。
(1)屬性定義:
<declare-styleable name="名稱">
<attr name="windowSoftInputMode">
<flag name = "stateUnspecified" value = "0" />
<flag name = "stateUnchanged" value = "1" />
<flag name = "stateHidden" value = "2" />
<flag name = "stateAlwaysHidden" value = "3" />
<flag name = "stateVisible" value = "4" />
<flag name = "stateAlwaysVisible" value = "5" />
<flag name = "adjustUnspecified" value = "0x00" />
<flag name = "adjustResize" value = "0x10" />
<flag name = "adjustPan" value = "0x20" />
<flag name = "adjustNothing" value = "0x30" />
</attr>
</declare-styleable>
(2)屬性使用:
<activity
android:name = ".StyleAndThemeActivity"
android:label = "@string/app_name"
android:windowSoftInputMode = "stateUnspecified | stateUnchanged | stateHidden">
<intent-filter>
<action android:name = "android.intent.action.MAIN" />
<category android:name = "android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
注意:
屬性定義時(shí)可以指定多種類型值。
(1)屬性定義:
<declare-styleable name = "名稱">
<attr name = "background" format = "reference|color" />
</declare-styleable>
(2)屬性使用:
<ImageView
android:layout_width = "42dip"
android:layout_height = "42dip"
android:background = "@drawable/圖片ID|#00FF00"
/>
摘要: showDialog()調(diào)用createDialog()和onPrepareDialog(),其中createDialog()調(diào)用onCreateDialog()。例子如下所示 Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> ... 閱讀全文
| |||||||||
日 | 一 | 二 | 三 | 四 | 五 | 六 | |||
---|---|---|---|---|---|---|---|---|---|
25 | 26 | 27 | 28 | 29 | 30 | 31 | |||
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 | |||
1 | 2 | 3 | 4 | 5 | 6 | 7 |
常用鏈接
留言簿(2)
隨筆分類
- Android(49)
- Androidpn(2)
- hibernate(1)
- Https(1)
- JavaCard(3)
- jQuery(6)
- netty
- NFC(1)
- react框架(1)
- spring(2)
- SpringBoot(1)
- Tomcat+Eclipse(18)
- WebService(2)
- 一些心得(1)
隨筆檔案
- 2020年4月 (4)
- 2015年7月 (5)
- 2015年6月 (6)
- 2015年5月 (4)
- 2015年4月 (3)
- 2015年3月 (1)
- 2015年2月 (1)
- 2015年1月 (4)
- 2014年12月 (1)
- 2014年11月 (2)
- 2014年10月 (2)
- 2014年9月 (2)
- 2014年5月 (5)
- 2014年3月 (3)
- 2014年2月 (2)
- 2014年1月 (8)
- 2013年12月 (2)
- 2013年7月 (2)
- 2013年6月 (4)
- 2013年5月 (16)
- 2012年7月 (1)
- 2012年3月 (2)
- 2011年7月 (6)
文章分類
文章檔案
相冊(cè)
收藏夾
Java
搜索
最新隨筆
最新評(píng)論

- 1.?re: Android JSON的簡單例子
- 評(píng)論內(nèi)容較長,點(diǎn)擊標(biāo)題查看
- --JSON.COM
- 2.?re: androidpn(本文服務(wù)器為tomcat)
- 評(píng)論內(nèi)容較長,點(diǎn)擊標(biāo)題查看
- --Deepak Singh