Skip to content

Instantly share code, notes, and snippets.

@drakeet
Last active June 13, 2016 08:09
Show Gist options
  • Save drakeet/91a15b8ae3f6e1ed328181c7c3f36bbf to your computer and use it in GitHub Desktop.
Save drakeet/91a15b8ae3f6e1ed328181c7c3f36bbf to your computer and use it in GitHub Desktop.
public class MainActivity extends AppCompatActivity implements Updatable {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.text);
EventBus.getRepository().addUpdatable(this);
}
@Override public void update() {
if (EventBus.getRepository().get() instanceof String) {
Log.d("MainActivity", "update");
textView.setText("呵呵, 我起来了. (黑月亮脸)");
}
}
@Override protected void onDestroy() {
super.onDestroy();
EventBus.getRepository().removeUpdatable(this);
}
public static class EventBus {
private static class LazyLoad {
static final BusRepository bus = new BusRepository();
}
@NonNull
public static MutableRepository<Object> getRepository() {
return LazyLoad.bus;
}
}
public static class Inhibitor extends IntentService {
static final String UPDATE = "me.drakeet.Inhibitor.update";
public Inhibitor() {
super("Inhibitor");
}
@Override protected void onHandleIntent(Intent intent) {
Log.d("Inhibitor", "呵呵, 我起来了");
EventBus.getRepository().accept(UPDATE);
}
}
public static class KeepAliveReceiver extends BroadcastReceiver {
@Override public void onReceive(Context context, Intent intent) {
Intent restart = new Intent(context, Inhibitor.class);
context.startService(restart);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment