1. 程式人生 > >Android學習筆記之IntentService

Android學習筆記之IntentService

activit update 使用 pac .get ger foo 異步 tex

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參數

Android學習筆記之IntentService