User Tools

Site Tools


android_learning:headfirst_android_development_notes:chapter_13

This is an old revision of the document!


Chapter 13

Under development.

p. 544

The File > New > Service menu has changed in Android Studio 1.5.1. Now there are explicit entries for the two Intent classes.

p. 547: Code block

DelayedMessageService.java
package com.hfad.joke;
 
import android.app.IntentService;
import android.content.Intent;
import android.util.Log;
 
public class DelayedMessageService extends IntentService {
    public static final String EXTRA_MESSAGE = "message";
 
    public DelayedMessageService() {
        super("DelayedMessageService");
    }
 
    @Override
    protected void onHandleIntent(Intent intent) {
        synchronized (this) {
            try {
                wait(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        String text = intent.getStringExtra(EXTRA_MESSAGE);
        showText(text);
    }
 
    private void showText(final String text) {
        Log.v("DelayedMessageService", "The message is: " + text);
    }
}

p. 550: Code block

    public void onClick(View view) {
        Intent intent = new Intent(this, DelayedMessageService.class);
        intent.putExtra(DelayedMessageService.EXTRA_MESSAGE,
                getResources().getString(R.string.button_response));
        startService(intent);
    }

p. 553

In other words, while most Intent methods run off the main thread, but onStartCommand() runs on the main thread.

p. 554

DelayedMessageService.java
package com.hfad.joke;
 
import android.app.IntentService;
import android.content.Intent;
import android.os.Handler;
import android.widget.Toast;
 
public class DelayedMessageService extends IntentService {
    public static final String EXTRA_MESSAGE = "message";
    private Handler handler;
 
    public DelayedMessageService() {
        super("DelayedMessageService");
    }
 
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // This method runs on the main thread, so the Handler that's
        // instantiated here will also be running on the main thread.    
        handler = new Handler();
        return super.onStartCommand(intent, flags, startId);
    }
 
    @Override
    protected void onHandleIntent(Intent intent) {
        synchronized (this) {
            try {
                wait(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        String text = intent.getStringExtra(EXTRA_MESSAGE);
        showText(text);
    }
 
    private void showText(final String text) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getApplicationContext(), text,
                    Toast.LENGTH_LONG).show();
            }
        });
    }
}
android_learning/headfirst_android_development_notes/chapter_13.1461973596.txt.gz · Last modified: 2016/04/29 23:46 (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki