應(yīng)用程序退出后Dialog彈出

FreeMusic新增功能

1.按住Home鍵,back鍵,或recent鍵 彈出提示框

難點(diǎn):

1.當(dāng)應(yīng)用程序退出的時(shí)候,dialog 依賴所在Activity的context,而應(yīng)用程序退出的時(shí)候 Activity銷毀,所以context隨之銷毀

解決方案:開啟服務(wù)或廣播,在其中開啟系統(tǒng)級(jí)別的對(duì)話框

2.關(guān)于Home鍵,Recent鍵退出時(shí)監(jiān)聽事件 位置,需求是要對(duì)所有的Activity生效

解決方案:

1.在Application的on Create方法進(jìn)行注冊(cè),在onTerminal方法里進(jìn)行反注

存在的問題:因?yàn)轫?xiàng)目采取的是多進(jìn)程,所以onCreate方法會(huì)被調(diào)用多次

會(huì)多次注冊(cè),產(chǎn)生錯(cuò)亂問題,故此方法舍棄

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);//需要添加的語句

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容