1、后台音乐保活
(1)、前后台管理
public class ForegroundCallbacks implements Application.ActivityLifecycleCallbacks {
public static final long CHECK_DELAY = 500;
public static final String TAG = ForegroundCallbacks.class.getName();
public interface Listener {
public void onBecameForeground(Activity activity);
public void onBecameBackground(Activity activity);
}
private static ForegroundCallbacks instance;
private boolean foreground = false, paused = true;
private Handler handler = new Handler();
private List<Listener> listeners = new CopyOnWriteArrayList<Listener>();
private Runnable check;
public static ForegroundCallbacks init(Application application){
if (instance == null) {
instance = new ForegroundCallbacks();
application.registerActivityLifecycleCallbacks(instance);
}
return instance;
}
public static ForegroundCallbacks get(Application application){
if (instance == null) {
init(application);
}
return instance;
}
public static ForegroundCallbacks get(Context ctx){
if (instance == null) {
Context appCtx = ctx.getApplicationContext();
if (appCtx instanceof Application) {
init((Application)appCtx);
}
throw new IllegalStateException(
"Foreground is not initialised and " +
"cannot obtain the Application object");
}
return instance;
}
public static ForegroundCallbacks get(){
if (instance == null) {
throw new IllegalStateException(
"Foreground is not initialised - invoke " +
"at least once with parameterised init/get");
}
return instance;
}
public boolean isForeground(){
return foreground;
}
public boolean isBackground(){
return !foreground;
}
public void addListener(Listener listener){
listeners.add(listener);
}
public void removeListener(Listener listener){
listeners.remove(listener);
}
@Override
public void onActivityResumed(final Activity activity) {
paused = false;
boolean wasBackground = !foreground;
foreground = true;
if (check != null)
handler.removeCallbacks(check);
if (wasBackground){
for (Listener l : listeners) {
try {
l.onBecameForeground(activity);
} catch (Exception exc) { }
}
} else {
}
}
@Override
public void onActivityPaused(final Activity activity) {
paused = true;
if (check != null)
handler.removeCallbacks(check);
handler.postDelayed(check = new Runnable(){
@Override
public void run() {
if (foreground && paused) {
foreground = false;
for (Listener l : listeners) {
try {
l.onBecameBackground(activity);
} catch (Exception exc) {
}
}
} else {
}
}
}, CHECK_DELAY);
}
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {}
@Override
public void onActivityStarted(Activity activity) {}
@Override
public void onActivityStopped(Activity activity) {}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {}
@Override
public void onActivityDestroyed(Activity activity) {}
}
在Application使用
private MediaPlayer mediaPlayer = null;
ForegroundCallbacks.init(this);
ForegroundCallbacks.get().addListener(new ForegroundCallbacks.Listener() {
@Override
public void onBecameForeground(Activity activity) {
//在前台
//todo 保活1
if (null!=mediaPlayer) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer=null;
}
}
@Override
public void onBecameBackground(Activity activity) {
//在后台
// todo 保活1 根据音乐资源文件创建MediaPlayer对象 设置循环播放属性 开始播放
if (null == mediaPlayer) {
mediaPlayer = MediaPlayer.create(activity, R.raw.no_notice);
mediaPlayer.setLooping(true);
}
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
}
}
});
通知权限保活
...