關閉還不能真的關,只能把他擺到後面..
所以用 moveTaskToBack, moveTaskToFront 來作。
Hotkey 的implement 在 PhoneWindow.java
所以在 onKeyDown( ) 的 case 特殊 Key,作
public class PhoneWindow extends Window implements MenuBuilder.Callback {
String packageName = "com.android.calculator2";
String className = "com.android.calculator2.Calculator";
ActivityManager am = (ActivityManager)getContext().getSystemService(Context.ACTIVITY_SERVICE);
List>ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(20);
for(int i=0;i<taskInfo.size();i++) {
Log.d("current task:", "CURRENT Activity ::" + taskInfo.get(i).topActivity.getClassName() + ", id: " + taskInfo.get(i).id);
Log.d("Package Name:", " " + taskInfo.get(i).topActivity.getPackageName());
if(taskInfo.get(i).topActivity.getPackageName().equals(packageName) == true){
Log.d(TAG,"Found!" + packageName + " in " + i);
if(i!=0) {
try {
ActivityManagerNative.getDefault().moveTaskToFront(taskInfo.get(i).id);
}catch (RemoteException e) {
Log.d(TAG,"Failed !! moveTaskToFront");
}
}else{
try {
ActivityManagerNative.getDefault().moveTaskToBack(taskInfo.get(i).id);
} catch (RemoteException e) {
Log.d(TAG,"Failed !! moveTaskToBack");
}
Log.d(TAG,"Calc is on top");
}
return true;
}
}
// calculator not launched.
Log.d(TAG," " + packageName + " not running");
Intent intent = new Intent();
intent.setClassName(packageName,className);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
getContext().startActivity(intent);
}catch( ActivityNotFoundException e){
Log.d(TAG,"Activity Not Found");
}
}
.. 找特定 packagename,如果是 0 (top), move to back.如果不是 0 (background), move to front
如果找不掉 -- startActivity 啟動他
用 eclipse 的 hierarchy viewr 可以看到這個動作。
但是某 app,本身就具有 "避免重複啟動" , back , home key, keeps running in background 功能。
在 move to front 後,沒有動作。
看 hierarachy view ,好像沒有取得 focus。
所以只好改成:
if on top ==> move to back
if not top ==> startActivity
這要動作。系統要拿掉 GET_TASKS, moveTaskToFront, moveTaskToBack 的 permission check
沒有留言:
張貼留言