Android学习笔记之IntentService

Android学习笔记之IntentService

IntentService是继承并处理异步请求的一个类,IntentService内有一个工作线程来处理耗时操作,启动IntentService的方式和启动传统的Service一样,同时,当任务执行完后,IntentService会自动停止,而不需要我们手动去控制或stopSelf()。另外,可以启动IntentService多次,而每一个耗时操作会以工作队列的方式在IntentService的onHandleIntent回调方法中执行,并且,每次只会执行一个工作线程,执行完第一个再执行第二个,以此类推。

这里我使用IntentService来输出一个Notification。

package es.source.code.service;

import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;

import es.source.code.activity.FoodDetailed;
import es.source.code.activity.R;
import es.source.code.model.Food;

public class UpdateService extends IntentService {
    public UpdateService() {
        super("UpdateService");
    }

    public UpdateService(String name) {
        super(name);
    }


    @Override
    protected void onHandleIntent(Intent intent) {
        //update foodview;
        System.out.println("onHandleIntent");
        Bundle bundle =  (Bundle)intent.getBundleExtra("new food");
        String name = (String)bundle.get("name");
        int storage = (int)bundle.get("storage");
        System.out.println(name+storage);
        Food food = new Food("new food", 13, 10, R.drawable.order_item_logo,
                false);
        sendNotification(food);
    }
    private void sendNotification(Food food){
        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel mChannel = new NotificationChannel("channel_01", "name",
                    NotificationManager.IMPORTANCE_HIGH);
            Intent resultIntent = new Intent(this,FoodDetailed.class);
            //intent可以携带参数到指定页面的,这里省略
            //resultIntent.putExtra(key,value);
            //添加数据
            resultIntent.putExtra("newfood", food);
            PendingIntent pendingIntent = PendingIntent.getActivity(this,0,
                    resultIntent,PendingIntent.FLAG_IMMUTABLE);
            notificationManager.createNotificationChannel(mChannel);//注意
            notification = new Notification.Builder(this)
                    .setChannelId("channel_01")
                    .setContentTitle("新菜")
                    .setContentText("New food.name:"+food.getFoodName()+
                            " price:" + food.getPrice() +" kind:"+"coldfood")
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentIntent(pendingIntent)
                    .build();
            notificationManager.notify(1, notification);
        } else {
            Intent resultIntent = new Intent(this,FoodDetailed.class);
            //intent可以携带参数到指定页面的,这里省略
            //resultIntent.putExtra(key,value);
            PendingIntent pendingIntent = PendingIntent.getActivity(this,0,
                    resultIntent,PendingIntent.FLAG_UPDATE_CURRENT);

            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                    .setContentTitle("新菜")
                    .setContentText("New food.name:"+food.getFoodName()+
                            " price:" + food.getPrice() +" kind:"+"coldfood")
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setOngoing(true)
                    .setChannelId("channel_01")//无效
                    .setContentIntent(pendingIntent);
            notification = notificationBuilder.build();
            notificationManager.notify(1, notification);
        }

注意这里的

notification因为版本的原因android8.0以上需要使用channel参数

posted @ 2018-11-08 14:54  PCM  阅读(177)  评论(0编辑  收藏  举报