FreeMusic新增功能
1.按住Home鍵谍婉,back鍵,或recent鍵 彈出提示框
難點(diǎn):
1.當(dāng)應(yīng)用程序退出的時(shí)候镀钓,dialog 依賴所在Activity的context穗熬,而應(yīng)用程序退出的時(shí)候 Activity銷毀,所以context隨之銷毀
解決方案:開(kāi)啟服務(wù)或廣播丁溅,在其中開(kāi)啟系統(tǒng)級(jí)別的對(duì)話框
2.關(guān)于Home鍵唤蔗,Recent鍵退出時(shí)監(jiān)聽(tīng)事件 位置,需求是要對(duì)所有的Activity生效
解決方案:
1.在Application的on Create方法進(jìn)行注冊(cè)窟赏,在onTerminal方法里進(jìn)行反注
存在的問(wèn)題:因?yàn)轫?xiàng)目采取的是多進(jìn)程妓柜,所以onCreate方法會(huì)被調(diào)用多次
會(huì)多次注冊(cè),產(chǎn)生錯(cuò)亂問(wèn)題饰序,故此方法舍棄
2.在BaseActivity里onResume方法里進(jìn)行注冊(cè)领虹,onPause里進(jìn)行反注冊(cè)
下面貼代碼:
1.對(duì)話框代碼
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);//需要添加的語(yǔ)句
dialog.show();
}
}
在廣播或服務(wù)中調(diào)用
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);
}
注意:服務(wù)的時(shí)候recent鍵殺死后會(huì)重啟 (故不建議用服務(wù))