The concept of BroadcastReceiver is some what which very important for event driven app and it help us a lot its pure object oriented nature is really helpful.Here is small snipped for a small scheduler using BroadcastReceiver.
import android.os.Bundle; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.util.Log; public class MainActivity extends Activity { static IntentFilter mIntentFilter; static { mIntentFilter = new IntentFilter(); mIntentFilter.addAction(Intent.ACTION_DATE_CHANGED); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); registerReceiver(mDateReceiver, mIntentFilter); } private final BroadcastReceiver mDateReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { scheduleTask(); } private void scheduleTask() { Log.i("scheduleTask", "scheduleTask"); } }; }
No comments:
Post a Comment