按下主页/后退按钮后,Android音乐播放器服务无法播放

时间:2014-01-06 10:24:50

标签: android android-service android-mediaplayer

我试图在android中编写音乐播放器代码。我在服务中编写了MediaPlayer代码。这是代码:

package com.example.audioservice;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service {
  static MediaPlayer mp;



    @Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
    System.out.println("in MyService onCreate()");

    mp=MediaPlayer.create(getApplicationContext(),R.raw.subanallah);

}

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        System.out.println("in onService onBind()");
        return null;
    }



    @Override
    public int onStartCommand(Intent intent,int flags, int startId) {
        super.onStart(intent, startId);
        Log.d("log", "In onStart.");
        System.out.println("in MyService onStartCommand()");

        mp.start();
        return Service.START_STICKY;
    }

}

并从Activity

调用该服务
Intent s=new Intent(this,MyService.class);
        startService(s);

在清单文件中添加了服务

<service android:name="com.example.audioservice.MyService"></service>

但是当按下主页/后退键时,应用程序又重新启动。我的要求是当按下主页/后退按钮时应用程序应该继续播放音乐。请帮助我。

2 个答案:

答案 0 :(得分:2)

编辑完整代码

分步执行::

crate 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<Button
    android:id="@+id/playId"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/stopId"
    android:layout_marginRight="53dp"
    android:text="Play" />

<Button
    android:id="@+id/stopId"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="48dp"
    android:layout_marginTop="50dp"
    android:text="Stop" />

之后创建MyService类并粘贴此

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;

public class MyService extends Service {

    MediaPlayer mp; 
    @Override   
    public IBinder onBind(Intent arg0) {

        return null;

    }

    @Override   
    public void onCreate() 
    {   
      super.onCreate(); 
      mp = MediaPlayer.create(getApplicationContext(), R.raw.ram3);

    }

    @Override   
    public int onStartCommand(Intent intent, int flags, int startId) 
    {   
        mp.start(); 
        mp.setLooping(true);
        return 0;

    }

    @Override   
    public void onDestroy() 
    {   
        mp.release();       
        super.onDestroy();

    }

}

之后创建MainActivity1类并粘贴此

import android.os.Bundle;

import android.app.Activity; import android.content.Intent; import android.view.View; import android.widget.Button;

public class MainActivity1 extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        Button play, stop;

        play = (Button) findViewById(R.id.playId);      
        stop = (Button) findViewById(R.id.stopId);      
        play.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View v) {

        Intent service = new Intent(MainActivity1.this, MyService.class);

        startService(service);

        }

        });

        stop.setOnClickListener(new View.OnClickListener() {

        @Override

          public void onClick(View v) {

        Intent name = new Intent(MainActivity1.this, MyService.class);

        stopService(name);

        }

        });

    }

}

最后一步是粘贴以下代码AndroidManifest.XML

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity1"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service
        android:name=".MyService"
        android:enabled="true" >
    </service>
</application>

这一定是必须的,毫无疑问。

答案 1 :(得分:0)

希望我能帮助你。

1.使用 startForeground()(我没有尝试过这种方式)
   2.将此代码放在startService()下: bindService(Intent,ServiceConnection,BIND_IMPORTAN)。当然,你需要实现。(我使用它,在我的情况下有效)。