FreeMusic新增功能
1.按住Home键,back键,或recent键 弹出提示框
难点:
1.当应用程序退出的时候,dialog 依赖所在Activity的context,而应用程序退出的时候 Activity销毁,所以context随之销毁
解决方案:开启服务或广播,在其中开启系统级别的对话框
2.关于Home键,Recent键退出时监听事件 位置,需求是要对所有的Activity生效
解决方案:
1.在Application的on Create方法进行注册,在onTerminal方法里进行反注
存在的问题:因为项目采取的是多进程,所以onCreate方法会被调用多次
会多次注册,产生错乱问题,故此方法舍弃
2.在BaseActivity里onResume方法里进行注册,onPause里进行反注册
下面贴代码:
1.对话框代码
public classPolicyReminderDialog {
public static voidshow(Context context) {
finalBooleanPrefField notShowPolicyAgainPref =newFreeMusicSharePreferences_(context).notShowPolicyAgain();
if(notShowPolicyAgainPref.get()) {
return;
}
finalandroid.app.AlertDialog.Builder builder;
builder =newandroid.app.AlertDialog.Builder(context);
builder.setTitle(R.string.policy_reminder_dialog_title)
.setMessage(R.string.policy_reminder_dialog_desc)
.setNegativeButton(R.string.policy_reminder_dialog_button_cancel, newDialogInterface.OnClickListener() {
@Override
public voidonClick(DialogInterface dialog, intwhich) {
}
})
.setNeutralButton(R.string.policy_reminder_dialog_button_dontshow, newDialogInterface.OnClickListener() {
@Override
public voidonClick(DialogInterface dialog, intwhich) {
notShowPolicyAgainPref.put(true);
}
});
android.app.AlertDialog dialog = builder.create();
dialog.setCanceledOnTouchOutside(false);
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);//需要添加的语句
dialog.show();
}
}
在广播或服务中调用
public classDialogReceiverextendsBroadcastReceiver {
@Override
public voidonReceive(Context context,Intent intent) {
PolicyReminderDialog.show(context);
}
}
back 键
@Override
public voidonBackPressed() {
super.onBackPressed();
Intent intents =newIntent("com.classiclistening.freemusic.PolicyDialog");
sendBroadcast(intents);
}
Home键
privateBroadcastReceiverhomeKeyReceiver=newBroadcastReceiver() {
@Override
public voidonReceive(Context context,Intent intent) {
Intent intents =newIntent("com.classiclistening.freemusic.PolicyDialog");
sendBroadcast(intents);
};
@Override
protected voidonResume() {
super.onResume();
IntentFilter homeFilter =newIntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
registerReceiver(homeKeyReceiver,homeFilter);
}
@Override
protected voidonPause() {
super.onPause();
unregisterReceiver(homeKeyReceiver);
}
注意:服务的时候recent键杀死后会重启 (故不建议用服务)