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();
            }
        });
    }
}

pp. 562-563: Code block

DelayedMessageService.java
package com.hfad.joke;
 
import android.app.IntentService;
import android.content.Intent;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.Context;
 
public class DelayedMessageService extends IntentService {
    public static final String EXTRA_MESSAGE = "message";
    public static final int NOTIFICATION_ID = 5453;
 
    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) {
        Intent intent = new Intent(this, MainActivity.class);
        // No, really ... this is a common enough pattern, why doesn't 
        // Android do the backstack/pendingIntent construction at a
        // higher level?
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(MainActivity.class);
        stackBuilder.addNextIntent(intent);
        PendingIntent pendingIntent =
            stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        Notification notification = new Notification.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(getString(R.string.app_name))
            .setAutoCancel(true)
            .setPriority(Notification.PRIORITY_MAX)
            .setDefaults(Notification.DEFAULT_VIBRATE)
            .setContentIntent(pendingIntent)
            .setContentText(text)
            .build();
        NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(NOTIFICATION_ID, notification);
    }
}
android_learning/headfirst_android_development_notes/chapter_13.1461977459.txt.gz · Last modified: 2016/04/30 00:50 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki