Android BroadcastReceiver完成并打开活动

时间:2015-12-25 18:04:03

标签: android

我在论坛中找到了允许我在设备屏幕关闭时关闭所有活动并打开另一个活动的代码。我在sharedpreference中存储了一个布尔值,当为true时,必须启动BroadcastReceiver。问题是即使布尔值为false,也会启动BroadcastReceiver。

    public class Impostazioni extends AppCompatActivity {
private BroadcastReceiver mReceiver = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.impostazioni);

Switch swChiusura = (Switch) findViewById(R.id.switch1);
SharedPreferences settings_chiusura = getSharedPreferences(CHIUSURA_AUTOMATICA, Context.MODE_PRIVATE);
    boolean vero_falso = settings_chiusura.getBoolean("pref_chiusura_automatica", false);
    if(vero_falso){
        swChiusura.setChecked(true);
    }else{
        swChiusura.setChecked(false);
    }
swChiusura.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked){
                SharedPreferences settings_percorso = getSharedPreferences(CHIUSURA_AUTOMATICA, Context.MODE_PRIVATE);
                SharedPreferences.Editor prefEditor = settings_percorso.edit();
                prefEditor.putBoolean("pref_chiusura_automatica", true);
                prefEditor.apply();
            }else{
                SharedPreferences settings_percorso = getSharedPreferences(CHIUSURA_AUTOMATICA, Context.MODE_PRIVATE);
                SharedPreferences.Editor prefEditor = settings_percorso.edit();
                prefEditor.putBoolean("pref_chiusura_automatica", false);
                prefEditor.apply();
                //disable reciver
                if (mReceiver != null) {
                    unregisterReceiver(mReceiver);
                    mReceiver = null;
                }
            }
        }
    });

 @Override
protected void onPause() {
    super.onPause();
     SharedPreferences settings_chiusura = getSharedPreferences(CHIUSURA_AUTOMATICA, Context.MODE_PRIVATE);
    boolean vero_falso = settings_chiusura.getBoolean("pref_chiusura_automatica", false);
    if (vero_falso) {
        /**
         * initialize receiver
         */
        final IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        mReceiver = new ScreenReceiver();
        registerReceiver(mReceiver, filter);

        // when the screen is about to turn off
        if (ScreenReceiver.wasScreenOn) {
            // this is the case when onPause() is called by the system due to a screen state change
            Log.e("MYAPP", "SCREEN TURNED OFF");
        }
    } else {
        if (mReceiver != null) {
            unregisterReceiver(mReceiver);
            mReceiver = null;
        }
    }
 }

@Override
protected void onDestroy() {
    super.onDestroy();
    if (mReceiver != null) {
        unregisterReceiver(mReceiver);
        mReceiver = null;
    }
}

接收机

public class ScreenReceiver extends BroadcastReceiver {

public static boolean wasScreenOn = true;

@Override
public void onReceive(final Context context, Intent intent) {

    if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
        intent = new Intent(context, Login.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
        wasScreenOn = false;
    } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
        wasScreenOn = true;
    }
}
}

1 个答案:

答案 0 :(得分:0)

这可能是一个糟糕的解决方案,但是,尝试将您的SharedPreferences布尔值放入ScreenReceiver的if块中。因为你知道,当你的屏幕关闭时,它可以在不需要布尔值的情况下工作。我的意思是:

unique=True