Back to project page android-training-tutorial.
The source code is released under:
MIT License
If you think the Android project android-training-tutorial 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 im.ene.dev.zbroadcastreceiver2.services; //from w ww. j a va 2 s . c om import im.ene.dev.zbroadcastreceiver2.MainActivity; import im.ene.dev.zbroadcastreceiver2.receivers.CustomBroadcastReceiver; import im.ene.dev.zbroadcastreceiver2.receivers.CustomBroadcastReceiver.OnBroadcastReceiveListener; import android.app.Service; import android.content.Intent; import android.content.IntentFilter; import android.os.Handler; import android.os.IBinder; public class CustomBroadcastService extends Service implements OnBroadcastReceiveListener { public static final String BROADCAST_SERVICE_ACTION = "im.ene.br2.broadcast_from_service"; public static final String TAG = "im.ene.br2.service"; public static int MAX = 10000; private Intent mBroadcastIntent; private CustomBroadcastReceiver mReceiver; private Handler mHandler; private int counter; @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { mBroadcastIntent = new Intent(CustomBroadcastService.BROADCAST_SERVICE_ACTION); mReceiver = new CustomBroadcastReceiver(); mReceiver.setOnBroadcastReceiveListener(this); registerReceiver(mReceiver, new IntentFilter(MainActivity.BROADCAST_ACTIVITY_ACTION)); mHandler = new Handler(); counter = 0; super.onCreate(); } public Runnable mSendBroadcastRunnable = new Runnable() { @Override public void run() { sendBroadcastToUpdateUI(); mHandler.postDelayed(this, 100); } }; public void sendBroadcastToUpdateUI() { counter += 10; if (counter >= MAX) counter = 0; mBroadcastIntent.putExtra("progress", counter); sendBroadcast(mBroadcastIntent); } @Override public int onStartCommand(Intent intent, int flags, int startId) { mHandler.postDelayed(mSendBroadcastRunnable, 100); return super.onStartCommand(intent, flags, startId); } @Override public void onBroadcastReceiveUpdateUI(Intent intent) { counter = intent.getIntExtra("progress", 1); } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); /* stop/pause receiving broadcast from activity */ try { unregisterReceiver(mReceiver); } catch (Exception er) { er.printStackTrace(); } } }