編輯:關於Android編程
在android開發過程中,我們可能存儲一些全局的變量,最好在正在app的任何一個activity或者service中都可以訪問到,這時我們可以使用application。
我們的一個應用就叫application,那麼應該很好理解一個應用裡面只會存在一個單例的application,也不難想到用這個在存儲全局變量,那麼到底是怎麼存儲呢?
首先,我們創建一個Application,繼承android.app.Application:
package com.podongfeng.firstapplication.app;
import android.app.Application;
public class MyApplication extends Application {
private Integer allViewInteger;
public Integer getAllViewInteger() {
return allViewInteger;
}
public void setAllViewInteger(Integer allViewInteger) {
this.allViewInteger = allViewInteger;
}
}
其實,在AndroidManifest.xml中肯定會存在一個系統聲明的Application,類似於這樣:
其實,只要在application標簽中增加android:name屬性指向我們自定義的application就可以了:
等等,是不是局的這樣還不是特別的方便,如果寫了一些共用的java方法,為了代碼的良好復用,沒有放在activity裡面呢?
通過一個參數把context傳過去,然後再用context去獲取Application?
這樣做當然可以,不過,既然Application是單例的,我們很容聯想到在單例的設計模式中使用getInstance方法來得到單例的對象。事實上,我們的MyApplication集成了Application,可以直接覆寫onCreate方法,在Application被創建時把對象賦值給一個靜態成員變量,這樣,就可以任何地方通過MyApplication的靜態方法去獲取這個單例了:
package com.podongfeng.firstapplication.app;
import android.app.Application;
public class MyApplication extends Application {
private static MyApplication myApplication = null;
public static MyApplication getMyApplication() {
return myApplication;
}
private Integer allViewInteger;
public Integer getAllViewInteger() {
return allViewInteger;
}
public void setAllViewInteger(Integer allViewInteger) {
this.allViewInteger = allViewInteger;
}
@Override
public void onCreate() {
super.onCreate();
myApplication = this;
}
}
package com.podongfeng.firstapplication;
import com.podongfeng.firstapplication.app.MyApplication;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener {
private Button nextBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyApplication myApplication = MyApplication.getMyApplication();
myApplication.setAllViewInteger(100);
nextBtn = (Button) findViewById(R.id.btn_next);
nextBtn.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(getApplicationContext(), SecActivity.class);
startActivity(intent);
}
}
package com.podongfeng.firstapplication;
import com.podongfeng.firstapplication.app.MyApplication;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class SecActivity extends Activity {
private TextView textView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activiry_sec);
textView = (TextView) findViewById(R.id.tv_sec);
textView.setText(String.valueOf(MyApplication.getMyApplication().getAllViewInteger()));
}
}
Android官方文檔之Services
官方原文鏈接本文將介紹Services的定義、創建、啟動、綁定、前台Service等相關內容。Service是Android中一個類,它是Android四大組件之一,使用
Android studio 百度地圖開發(1)配置工程、顯示地圖
Android studio 百度地圖開發(1)配置工程、顯示地圖email:chentravelling@163.com最近在學習寫app,需要用到百度地圖,於是整理了
Android App卡頓慢優化之多線程優化
本博客涉及的內容有:多線程並發的性能問題,介紹了AsyncTask,HandlerThread,IntentService與ThreadPool分別適合的使用場景以及各自
Android 源碼解析View的touch事件分發機制
概述本篇主要分析的是touch事件的分發機制,網上關於這個知識點的分析文章非常多。但是還是想通過結合自身的總結,來加深自己的理解。對於事件分發機制,我將使用兩篇文章對其進