Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Service, TimerTask and BroadcastReceiver to check for various updates
    primarykey
    data
    text
    <p>I'm trying to create a simple program that does the following:</p> <ol> <li>A service (NewsService) started by my activity (UpdateServiceActivity) checks for news.</li> <li>If news are found (NewsService) sends a broadcast to a receiver (NewsReceiver).</li> <li>Upon receiving the broadcast the receiver (NewsReceiver) should notify the activity (UpdateServiceActivity) that there are news.</li> <li>Upon notification, the activity (UpdateServiceActivity) gets the news and handles them.</li> </ol> <p>So far I'm just working on a simple example. Here is my code so far:</p> <p><strong>UpdateServiceActivity</strong></p> <pre><code>public class UpdateServiceActivity extends Activity implements OnClickListener { private static final String TAG = "UpdateServiceActivity"; Button buttonStart, buttonStop; BroadcastReceiver receiver; @Override public void onCreate( Bundle savedInstanceState ) { super.onCreate( savedInstanceState ); setContentView( R.layout.main ); buttonStart = (Button) findViewById( R.id.buttonStart ); buttonStop = (Button) findViewById( R.id.buttonStop ); buttonStart.setOnClickListener( this ); buttonStop.setOnClickListener( this ); receiver = new NewsReceiver(); } @Override protected void onPause() { super.onPause(); unregisterReceiver( receiver ); } @Override protected void onResume() { super.onResume(); registerReceiver( receiver, new IntentFilter() ); } public void onClick( View src ) { switch( src.getId() ) { case R.id.buttonStart: Log.e( TAG, "onClick: starting service" ); startService( new Intent( this, NewsService.class ) ); break; case R.id.buttonStop: Log.e( TAG, "onClick: stopping service" ); stopService( new Intent( this, NewsService.class ) ); break; } } } </code></pre> <p><strong>NewsService</strong></p> <pre><code>public class NewsService extends Service { public static final String NEWS_INTENT = "bs.kalender.news"; private Timer timer = new Timer(); @Override public IBinder onBind( Intent arg0 ) { return null; } @Override public void onStart( Intent intent, int startId ) { startService(); } private void startService() { timer.scheduleAtFixedRate( new NewsChecker(), 0, 5000 ); } private class NewsChecker extends TimerTask { @Override public void run() { Intent intent = new Intent( getApplicationContext(), NewsReceiver.class ); sendBroadcast( intent ); } } } </code></pre> <p><strong>NewsReceiver</strong></p> <pre><code>public class NewsReceiver extends BroadcastReceiver { @Override public void onReceive( Context context, Intent intent ) { Toast.makeText( context, "Broadcast recieved! There is news!", Toast.LENGTH_SHORT).show(); } } </code></pre> <p><strong>Manifest</strong></p> <pre><code>&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="bs.update" android:versionCode="1" android:versionName="1.0"&gt; &lt;uses-sdk android:minSdkVersion="7" /&gt; &lt;uses-permission android:name="android.permission.INTERNET" /&gt; &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"&gt;&lt;/uses-permission&gt; &lt;application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true"&gt; &lt;activity android:name=".UpdateServiceActivity" android:label="@string/app_name"&gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN" /&gt; &lt;category android:name="android.intent.category.LAUNCHER" /&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;service android:name=".NewsService" /&gt; &lt;receiver android:name=".NewsReceiver" /&gt; &lt;/application&gt; &lt;/manifest&gt; </code></pre> <p>The problems I run in to are:</p> <ul> <li>When hitting the 'Home'-button (the App goes to the background, and on pause is called) the NewsReceiver keeps firing toasts. I was of the understanding that once I unregister the receiver, it shouldn't be availible for receiving broadcast.</li> <li>Even if I hit the button to stop the NewsService, the TimerTask keeps running and posting broadcast.</li> </ul> <p>What am I doing wrong? Is it a general misunderstanding of how Broadcasting/Receiving works? Am I on track and what should be changed to accomplish what I desire?</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