如何在应用程序级别调用Intent.ACTION_CLOSE_SYSTEM_DIALOGS

时间:2014-03-05 09:19:40

标签: android broadcastreceiver

当用户点击电源按钮时,我正在尝试显示我的菜单。我正在使用此代码禁用系统电源按钮微调器菜单,但我不想在每个活动上编写此代码。如何调用此服务在应用程序级别禁用系统对话框?

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);

    Log.d("Focus debug", "Focus changed !");

    if (!hasFocus) {
        Log.d("Focus debug", "Lost focus !");

        Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
        sendBroadcast(closeDialog);
    }
}

/* I don't want to write above code on each activity */

import android.widget.Toast;

public class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {

    public void onReceive(final Context context, final Intent intent) {
        // Log.i("HERE", "------------------------------------HERE");
        // Toast.makeText(context, "OFFFFFFFFFFFFFFFF",Toast.LENGTH_LONG).show();
    }
}

<receiver 
    android:name="com.eurosoft.cabtreasure.receiver.CloseSystemDialogsIntentReceiver">
    <intent-filter>
        <action android:name="android.intent.action.CLOSE_SYSTEM_DIALOGS" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</receiver>

1 个答案:

答案 0 :(得分:1)

您可以创建一个新的活动(例如,将其命名为 BaseActivity ),该活动会覆盖onWindowFocusChanged方法。

然后,您可以将需要此onWindowFocusChanged的所有其他活动扩展为BaseActivity,而无需将此方法粘贴到所有活动