gpt4 book ai didi

android - 当用户关闭屏幕android时使应用程序正常工作

转载 作者:行者123 更新时间:2023-11-29 19:03:51 26 4
gpt4 key购买 nike

我正在开发一个媒体播放器应用程序。

问题:如何使媒体播放器(应用程序)正常工作而不会因屏幕关闭而出现问题?问题:loadInBackground() 返回 uri,但屏幕关闭时未调用 onLoadFinished。

用几句话来更好地解释问题:

媒体播放器包含加载歌曲的Loader和加载相关建议的另一个Loader。我还实现了 play_next() 的方法,它依赖于完成时媒体播放器的监听器(右上角的按钮)。

enter image description here

媒体播放器在我制作的服务类中初始化,因此用户可以搜索新歌曲,并使用按钮准备 next_song()(播放继续,因为我连接到每次加载 Activity 时服务,我从服务媒体播放器返回,这样我就可以为 onFinish 方法附加监听器)。

令我困扰的是,当用户关闭屏幕时,Activity 进入空闲状态(来自 android 监视器的状态 - log cat)并且一旦进入空闲状态(又名关闭屏幕 ) 如果歌曲结束,它将开始新的 Intent ,即媒体播放器开始初始化和自动播放歌曲。它在屏幕打开时工作,但如果它进入空闲状态则不起作用。

如果我打开屏幕,我会得到这样的 Activity :

enter image description here

粉红色的小点是一个进度条。所以这个 Activity 试图刷新自己?在 onCreate() 方法中,我调用了 start_loader,它初始化并使用 Loader 执行操作。

我看过一些电源管理器工具,也看到了关于它的不好的评论,这暗示了电池的使用,但我确实尝试过,并且从 log cat 中它再次进入空闲状态(如果它重要的话)。

请帮忙,也许我重写 onPause() Activity 和 onResume()

我还收到来自 loadInBackground() 的消息,它是来自歌曲的 uri,从那里开始卡住不会继续。

最佳答案

您需要为在后台运行的服务创建服务。因此,当您播放歌曲时,如果您一直关闭屏幕,就不会停止。 Service in 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");
}
}
}
}
}

关于android - 当用户关闭屏幕android时使应用程序正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47924919/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com