2

チャンネルのライブ ストリームを表示するアプリを開発しています。デフォルトでは横向きモードで起動し、オプション メニューから縦向きに変更したり、その逆に変更したりできます。

今、私はマニフェストでこのコードを使用していて、そのアクティビティ 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;
    }

}

マニフェスト.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>
4

2 に答える 2

2

setContentViewからandtry/catchブロックを引き出して配置することで解決しましonCreateonConfigChanged

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 で別の問題を発見しました。about ボタンを押してこのアクティビティから抜け出し、MainActivity を実行すると、アプリが停止します。なにが問題ですか?

于 2013-03-11T06:24:57.483 に答える
0

android:configChanges="keyboardHidden|orientation"AndroidManifest に書き込むと、Android に次のように伝えることになります。

そのため、画面の向きが変更されたときにアクティビティに縦向きのレイアウトが設定されません。なぜあなたは使用していandroid:configChanges="keyboardHidden|orientation ますか?

于 2013-03-11T05:45:23.133 に答える