2013-03-11 78 views
2
变化方向

我开发的应用程序用于显示频道的直播流,并将其默认启动景观模式,我可以从我的选项菜单布局被打破,在Android的

现在我使用更改为纵向,反之亦然这个代码在清单,并把它在该活动中 android:configChanges =“keyboard | keyboardHidden | orientation | screenSize”

当我启动的应用程序,它工作正常,但当我改变方向肖像,应用程序挂起选项菜单不会照常消失。

如果我从清单中删除了这部分|screenSize,它工作正常,但布局土地不起作用,并且在两个方向都使用纵向布局。

这是LiveActivity.java

package com.shadatv.shada; 

import android.media.MediaPlayer; 
import android.net.Uri; 
import android.os.Bundle; 
import android.app.Activity; 
import android.app.AlertDialog; 

import android.content.DialogInterface; 
import android.content.Intent; 
import android.content.pm.ActivityInfo; 
import android.content.res.Configuration; 

import android.view.Menu; 
import android.view.MenuItem; 

import android.view.WindowManager; 

import android.widget.VideoView; 

public class LiveActivity extends Activity { 
    VideoView videoView; 

    private AlertDialog.Builder errorDialog; 
    String httpLiveUrl = "http://38.96.148.147:1935/live/livestream/playlist.m3u8"; 

    // ============================================================================= 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
     if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { 
      getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
        WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     } 
     setContentView(R.layout.live); 
     try { 
      videoView = (VideoView) findViewById(R.id.myVideoView); 
      videoView.setVideoURI(Uri.parse(httpLiveUrl)); 
      videoView.requestFocus(); 

      videoView 
        .setOnPreparedListener(new MediaPlayer.OnPreparedListener() { 

         @Override 
         public void onPrepared(MediaPlayer mp) { 
          // TODO Auto-generated method stub 

          videoView.seekTo(1000); 
          videoView.start(); 

         } 
        }); 

     } catch (Exception e) { 
      errorDialog = new AlertDialog.Builder(this); 
      errorDialog.setMessage("مشكلة في البث"); 
      errorDialog.setCancelable(true); 
      errorDialog.setNeutralButton("Ok", 
        new DialogInterface.OnClickListener() { 
         // click listener on the alert box 
         public void onClick(DialogInterface arg0, int arg1) { 

         } 
        }); 
      AlertDialog errorStream = errorDialog.create(); 
      errorStream.show(); 
     } 
    } 

    // ============================================================================= 

    @Override 
    public boolean onPrepareOptionsMenu(Menu menu) { 
     menu.clear(); 
     getMenuInflater().inflate(R.menu.activity_shada, menu); 
     MenuItem fullItem = menu.findItem(R.id.fullScreen); 
     MenuItem smallItem = menu.findItem(R.id.smallScreen); 

     if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { 
      fullItem.setVisible(false); 
      smallItem.setVisible(true); 
     } else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { 
      smallItem.setVisible(false); 
      fullItem.setVisible(true); 
     } 
     return super.onPrepareOptionsMenu(menu); 
    } 

    // ============================================================================= 

    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 
     if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { 
      getWindow().clearFlags(
        WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); 
      getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
        WindowManager.LayoutParams.FLAG_FULLSCREEN); 
      // setContentView(R.layout.activity_shada); 
     } else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { 
      getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 
      getWindow().setFlags(
        WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN, 
        WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); 
      // setContentView(R.layout.activity_shada1); 
     } 
    } 

    // ============================================================================= 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
     case R.id.about: { 
      // TODO Auto-generated method stub 
      startActivity(new Intent("com.shadatv.MainActivity")); 
     } 
      break; 

     case R.id.smallScreen: { 
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
     } 
      break; 

     case R.id.fullScreen: { 
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
     } 
      break; 
     } 
     return true; 
    } 

} 

的manifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.shadatv.shada" 
    android:versionCode="3" 
    android:versionName="1.0.2" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="15" /> 

    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/shada" 
     android:label="@string/app_name" 

     >  
     <activity 
      android:name=".MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <activity 
      android:name=".LiveActivity" 
      android:configChanges="keyboard|keyboardHidden|orientation|screenSize" 
      android:exported="false" 
      android:label="@string/app_name" 
      android:theme="@android:style/Theme.NoTitleBar.Fullscreen" > 

      <intent-filter> 
       <action android:name="com.shadatv.LiveActivity" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 


</manifest> 

回答

0

当你写在你的AndroidManifest android:configChanges="keyboardHidden|orientation",你告诉安卓:“不要做默认复位时键盘被拔出,或者电话被旋转;我想自己处理这个问题

所以当屏幕方向时,肖像布局不会设置在你的活动上改变。 为什么你使用android:configChanges="keyboardHidden|orientation

+0

因为我从景观转换为肖像,反之亦然动态从选项菜单 – 2013-03-11 06:16:39

+0

所以我认为你必须setContentView(R.layout.portrait);动态onclick。 – 2013-03-11 06:22:13

+0

感谢我解决了它,但还有另一个问题,我解释了我的答案。请问你能帮帮我吗?? – 2013-03-11 06:27:31

2

我解决它由onCreate拉出setContentViewtry/catch块,并把它onConfigChanged

package com.shadatv.shada; 

import android.media.MediaPlayer; 
import android.net.Uri; 
import android.os.Bundle; 
import android.app.Activity; 
import android.app.AlertDialog; 

import android.content.DialogInterface; 
import android.content.Intent; 
import android.content.pm.ActivityInfo; 
import android.content.res.Configuration; 

import android.view.Menu; 
import android.view.MenuItem; 

import android.view.WindowManager; 

import android.widget.VideoView; 

public class LiveActivity extends Activity { 
    VideoView videoView; 

    private AlertDialog.Builder errorDialog; 
    String httpLiveUrl = "http://38.96.148.147:1935/live/livestream/playlist.m3u8"; 

    // ============================================================================= 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
     // if (getResources().getConfiguration().orientation == 
     // Configuration.ORIENTATION_LANDSCAPE) { 
     // getWindow().clearFlags(
     // WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); 
     // getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
     // WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     // } 
     // setContentView(R.layout.live); 

    } 

    // ============================================================================= 

    @Override 
    public boolean onPrepareOptionsMenu(Menu menu) { 
     menu.clear(); 
     getMenuInflater().inflate(R.menu.activity_shada, menu); 
     MenuItem fullItem = menu.findItem(R.id.fullScreen); 
     MenuItem smallItem = menu.findItem(R.id.smallScreen); 

     if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { 
      fullItem.setVisible(false); 
      smallItem.setVisible(true); 
     } else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { 
      smallItem.setVisible(false); 
      fullItem.setVisible(true); 
     } 
     return super.onPrepareOptionsMenu(menu); 
    } 

    // ============================================================================= 

    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 
     setContentView(R.layout.live); 
     try { 
      videoView = (VideoView) findViewById(R.id.myVideoView); 
      videoView.setVideoURI(Uri.parse(httpLiveUrl)); 
      videoView.requestFocus(); 

      videoView 
        .setOnPreparedListener(new MediaPlayer.OnPreparedListener() { 

         @Override 
         public void onPrepared(MediaPlayer mp) { 
          // TODO Auto-generated method stub 

          videoView.seekTo(1000); 
          videoView.start(); 

         } 
        }); 

     } catch (Exception e) { 
      errorDialog = new AlertDialog.Builder(this); 
      errorDialog.setMessage("مشكلة في البث"); 
      errorDialog.setCancelable(true); 
      errorDialog.setNeutralButton("Ok", 
        new DialogInterface.OnClickListener() { 
         // click listener on the alert box 
         public void onClick(DialogInterface arg0, int arg1) { 

         } 
        }); 
      AlertDialog errorStream = errorDialog.create(); 
      errorStream.show(); 
     } 

     if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { 
      getWindow().clearFlags(
        WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); 
      getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
        WindowManager.LayoutParams.FLAG_FULLSCREEN);  
     } else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { 
      getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 
      getWindow().setFlags(
        WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN, 
        WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); 
     } 
    } 

    // ============================================================================= 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
     case R.id.about: 
      // TODO Auto-generated method stub 
      startActivity(new Intent("com.shadatv.MainActivity")); 
      break; 

     case R.id.smallScreen: 
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
      break; 

     case R.id.fullScreen: 
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
      break; 
     } 
     return true; 
    } 

} 

但是我发现在optionsMenu另一个问题。当我按下按钮退出此活动并运行MainActivity时,应用程序停止。哪里不对?

+0

为什么你不使用这种方式来调用活动。 Intent intent = new Intent(LiveActivity .this,Main.class); startActivity(intent); – 2013-03-11 06:33:40

+0

看起来这个答案在最后提出了一个新问题。我们更喜欢不包含的问题 - 可以删除最终位并允许答案仍然有用,还是更好地回答完全被删除? – halfer 2016-12-02 08:54:37