Android的音乐播放器服务后回家/后退按钮不打是pressed [英] Android Music Player Service not playing after home/back button is pressed

查看:222
本文介绍了Android的音乐播放器服务后回家/后退按钮不打是pressed的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想写音乐播放器code在android.I写了一个service.Here在MediaPlayer code是code:

Am trying to write music player code in android.I wrote the MediaPlayer code in a service.Here is the code:

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;
    }

}

和从活动称为服务

Intent s=new Intent(this,MyService.class);
        startService(s);

补充清单文件服务

Added the Service in manifest file

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

但是,当家庭/后退按钮是pressed应用程序正在重新启动again.My的要求是,当家庭/后退按钮是pressed应用程序应该continously发挥music.Please帮我。

But when home/backbutton is pressed app is restarting again.My requirement is when home/back button is pressed the app should continously play the music.Please help me.

推荐答案

编辑全code

一步重复此步骤:

箱子activity_main.xml中粘贴此

crate activity_main.xml and paste this

<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类,并粘贴此

After that create MyService class and paste this

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类,并粘贴此

After that create MainActivity1 class and paste this

import android.os.Bundle;

进口android.app.Activity;
进口android.content.Intent;
进口android.view.View;
进口android.widget.Button;

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);

        }

        });

    }

}

最后一步是粘贴以下code的Andr​​oidManifest.xml

Last step is paste the following code 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>

这是必须的工作,没有这方面的疑问。

It's must work, no doubt about this.

这篇关于Android的音乐播放器服务后回家/后退按钮不打是pressed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆