我正在开发用于显示频道直播的应用程序,默认情况下它会以横向模式启动,我可以从我的选项菜单中将其更改为纵向,反之亦然
现在我在清单中使用此代码并将其放入该活动中 机器人:configChanges = “键盘| keyboardHidden |取向|屏幕尺寸”
当我启动应用程序时,它工作正常,但当我将方向更改为纵向时,应用程序挂起,选项菜单不会像往常一样消失。
如果我从清单中删除了这个部分|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 :(得分:2)
我通过从setContentView
拉出try/catch
和onCreate
块并将其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时,应用程序停止。有什么问题?
答案 1 :(得分:0)
当您在AndroidManifest中编写android:configChanges="keyboardHidden|orientation"
时,您正在告诉Android:“当拔出键盘或手机旋转时,请勿执行默认重置;我想自己处理此问题。
因此,当屏幕方向改变时,纵向布局不会设置在您的活动上。
为什么使用android:configChanges="keyboardHidden|orientation
?