Back to project page passwords-by-harry.
The source code is released under:
Apache License
If you think the Android project passwords-by-harry listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.github.harryfrommarydelde.passwordsbyharry; //from w w w. j av a2s .co m import android.app.Activity; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.content.SharedPreferences; import android.os.Bundle; import android.os.IBinder; import android.preference.PreferenceManager; import android.view.Menu; import android.view.MenuItem; /** * Created by harry on 9/7/13. */ abstract public class DataServiceConnectedActivity extends Activity implements SharedPreferences.OnSharedPreferenceChangeListener { protected SharedPreferences settings; protected boolean dataServiceConnected = false; protected DataService.DataBinder dataBinder; private ServiceConnection dataConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { dataBinder = (DataService.DataBinder) service; dataServiceConnected = true; localOnServiceConnected(dataBinder); } @Override public void onServiceDisconnected(ComponentName name) { dataServiceConnected = false; } }; abstract protected void localOnServiceConnected(DataService.DataBinder dataBinder); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); PreferenceManager.setDefaultValues(this, R.xml.preferences, false); settings = PreferenceManager.getDefaultSharedPreferences(this); settings.registerOnSharedPreferenceChangeListener(this); bindService(new Intent(this, DataService.class), dataConnection, BIND_AUTO_CREATE); } @Override protected void onDestroy() { super.onDestroy(); if (dataServiceConnected) { unbindService(dataConnection); dataServiceConnected = false; } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. // we are currently using a default menu for all activities, if we later have custom // menus for some activities we will add a constructor to take an alternate menu resource. getMenuInflater().inflate(R.menu.general_menu, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case (R.id.action_settings): Intent settings = new Intent(this, SettingsActivity.class); startActivity(settings); break; } return false; } }