나는 사용자가 죽일 때까지 백그라운드에서 소리 만 재생하는 간단한 응용 프로그램을 개발 중입니다. 문제는 크롬에서 30-40 개 이상의 탭을 열면 소리 재생이 중지되고 앱을 다시 열어야한다는 것입니다.많은 크롬 탭을 열 때 내 앱이 작동하지 않는 이유는 무엇입니까?
스마트 폰에는 1GB의 RAM 만 있지만 Chrome에서 100 개 이상의 탭을 열어도 많은 뮤직 플레이어가 작동한다는 것을 알았습니다.
PlayerService.class :
public class PlayerService extends Service
{
MediaPlayer mediaPlayer = null;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
mediaPlayer = MediaPlayer.create(this, R.raw.song);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
}
return START_STICKY;
}
public void onDestroy() {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
mediaPlayer.release();
Log.i("PROVA SERVICE", "Distruzione Service");
}
}
MainActivity.class :
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void play(View v)
{
startService(new Intent(this, PlayerService.class));
}
public void stop(View v)
{
stopService(new Intent(this, PlayerService.class));
}
...
activity_main.xml :
이
코드입니다내가 분명히 매니페스트에서 서비스 추가 한
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView android:text="Play!" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:onClick="play"
android:id="@+id/textView"
android:layout_centerHorizontal="true"
android:textStyle="bold"
android:textSize="20dp" />
<TextView android:text="Stop!" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:onClick="stop"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:textSize="20dp"
android:textStyle="bold" />
</RelativeLayout>
:
<service android:name="PlayerService"/>
그래서, 문제는 무엇입니까?
예, 올바르지 만 어쨌든 시스템에 의해 종료 될 수 있습니다. 서비스가 포 그라운드에서 실행되는 경우 툴바에 알림을 설정하여 사용자에게 알릴 수 있습니다. – Mikhail
일반적으로 앱을 즉시 종료하지 않습니다. – Krish