2011-06-18 8 views
2

AndroidがonDestroyメソッドを呼び出す前に画面の向きを検出する方法を知っている人はいますか? onDestroyメソッドが呼び出されたときにキャンセルする必要のあるアクティビティに非同期タスクがあります。画面の向きが変更されたときに非同期タスクをキャンセルしたくないです。Androidの画面の向きを検出する方法

答えて

0

を試してみてください。画面の向きが変わると、AndroidはonDestroyメソッドの前にonRetainNonConfigurationInstance()メソッドを呼び出します。ブール変数を定義して、画面の向きを変更するためのフラグとして機能させることができます。この変数は、onRetainNonConfigurationInstance()メソッドが呼び出されたときにtrueに設定されます。

1

私はそうは考えていません。しかし、私は間違っているかもしれません。とにかく、android:configChangesonConfigurationChanged APIを見てください。それは助けになるかもしれません。

また、onDestroyメソッドで非同期タスクをキャンセルしないでください。

onStopの実行中に呼び出されるAPIがあります。 isChangingConfigurationsを参照してください。 ConfigChanges:あなたがマニフェストアンドロイドに追加する必要が

int width, height; 

DisplayMetrics displaymetrics = new DisplayMetrics(); 
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); 

height = displaymetrics.heightPixels; 
width = displaymetrics.widthPixels; 
+0

ユーザーが画面を終了するかアプリケーションを終了したときにAsyncTaskをキャンセルしますが、画面がフォアグラウンドのままであるため画面の向きが変更された後では、AsyncTaskをキャンセルします。 – kenn3th

1

アンドロイドであるAPIバージョン11にする必要があります。これはあなたのアクティビティを再作成することはありませんが、onConfigurationChanged()メソッドをオーバーライドすることによって、変更された場合は新しいオリエンテーションのすべてのリソースを手動で適用する必要があります。現在の方向と、設定中に変更できる追加のパラメータについての情報も表示されます。

+0

ありがとう、今のところ必要なのは: 'int width、height; DisplayMetrics displaymetrics = new DisplayMetrics(); getWindowManager()。getDefaultDisplay()。getMetrics(displaymetrics); 高さ= displaymetrics.heightPixels; width = displaymetrics.widthPixels; 戻り値>幅; ' –

1

:それはこれを試してみてください3.0

2

は、私はそれが正しいかどうか確認してください解決策を見つけ出すために管理ではなく、この

public int getscrOrientation() 
{ 
Display getOrient = getWindowManager().getDefaultDisplay(); 

int orientation = getOrient.getOrientation(); 

// Sometimes you may get undefined orientation Value is 0 
// simple logic solves the problem compare the screen 
// X,Y Co-ordinates and determine the Orientation in such cases 
if(orientation==Configuration.ORIENTATION_UNDEFINED){ 

Configuration config = getResources().getConfiguration(); 
orientation = config.orientation; 

if(orientation==Configuration.ORIENTATION_UNDEFINED){ 
//if height and widht of screen are equal then 
// it is square orientation 
if(getOrient.getWidth()==getOrient.getHeight()){ 
orientation = Configuration.ORIENTATION_SQUARE; 
}else{ //if widht is less than height than it is portrait 
if(getOrient.getWidth() < getOrient.getHeight()){ 
orientation = Configuration.ORIENTATION_PORTRAIT; 
}else{ // if it is not any of the above it will defineitly be landscape 
orientation = Configuration.ORIENTATION_LANDSCAPE; 
} 
} 
} 
} 
return orientation; // return value 1 is portrait and 2 is Landscape Mode 
} 
関連する問題