編輯:關於Android編程
基於上一篇博客《Android退出程序(上)——單例模式》的學習,我們知道了如何利用單例和循環遍歷的方式來退出我們的應用程序。這篇博客則要從另一個角度來解決問題——廣播。即是,接收到一個關閉Activity的廣播時,就去關閉當前Activity。
vcHL0ru49rv5tKHA4NbQo6zIu7rzyMPG5Mv70OjSqsq1z9a52LHVYWN0aXZpdHm1xEFjdGl2aXR5wLS8zLPQy/y+zbrDwcuhozwvcD4KPHA+PGJyPgo8L3A+CjxoMT60+sLryr7A/aO6PC9oMT4KPHA+1eLA79a7zPmz9sG9uPaxyL3Pudi8/LXEtPrC66O6PC9wPgo8aDI+MS6908rVysK8/jwvaDI+CjxwPjxwcmUgY2xhc3M9"brush:java;">public class BaseActivity extends Activity {
public static final String TAG = BaseActivity.class.getName();
public static final String BROAD_CAST_MESSAGE = TAG + ".BROAD_CAST_MESSAGE";
private ExitappReceiver mReceiver = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
registerReceiver();
}
private void registerReceiver() {
IntentFilter filter = new IntentFilter();
try {
if (mReceiver != null) {
unregisterReceiver(mReceiver);
}
} catch (Exception e) {
e.printStackTrace();
}
mReceiver = new ExitappReceiver();
filter.addAction(BROAD_CAST_MESSAGE);
registerReceiver(mReceiver, filter);
}
@Override
protected void onDestroy() {
if (mReceiver != null) {
unregisterReceiver(mReceiver);
}
super.onDestroy();
}
class ExitappReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(BROAD_CAST_MESSAGE)) {
finish();
}
}
}
}
public class TwoActivity extends BaseActivity { private int getLayoutResID() { return R.layout.activity_two; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(getLayoutResID()); Button nextButton = (Button) findViewById(R.id.activity_two_button); nextButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { sendBroadcast(); } }); } private void sendBroadcast() { Intent intent = new Intent(); intent.setAction(BaseActivity.BROAD_CAST_MESSAGE); sendBroadcast(intent); } }
前言在日常開發APP 的過程中,隨著業務的擴展,規模的變化。我們的代碼規模也會逐漸變得龐大,每一個類裡的代碼也會逐漸增多。尤其是Activity和Fragment ,由於
概念: java中單例模式是一種常見的設計模式,單例模式分三種:懶漢式單例、餓漢式單例、登記式單例三種。 單例模式有一下特點: 1、單例類只能有一個實例。
本文實例講述了使用SAX來解析XML。通常來說在Android裡面可以使用SAX和DOM,DOM需要把整個XML文件讀入內存再解析,比較消耗內存,而SAX是基於事件驅動的
水流波動的波形都是三角波,曲線是正余弦曲線,但是Android中沒有提供繪制正余弦曲線的API,好在Path類有個繪制貝塞爾曲線的方法