簡體   English   中英

當用戶關閉屏幕android時,使應用程序正常工作

[英]Making application work normally when user turns off screen android

我正在開發的是媒體播放器的應用程序。

問題:如何使媒體播放器(應用程序)正常工作而不會關閉屏幕? 問題: loadInBackground()返回uri,但屏幕關閉時不會調用onLoadFinished。

用幾句話可以更好地解釋問題:

媒體播放器包含Loader歌曲的Loader和加載相關建議的另一個Loader 我還實現了play_next()方法,該方法依賴於完成時媒體播放器的偵聽器(右上角的按鈕)。

在此處輸入圖片說明

媒體播放器在我制作的服務類中進行了初始化,因此用戶可以搜索新歌曲,並使用按鈕准備next_song() (並且繼續播放,因為每次加載Activity並從中返回時,我都連接到服務服務媒體播放器,因此我可以為onFinish方法附加偵聽onFinish )。

讓我感到困擾的是,當用戶關閉屏幕時,活動進入空閑狀態(來自android監視器的狀態-日志貓),一旦處於空閑狀態(又稱為關閉屏幕 ),則歌曲結束了,它將重新開始播放意圖是媒體播放器開始初始化和自動播放歌曲。 它在屏幕打開時有效,但在進入空閑狀態時則無效。

如果我打開屏幕,則會有如下活動:

在此處輸入圖片說明

小粉紅點是進度條。 那么活動試圖自我刷新嗎? onCreate()方法中,我調用start_loader ,該方法會初始化並使用Loader

我看過一些電源管理器工具,並看到有關它的不好的評論,這暗示着電池的使用情況,但是我確實嘗試了一下,並且從log cat來看,它再次進入了空閑狀態(如果很重要)。

請幫助,也許如果我重寫onPause()活動和onResume()嗎?

我也從loadInBackground()獲得消息,這是歌曲中的uri,從那里凍結不會繼續。

您需要為其在Background ..上運行的Service創建Service ,因此在播放歌曲時,如果不關閉屏幕就不要停止。 Android中的服務

上部鏈接完美地描述了服務。

服務示例...

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.IBinder;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.widget.Toast;

import com.example.divyesh.musiclite.Pojos.SongsList;

import java.io.File;
import java.io.IOException;

/**
 * Created by Divyesh on 11/18/2017.
 */

public class MediaSongServiece extends Service {
    SongsList s;
    private static Boolean destroy = false;
    private String TAG = "HELLO";
    private MusicIntentReceiver reciever;
    private SharedPreferences prefrence;
    private static MediaPlayer player;
    private int thisStartId = 1;
    private String ss[];
    SharedPreferences.Editor editor;

    public IBinder onBind(Intent arg0) {

        return null;
    }

    public static void requestPlayMedia() {
        player.start();
    }

    public void requestPauseMedia() {
        player.pause();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        reciever = new MusicIntentReceiver();
        IntentFilter filter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
        registerReceiver(reciever, filter);
        Log.d("service", "onCreate");
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        super.onTaskRemoved(rootIntent);
        stopSelf();
    }

    @Override
    public boolean onUnbind(Intent intent) {
        stopSelf(thisStartId);
        return super.onUnbind(intent);
    }

    public void onStart(Intent intent, int startId) {
        if (intent.equals(null)) {
            stopSelf();
        }
        if (destroy == false) {
            thisStartId = startId;
            ss = intent.getExtras().getStringArray("getArray");
            Log.e(TAG, "onStart: " + ss[0] + ss[1] + "    path" + ss[5]);
            s = new SongsList();
            s.setAll(ss);
            if (player != null) {
                player.stop();
                player.reset();
                try {
                    player.setDataSource(getApplicationContext(), Uri.fromFile(new File(s.getPath())));
                    player.prepare();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                player.setLooping(true);
                player.setVolume(100, 100);
                player.start();

                Log.e(TAG, "onStart: m= is  not null" + player.isPlaying());
            } else {

                player = MediaPlayer.create(getApplicationContext(), Uri.fromFile(new File(s.getPath())));
                player.setLooping(true);
                player.setVolume(100, 100);
                player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mediaPlayer) {
                        mediaPlayer.start();

                    }
                });
                Log.e(TAG, "onStart: m= is  null WE created new player");
            }
        } else {
            Log.e(TAG, "onelse destroy ");
            recover();
        }
    }

    private void recover() {
        destroy = false;
        prefrence = getSharedPreferences("SongPrefrence", Context.MODE_PRIVATE);

        for (int i = 0; i <= 5; i++) {
            ss[i] = prefrence.getString("" + i, "");
        }
        String currentPose = prefrence.getString("current_pos", "");

        Log.e(TAG, "recover: Shared Daata is" + ss[5] + "_______" + currentPose);

    }

    @Override
    public void onDestroy() {

        unregisterReceiver(reciever);
        player.stop();
        player.release();
        stopSelf(thisStartId);

    }


    @Override
    public void onLowMemory() {

    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            prefrence = getSharedPreferences("SongPrefrence", Context.MODE_PRIVATE);
            editor = prefrence.edit();
            destroy = true;
        }
        if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            prefrence = getSharedPreferences("SongPrefrence", Context.MODE_PRIVATE);
            editor = prefrence.edit();
            destroy = true;
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    private void saveData() {
        player.pause();
        for (int i = 0; i < ss.length; i++) {
            editor.putString("" + i, ss[i]);
        }
        editor.putString("current_pos", "" + player.getCurrentPosition());
        editor.commit();
    }

    public class MusicIntentReceiver extends BroadcastReceiver {
        public String TAG = "ss";

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
                int state = intent.getIntExtra("state", -1);
                switch (state) {
                    case 0:
                        Log.d(TAG, "Headset is unplugged");
                        Toast.makeText(context, " Headset is unpluged ", Toast.LENGTH_SHORT).show();
                        Log.e(TAG, "onReceive: " + " is play song " + player.isPlaying());
                        break;
                    case 1:
                        Log.d(TAG, "Headset is plugged");
                        break;
                    default:
                        Log.d(TAG, "I have no idea what the headset state is");
                }
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM