Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your activity will look something like this ..</p> <pre><code>import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class GenerateBroadcastActivity extends Activity { Button bt ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.generate_broadcast); bt = (Button) findViewById(R.id.bt1); bt.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { startService(new Intent(getApplicationContext(),MyNewService.class)); } }); } @Override protected void onPause() { super.onPause(); unregisterReceiver(receiver); } @Override protected void onResume() { super.onResume(); registerReceiver(receiver, new IntentFilter("BROADCAST_UNIQUE_NAME")); } private BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { //take out data from intent int progressValue = intent.getIntExtra("progressValue",0); // just for instance , you can set the progress in your progress bar //defined globally in your app progressBar.setProgress(progressValue); Toast.makeText(context, "Broadcast coming with Progress value" + progressValue, Toast.LENGTH_LONG).show(); } }; } </code></pre> <p>Write your service class like this ...</p> <pre><code>import android.app.IntentService; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Intent; import android.util.Log; public class MyNewService extends IntentService { public MyNewService() { super("Service Name"); } @Override protected void onHandleIntent(Intent intent) { //Do some background task . int progressValue = 20 ; //Send broadcast like this Intent broadcast = new Intent(); // name should be same as specified in your activity class onResume method broadcast.setAction("BROADCAST_UNIQUE_NAME"); //send extra data with your broadcast broadcast.putExtra("progressValue",progressValue); //send it now sendBroadcast(broadcast); } } </code></pre> <p>This broadcast wil be received by your broadcast receiver and you will get a message on screen. Refer this link for basics : <a href="http://www.edureka.in/blog/android-tutorials-broadcast-receivers/" rel="nofollow">http://www.edureka.in/blog/android-tutorials-broadcast-receivers/</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload