2017-04-05 172 views
2

我有一個設備,顯然有東西與方向打破,因爲重新啓動後,大約一個小時,它會旋轉屏幕響應方向的變化 - 然後它會停止響應,無論是在「桌面「級別和應用程序級別。更改Android主屏幕方向?

所以,我發現Change Screen Orientation programmatically using a Button,我想我可以創建一個「圖標按鈕」的應用程序,當按下時,不會運行一個新的應用程序,而是試圖改變方向。

「圖標/按鈕」應用程序的框架是Lock screen (it.reyboz.screenlock)的副本。我張貼在依據這個項目 - 但因爲它是很難有默認的文件夾(文件和二進制文件)的要點,這是你可以用它來獲取代碼的程序:

git clone https://gist.github.com/e6422677cababc17526d0cb57aceb76a.git dl_archive_git 

cd dl_archive_git 

bash run-me-to-unpack.sh 

# check upacked dir: 
tree rotate-btn-droid 

cd rotate-btn-droid/ 

# change your SDK path (ASDKPATH) in buildme.sh, and then: 
bash buildme.sh 
# if your java install is not in the path, then call the last command with JAVA_HOME prepended: 
# JAVA_HOME=/path/to/jdkXXX bash buildme.sh 

基本上,我米只是努力做到以下幾點在MainActivity.java

Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay(); 
int orientation = display.getOrientation(); 
// OR: orientation = getRequestedOrientation(); // inside an Activity 

switch(orientation) { 
    case Configuration.ORIENTATION_PORTRAIT: 
    setRequestedOrientation (Build.VERSION.SDK_INT < 9 ? 
          ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE : 
          ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE); 
    break; 
    case Configuration.ORIENTATION_LANDSCAPE: 
    setRequestedOrientation (Build.VERSION.SDK_INT < 9 ? 
          ActivityInfo.SCREEN_ORIENTATION_PORTRAIT : 
          ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT); 
    break; 
} 

...然而,沒事的時候我點擊應用圖標,然後運行該代碼發生。

所以,我的問題是 - 原則上可能強制在「桌面」級別上更改設備方向?如果是這樣,它是否依賴於Android版本(可能是供應商品牌) - 以及如何?

+1

我不確定它是否可能在設備級cuz如果你按照[this here](https://developer.android.com/reference/android/app/Activity.html#setRequestedOrientation( int))它清楚地說明它會改變希望的方向只有 – shadygoneinsane

+1

有一個解決方法[這裏](http://stackoverflow.com/a/14654302/5134647),如果你的設備API是18或以下,你可以使用[this here](http:// stackoverflow.com/a/26895627/5134647) – shadygoneinsane

+0

謝謝@shadygoneinsane - 將確保我檢查解決方法;如果有任何工作會報告回來 – sdaau

回答

0

只是好奇,所以嘗試了Sam's solution並做出了一些改變:

會需要這個權限在Manifest

權限<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

而且這個權限是一個危險的權限,但如果你要安裝的應用程序從Play商店你不用擔心這一點。

我做了一個窗口服務是這樣的:

public class MyServiceNew extends Service { 

private View view; 

public MyServiceNew() { 
} 

@Override 
public IBinder onBind(Intent intent) { 
    throw new UnsupportedOperationException("Not yet implemented"); 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    int flag = intent.getExtras().getInt("Flag"); 
    WindowManager.LayoutParams layout; 
    if (flag == 1) { 
     layout = generateLayoutLandscape(); 
    } else { 
     layout = generateLayoutPortrait(); 
    } 
    WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE); 
    wm.addView(view, layout); 
    return super.onStartCommand(intent, flags, startId); 
} 

private WindowManager.LayoutParams generateLayoutPortrait() { 
    Toast.makeText(this, "Port", Toast.LENGTH_LONG).show(); 
    view = new View(this); 
    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
      WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, 
      WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, 
      PixelFormat.TRANSPARENT); 
    params.alpha = 0f; 
    params.width = 0; 
    params.height = 0; 

    //The orientation to force 
    params.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; 
    return params; 
} 

private WindowManager.LayoutParams generateLayoutLandscape() { 
    Toast.makeText(this, "Land", Toast.LENGTH_LONG).show(); 
    view = new View(this); 
    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
      WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, 
      WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, 
      PixelFormat.TRANSPARENT); 
    params.alpha = 0f; 
    params.width = 0; 
    params.height = 0; 

    //The orientation to force 
    params.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; 
    return params; 
} 
} 

從你的活動而發展確保包含ACTION_MANAGE_OVERLAY_PERMISSION;您再也不需要擔心通過Google Play Store安裝應用程序的用戶。這裏使用下面的代碼允許上述M器件權限:

public void requestSystemAlertPermission(int requestCode) { 
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) 
     return; 
    final String packageName = getPackageName(); 
    final Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + packageName)); 
    startActivityForResult(intent, requestCode); 
} 

現在剛開始你的服務,並通過爲縱向或橫向 演員我使用1景觀和2肖像

Intent intentLand = new Intent(SoNew.this, MyServiceNew.class); 
intentLand.putExtra("Flag", 2); //or change 1 for Port 
startService(intentLand); 

希望這會有所幫助:)