保活(二)-播放无声音乐

服务

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

import com.cpsc.sanya.R;


/**
 * 描述:
 * 

* * @author allens * @date 2018/1/24 */ public class PlayerMusicService extends Service { private final static String TAG = "PlayerMusicService"; private MediaPlayer mMediaPlayer; @Nullable @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); Log.d(TAG, TAG + "---->onCreate,启动服务"); mMediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.no_notice); mMediaPlayer.setLooping(true); } @Override public int onStartCommand(Intent intent, int flags, int startId) { new Thread(new Runnable() { @Override public void run() { startPlayMusic(); } }).start(); return START_STICKY; } private void startPlayMusic() { if (mMediaPlayer != null) { Log.d(TAG, "启动后台播放音乐"); mMediaPlayer.start(); } } private void stopPlayMusic() { if (mMediaPlayer != null) { Log.d(TAG, "关闭后台播放音乐"); mMediaPlayer.stop(); } } @Override public void onDestroy() { super.onDestroy(); stopPlayMusic(); Log.d(TAG, TAG + "---->onCreate,停止服务"); // 重启 Intent intent = new Intent(getApplicationContext(), PlayerMusicService.class); startService(intent); } }

无声音乐下载

https://pan.baidu.com/s/1smPKs5f

清单文件

    

你可能感兴趣的:(保活(二)-播放无声音乐)