现有一个activity,并且有dialog弹出(如图)。
当在dialog存在的时候,点击手机back键,先退出的是此dialog,当再次点击手机返回键的时候,才能退出此activity。
那么如何写代码实现只点击一次手机back键时,退出当前activity?
实现:
private AlertDialog mDialog;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.title_alert_text);
builder.setMessage(R.string.net_alert_text);
builder.setPositiveButton(R.string.setting,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
builder.setNegativeButton(R.string.cancanl,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
Loading.this.finish();
}
});
builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
Loading.this.finish();
}
});
mDialog = builder.create();
mDialog.show();
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0){
mDialog.dismiss();
Loading.this.finish();
}
return super.onKeyDown(keyCode, event);
}
1.管道(pipe)及有名管道(named pipe):
管道可用于具有亲缘关系的父子进程间的通信,有名管道除了具有管道所具有的功能外,它还允许无亲缘关系进程间的通信。
2.信号(signal):
信号是在软件层次上对中断机制的一种模拟,它是比较复杂的通信方式,用于通知进程有某事件发生,一个进程收到一个信号与处理器收到一个中断请求效果上可以说是一致的。
3.消息队列(message queue):
消息队列是消息的链接表,它克服了上两种通信方式中信号量有限的缺点,具有写权限得进程可以按照一定得规则向消息队列中添加新信息;对消息队列有读权限得进程则可以从消息队列中读取信息。
4.共享内存(shared memory):
可以说这是最有用的进程间通信方式。它使得多个进程可以访问同一块内存空间,不同进程可以及时看到对方进程中对共享内存中数据得更新。这种方式需要依靠某种同步操作,如互斥锁和信号量等。
5.信号量(semaphore):
主要作为进程之间及同一种进程的不同线程之间得同步和互斥手段。
6.套接字(socket);
这是一种更为一般得进程间通信机制,它可用于网络中不同机器之间的进程间通信,应用非常广泛。
改动文件如下
froyo/frameworks/base/api/current.xml
line 167996 修改如下
<method name="getHeight"
return="int"
abstract="false"
native="false"
synchronized="false"
static="false"
final="false"
deprecated="not deprecated"
visibility="public"
>
</method>
<method name="getHeightWithPanel"
return="int"
abstract="false"
native="true"
synchronized="false"
static="false"
final="false"
deprecated="not deprecated"
visibility="public"
>
line 168075 修改如下
<method name="getWidth"
return="int"
abstract="false"
native="false"
synchronized="false"
static="false"
final="false"
deprecated="not deprecated"
visibility="public"
>
</method>
<method name="getWidthWithPanel"
return="int"
abstract="false"
native="true"
synchronized="false"
static="false"
final="false"
deprecated="not deprecated"
visibility="public"
>
froyo\frameworks\base\core\java\android\view\Display.java
line 69
把native public int getWidth(); 改成 如下方法
public int getWidth() {
if(getWidthWithPanel() > getHeightWithPanel()){
return getWidthWithPanel() - 40;
}else{
return getWidthWithPanel();
}
}
并且加入此方法:
native public int getWidthWithPanel();
line 87
把native public int getHeight();改成如下方法
public int getHeight() {
if(getWidthWithPanel() > getHeightWithPanel()){
return getHeightWithPanel();
}else{
return getHeightWithPanel() - 40;
}
}
并加入
native public int getHeightWithPanel();
froyo\frameworks\base\core\java\android\view\IWindowManager.aidl
line 55 加入
boolean injectKeyEvent2(in KeyEvent ev, boolean sync);
froyo\frameworks\base\core\jni\android_view_Display.cpp
line 108:
把 { "getWidth", "()I", 改成 { "getWidthWithPanel", "()I",
line 110:
把{ "getHeight", "()I", 改成 { "getHeightWithPanel", "()I",
froyo\frameworks\base\services\java\com\android\server\InputDevice.java
line 838:
把
final int dispW = display.getWidth()-1;
final int dispH = display.getHeight()-1;
改成
final int dispW = display.getWidthWithPanel()-1;
final int dispH = display.getHeightWithPanel()-1;
froyo\frameworks\base\services\java\com\android\server\KeyInputQueue.java
line 51 加入 :import android.util.Log;
line 1382 加入 方法:
public void addLocked2(KeyEvent ev) {
Log.i(TAG, "addLocked2");
synchronized (mFirst) {
final int N = mDevices.size();
for (int i=0; i<N; i++) {
InputDevice d = mDevices.valueAt(i);
if( (d != null) && ((d.classes&RawInputEvent.CLASS_KEYBOARD) != 0) ) {
addLocked(d, System.nanoTime(), 0, RawInputEvent.CLASS_KEYBOARD, ev);
Log.i(TAG, "addLocked2 .........");
break;
}
}
}
}
froyo\frameworks\base\services\java\com\android\server\WindowManagerService.java
line 5528 :加入
public boolean injectKeyEvent2(KeyEvent ev, boolean sync) {
mQueue.addLocked2(ev);
return true;
}
froyo\frameworks\base\services\java\com\android\server\status\StatusBarService.java
line 70:加入
import android.graphics.Color;
import android.widget.ImageView;
import android.os.ServiceManager;
import android.util.Log;
import android.view.IWindowManager;
import android.view.KeyCharacterMap;
import android.view.View.OnTouchListener;
line 98 :修改
把public class StatusBarService extends IStatusBar.Stub改成
public class StatusBarService extends IStatusBar.Stub implements OnTouchListener
line 244 加入:ImageView iv1, iv2, iv3, iv4;
line 364 加入:
LinearLayout mSystemMenuBarView = (LinearLayout)View.inflate(mContext,
com.android.internal.R.layout.control_panel, null);
mSystemMenuBarView.setBackgroundColor(Color.BLACK);
mSystemMenuBarView.setOrientation(LinearLayout.VERTICAL);
iv1 = (ImageView) mSystemMenuBarView.findViewById(R.id.panel1);
iv2 = (ImageView) mSystemMenuBarView.findViewById(R.id.panel2);
iv3 = (ImageView) mSystemMenuBarView.findViewById(R.id.panel3);
iv4 = (ImageView) mSystemMenuBarView.findViewById(R.id.panel4);
iv1.setOnTouchListener(this);
iv2.setOnTouchListener(this);
iv3.setOnTouchListener(this);
iv4.setOnTouchListener(this);
WindowManager.LayoutParams lp1 = new WindowManager.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.FIRST_SYSTEM_WINDOW + 20,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|//note by unic: this line of code make the activity content being in the front.
WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING,
PixelFormat.TRANSLUCENT);
// lp1.gravity = Gravity.RIGHT | Gravity.FILL_VERTICAL;
lp1.setTitle("systemMenuBar");
WindowManagerImpl.getDefault().addView(mSystemMenuBarView, lp1);
line 1880加入:
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (mDisplay.getWidth() > mDisplay.getHeight()) {
switch (v.getId()) {
case R.id.panel1:
iv1.setImageResource(R.drawable.panel_back_pressed);
new Thread(new RunKey(KeyEvent.KEYCODE_BACK, KeyEvent.ACTION_DOWN)).start();
break;
case R.id.panel2:
iv2.setImageResource(R.drawable.panel_search_pressed);
new Thread(new RunKey(KeyEvent.KEYCODE_SEARCH, KeyEvent.ACTION_DOWN)).start();
break;
case R.id.panel3:
iv3.setImageResource(R.drawable.panel_menu_pressed);
new Thread(new RunKey(KeyEvent.KEYCODE_MENU, KeyEvent.ACTION_DOWN)).start();
break;
case R.id.panel4:
iv4.setImageResource(R.drawable.panel_home_pressed);
new Thread(new RunKey(KeyEvent.KEYCODE_HOME, KeyEvent.ACTION_DOWN)).start();
break;
}
}else{
switch (v.getId()) {
case R.id.panel1:
iv1.setImageResource(R.drawable.panel_home_pressed);
new Thread(new RunKey(KeyEvent.KEYCODE_HOME, KeyEvent.ACTION_DOWN)).start();
break;
case R.id.panel2:
iv2.setImageResource(R.drawable.panel_menu_pressed);
new Thread(new RunKey(KeyEvent.KEYCODE_MENU, KeyEvent.ACTION_DOWN)).start();
break;
case R.id.panel3:
iv3.setImageResource(R.drawable.panel_search_pressed);
new Thread(new RunKey(KeyEvent.KEYCODE_SEARCH, KeyEvent.ACTION_DOWN)).start();
break;
case R.id.panel4:
iv4.setImageResource(R.drawable.panel_back_pressed);
new Thread(new RunKey(KeyEvent.KEYCODE_BACK, KeyEvent.ACTION_DOWN)).start();
break;
}
}
}
if (event.getAction() == MotionEvent.ACTION_UP) {
if (mDisplay.getWidth() > mDisplay.getHeight()) {
switch (v.getId()) {
case R.id.panel1:
iv1.setImageResource(R.drawable.panel_back);
new Thread(new RunKey(KeyEvent.KEYCODE_BACK, KeyEvent.ACTION_UP)).start();
break;
case R.id.panel2:
iv2.setImageResource(R.drawable.panel_search);
new Thread(new RunKey(KeyEvent.KEYCODE_SEARCH, KeyEvent.ACTION_UP)).start();
break;
case R.id.panel3:
iv3.setImageResource(R.drawable.panel_menu);
new Thread(new RunKey(KeyEvent.KEYCODE_MENU, KeyEvent.ACTION_UP)).start();
break;
case R.id.panel4:
iv4.setImageResource(R.drawable.panel_home);
new Thread(new RunKey(KeyEvent.KEYCODE_HOME, KeyEvent.ACTION_UP)).start();
break;
}
}else {
switch (v.getId()) {
case R.id.panel1:
iv1.setImageResource(R.drawable.panel_home);
new Thread(new RunKey(KeyEvent.KEYCODE_HOME, KeyEvent.ACTION_UP)).start();
break;
case R.id.panel2:
iv2.setImageResource(R.drawable.panel_menu);
new Thread(new RunKey(KeyEvent.KEYCODE_MENU, KeyEvent.ACTION_UP)).start();
break;
case R.id.panel3:
iv3.setImageResource(R.drawable.panel_search);
new Thread(new RunKey(KeyEvent.KEYCODE_SEARCH, KeyEvent.ACTION_UP)).start();
break;
case R.id.panel4:
iv4.setImageResource(R.drawable.panel_back);
new Thread(new RunKey(KeyEvent.KEYCODE_BACK, KeyEvent.ACTION_UP)).start();
break;
}
}
}
return true;
}
private class RunKey implements Runnable{
int code;
int type;
public void run() {
long now = SystemClock.uptimeMillis();
try {
KeyEvent down = new KeyEvent(now, now, type, code, 0);
(IWindowManager.Stub
.asInterface(ServiceManager.getService("window")))
.injectKeyEvent2(down, true);
} catch (RemoteException e) {
Log.i("Input", "DeadOjbectException");
}
}
public RunKey (int keycode, int actionType){
this.code = keycode;
this.type = actionType;
}
}
froyo\frameworks\policies\base\phone\com\android\internal\policy\impl\PhoneWindowManager.java
line 194 : 加入 WindowState mSystemMenuBar = null;
line 766 :在 方法 public int windowTypeToLayerLw(int type) 中加入
case TYPE_STATUS_BAR + 20:
return STATUS_BAR_LAYER + 20;
line 960 :在方法 public int prepareAddWindowLw(WindowState win, WindowManager.LayoutParams attrs)
中加入
case TYPE_STATUS_BAR + 20:
if (mSystemMenuBar != null) {
return WindowManagerImpl.ADD_MULTIPLE_SINGLETON;
}
mSystemMenuBar = win;
break;
line 992 : 在方法 public void removeWindowLw(WindowState win)中 加入
else if (mSystemMenuBar == win) {
mSystemMenuBar = null;
}
line 1251 在方法 public void beginLayoutLw(int displayWidth, int displayHeight 中加入
final Rect pf = mTmpParentFrame;
final Rect df = mTmpDisplayFrame;
final Rect vf = mTmpVisibleFrame;
pf.left = df.left = vf.left = 0;
pf.top = df.top = vf.top = 0;
pf.right = df.right = vf.right = displayWidth;
pf.bottom = df.bottom = vf.bottom = displayHeight;
并且 把
if (mStatusBar != null) {
final Rect pf = mTmpParentFrame;
final Rect df = mTmpDisplayFrame;
final Rect vf = mTmpVisibleFrame;
pf.left = df.left = vf.left = 0;
pf.top = df.top = vf.top = 0;
pf.right = df.right = vf.right = displayWidth;
pf.bottom = df.bottom = vf.bottom = displayHeight;
mStatusBar.computeFrameLw(pf, df, vf, vf);
改成
if (mStatusBar != null) {
mStatusBar.computeFrameLw(pf, df, vf, vf);
并在 line 1270 加入(加到方法beginLayoutLw中)
if (displayWidth > displayHeight) {
pf.left = df.left = vf.left = displayWidth;
pf.right = df.right = vf.right = displayWidth + 40;
} else {
pf.top = df.top = vf.top = displayHeight;
pf.bottom = df.bottom = vf.bottom = displayHeight + 40;
}
if (mSystemMenuBar != null) {
mSystemMenuBar.computeFrameLw(pf, df, vf, vf);
}
line 1348 在方法 public void layoutWindowLw(WindowState win, WindowManager.LayoutParams attrs,
WindowState attached) 中 加入 (line 1355)
if (win == mSystemMenuBar) {
return;
}