-1

I have set orientation of my activity to landscape and it remains in that state except one case.and the case is screen lock .when my activity is running and i lock my screen it changes orientation from landscape to portrait for little bit of time and causes my app to restart and which I don't want.any solution...?

package com.example.hello;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import android.app.Activity;
import android.app.KeyguardManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.os.PowerManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.MediaController;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.VideoView;
public class MainActivity extends Activity{

    VideoView vv;
    public ArrayList<String> list = new ArrayList<String>();
    int n,positio = 0;
    String file = null;
    Spinner spin;
    MediaPlayer mp;
    Intent intent;
    File fl;
    boolean isPlaying = true;
     boolean screenOn = false;
     PowerManager pm;
    @Override
    public void onCreate(Bundle savedInstance){

        super.onCreate(savedInstance);
        Log.d("zaid iqbal", "in onCreate");
        requestFullScreen();
        setContentView(R.layout.activity_main);
        vv = (VideoView)findViewById(R.id.video);
        spin = (Spinner)findViewById(R.id.spinner);
        MediaController mc = new MediaController(this);
        pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        vv.setMediaController(mc);
        setupSpin();


    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
            //Log.d("zaid iqbal", "in onPause");
        isPlaying = vv.isPlaying();
        screenOn = pm.isScreenOn();

        if (screenOn && isPlaying) {
            // Screen is still on, so do your thing here
            n++;
            positio = vv.getCurrentPosition();
            vv.pause();
            //Toast.makeText(this, file, Toast.LENGTH_LONG).show();
            intent = new Intent(this,playBack.class);
            intent.putExtra("file", file);
            intent.putExtra("position", positio);
            intent.putExtra("n", n);
            startService(intent);
        }

        if(!isPlaying){

            positio = vv.getCurrentPosition();

        }
    super.onPause();
}

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        //Log.d("zaid iqbal", "in onResume");
        if(!isPlaying){

            vv.setVideoPath(file);
            vv.seekTo(positio);


        }else{

        async aa = new async();
        aa.execute();

        }
        super.onResume();
}

    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub

    }

    public void requestFullScreen(){

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN
                ,WindowManager.LayoutParams.FLAG_FULLSCREEN);

    }
    public void setupSpin(){

        File root = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Video/");
        listFiles(root);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,list);
        spin.setAdapter(adapter);

        spin.setOnItemSelectedListener(new OnItemSelectedListener(){

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int position, long id) {
                // TODO Auto-generated method stub
                //Log.d("zaid iqbal","in onclick of spin");
                if(n == 0){
                file = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Video/" + list.get(position);
                Log.d("zaid", file);
                //Toast.makeText(MainActivity.this, "original" + file, Toast.LENGTH_SHORT).show();
                vv.setVideoPath(file);
                vv.start();
                }
                //n = 0;
                /*File fl = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Video/","settings.txt");
                try {
                    BufferedReader reader = new BufferedReader(new FileReader(fl));
                    file = reader.readLine();
                    position = Integer.parseInt(reader.readLine());
                    reader.close();

                    BufferedWriter writer = new BufferedWriter(new FileWriter(fl));
                    writer.write(file);
                    writer.write(position);
                    writer.write(n);
                    position = Integer.parseInt(reader.readLine());
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }*/
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }

        });

    }

    public void listFiles(File f){
        try{
        File[] files = f.listFiles();
        for(File file : files){

            if(file.isFile()){

                String filenameArray[] = file.toString().split("\\.");
                String extension = filenameArray[filenameArray.length-1];
                //if(extension == "mp4" || extension == "3gp")
                list.add(file.getName());

            }

        }
        }catch(Exception e){

            e.printStackTrace();

        }

    }

    class async extends AsyncTask<String,Long,Long>{

        @Override
        protected Long doInBackground(String... params) {
            // TODO Auto-generated method stub
            if(intent != null){
                stopService(intent);
            //Reading file

            }   
            return null;
        }

        @Override
        protected void onPostExecute(Long result) {
            // TODO Auto-generated method stub
            fl = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Video/","settings.txt");
            try {
                //Log.d("zaid iqbal", "getted file");
                BufferedReader reader = new BufferedReader(new FileReader(fl));
                file = reader.readLine();
                //Toast.makeText(MainActivity.this, "getted file" + file, Toast.LENGTH_SHORT).show();
                positio = Integer.parseInt(reader.readLine());
                n = Integer.parseInt(reader.readLine());
                reader.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            if(n != 0){

            n=0;
            try {
                BufferedWriter writer = new BufferedWriter(new FileWriter(fl));
                writer.write(file + "\n" + positio + "\n" + n);
                writer.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            vv.setVideoPath(file);
            vv.seekTo(positio);
            vv.start();
        }
            super.onPostExecute(result);
    }   
}

}
Zaid Iqbal
  • 123
  • 1
  • 5
  • 12

3 Answers3

0

First, I would read up on Android activity lifecycle. Personally I think this is the way begin to address the issue -

http://developer.android.com/training/basics/activity-lifecycle/index.html

Another option to investigate -

android:configChanges="orientation|keyboardHidden|screenSize"

how to stop activity recreation on screen orientation?

Community
  • 1
  • 1
Kris Krause
  • 7,304
  • 2
  • 23
  • 26
0

Try this in your Activity Tag of Manifest file :

<activity android:name=".MyActivity"
          android:configChanges="orientation|keyboardHidden"
          android:label="@string/app_name">    

Here Orientation refers to Configuration changes which you want to handle by your own.

To declare that your activity handles a configuration change, edit the appropriate <activity> element in your manifest file to include the android:configChanges attribute with a value that represents the configuration you want to handle.

Use onSaveInstanceState() and onRestoreInstanceState() if you want to restore your previous state of an activity.

Refer this also

Community
  • 1
  • 1
Piyush Kukadiya
  • 1,880
  • 16
  • 26
0

You may try to call setRequestedOrientation() in you activity so the orientation is forced.

digital illusion
  • 497
  • 3
  • 19
  • http://developer.android.com/reference/android/app/Activity.html#setRequestedOrientation%28int%29 But be aware that even if this will avoid changing from landscape to portrait, the application lifecycle managment is going to require that you restore the state – digital illusion Sep 06 '14 at 12:28