Android上是否有任何全局活動,這樣我就可以將我的代碼放入該活動中,並且會影響我項目中的所有活動?因爲相同的代碼是寫在多個活動,如KeyEvent.KEYCODE_BACK
我可以將我的代碼組合成某種「全局活動」嗎?
例如,這發生在我這裏我用:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
try {
final Intent itnt_BackServices = new Intent(this,
BackServices.class);
AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
alertbox.setTitle("Touch signs");
alertbox.setMessage("Do you want to quit!");
alertbox.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
stopService(itnt_BackServices);
mPlayer.stop();
finish();
}
});
alertbox.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
}
});
alertbox.show();
} catch (Exception e) {
// TODO: handle exception
}
}
return false;
}
我複製並粘貼在每一項活動,我寧願使用某種全球活動。
只是爲了注意如果你的應用程序不是一個遊戲,確認退出是不好的UX – weakwire