Skip to content

Instantly share code, notes, and snippets.

@drakeet drakeet/Inhibitor.java
Last active Jun 13, 2016

Embed
What would you like to do?
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
You can’t perform that action at this time.