使用ResultReceiver的Android IntentService

In this tutorial, we’ll be using IntentService with a ResultReceiver in our Android Application. We’ll be creating an application which downloads the image from the url, stores it and adds it to a ListView.

在本教程中,我们将在Android应用程序IntentServiceResultReceiver使用。 我们将创建一个应用程序,该应用程序从url下载图像,将其存储并将其添加到ListView。

Android ResultReceiver (Android ResultReceiver)

Android ResultReceiver is used to receive callback results from someone. It’s different from a BroadcastReceiver.

Android ResultReceiver用于接收某人的回调结果。 它与BroadcastReceiver不同。

A BroadcastReceiver receives all kinds of messages. It can receive messages from outside the activity too.

BroadcastReceiver接收各种消息。 它也可以从活动外部接收消息。

A ResultReceiver is specifically used for receiving messages internal to the application. It then can check the result type from the list of result types defined in it. Based on that it can forward the information to the Activity.

ResultReceiver专门用于接收应用程序内部的消息。 然后,它可以从其中定义的结果类型列表中检查结果类型。 基于此,它可以将信息转发给活动。

The IntentService can use the send method to pass on the data to the ResultReceiver.

IntentService可以使用send方法将数据传递给ResultReceiver。

The ResultReceiver has the onReceiveResult callback method which gets called whenever a new result is received. Based on the result code in the onReceiveResult, it performs the action.

ResultReceiver具有onReceiveResult回调方法,每当接收到新结果时都会调用该方法。 根据onReceiveResult的结果代码,它执行操作。

In the following section we’ll be creating an application in which we download the image from the url in the IntentService and then the Service would send the results to the ResultReceiver where based on the result code (type) we’ll do the necessary action.

在以下部分中,我们将创建一个应用程序,在该应用程序中,我们从IntentService中的url下载图像,然后Service将结果发送到ResultReceiver,在其中根据结果代码(类型),我们将执行必要的操作。

Android ResultReceiver示例项目结构 (Android ResultReceiver Example Project Structure)

Android ResultReceiver布局代码 (Android ResultReceiver Layout code)

The code for the activity_main.xml layout file is given below:

下面给出了activity_main.xml布局文件的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/inEnterUrl"
        android:layout_width="match_parent"
        android:hint="Enter URL"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/button" />


    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/inEnterUrl" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Button" />

</RelativeLayout>

listview_item_row.xml

listview_item_row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="10dp">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />

</RelativeLayout>

The code for the IntentService.java class is given below:

下面给出了IntentService.java类的代码:

package com.journaldev.androidintentserviceresultreceiver;

import android.app.IntentService;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.ResultReceiver;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class MyIntentService extends IntentService {

    public static final int DOWNLOAD_SUCCESS = 2;
    public static final int DOWNLOAD_ERROR = 3;

    public MyIntentService() {
        super(MyIntentService.class.getName());
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        String url = intent.getStringExtra("url");
        final ResultReceiver receiver = intent.getParcelableExtra("receiver");
        Bundle bundle = new Bundle();

        File downloadFile = new File(Environment.getExternalStorageDirectory() + "/journaldevFile.png");
        if (downloadFile.exists())
            downloadFile.delete();
        try {
            downloadFile.createNewFile();
            URL downloadURL = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) downloadURL
                    .openConnection();
            InputStream is = conn.getInputStream();
            FileOutputStream os = new FileOutputStream(downloadFile);
            byte buffer[] = new byte[1024];
            int byteCount;
            while ((byteCount = is.read(buffer)) != -1) {
                os.write(buffer, 0, byteCount);
            }
            os.close();
            is.close();

            String filePath = downloadFile.getPath();
            bundle.putString("filePath", filePath);
            receiver.send(DOWNLOAD_SUCCESS, bundle);

        } catch (Exception e) {
            receiver.send(DOWNLOAD_ERROR, Bundle.EMPTY);
            e.printStackTrace();
        }
    }
}

Here we download the url string we get from the Activity via Intent and store it in a file. We later send it to the ResultReceiver.

在这里,我们通过Intent下载从Activity获得的url字符串,并将其存储在文件中。 稍后我们将其发送到ResultReceiver。

DO NOT forget to set the service in the Manifest file. Also add the Internet Permission.
不要忘记在清单文件中设置服务。 还添加Internet权限。

In our MainActivity.java we do the following:

在我们的MainActivity.java中,执行以下操作:

package com.journaldev.androidintentserviceresultreceiver;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.ResultReceiver;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    Context mContext;

    public static final int PERMISSION_EXTERNAL_STORAGE = 101;
    ListView listView;
    ArrayList<Bitmap> bitmapArrayList = new ArrayList<>();
    ImageResultReceiver imageResultReceiver;

    Button button;
    EditText inputText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mContext = this;
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_EXTERNAL_STORAGE
            );
        }

        button = findViewById(R.id.button);
        inputText = findViewById(R.id.inEnterUrl);
        inputText.setText("https://www.android.com/static/2016/img/share/andy-lg.png");
        listView = findViewById(R.id.listView);

        imageResultReceiver = new ImageResultReceiver(new Handler());

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent startIntent = new Intent(MainActivity.this,
                        MyIntentService.class);
                startIntent.putExtra("receiver", imageResultReceiver);
                startIntent.putExtra("url", inputText.getText().toString().trim());
                startService(startIntent);

            }
        });


    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case PERMISSION_EXTERNAL_STORAGE:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                } else {
                    Toast.makeText(getApplicationContext(), "We need the above permission to save images", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    private class ImageResultReceiver extends ResultReceiver {

        public ImageResultReceiver(Handler handler) {
            super(handler);
        }

        @Override
        protected void onReceiveResult(int resultCode, Bundle resultData) {
            switch (resultCode) {
                case MyIntentService.DOWNLOAD_ERROR:
                    Toast.makeText(getApplicationContext(), "Error in Downloading",
                            Toast.LENGTH_SHORT).show();
                    break;

                case MyIntentService.DOWNLOAD_SUCCESS:
                    String filePath = resultData.getString("filePath");
                    Bitmap bmp = BitmapFactory.decodeFile(filePath);

                    if (bmp != null) {
                        bitmapArrayList.add(bmp);
                        Log.d("API123", "bitmap " + bitmapArrayList.size());
                        listView.setAdapter(new ImagesAdapter(mContext, bitmapArrayList));
                        //imagesAdapter.setItems(bitmapArrayList);
                        Toast.makeText(getApplicationContext(),
                                "Image Download Successful",
                                Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(getApplicationContext(),
                                "Error in Downloading",
                                Toast.LENGTH_SHORT).show();
                    }

                    break;
            }
            super.onReceiveResult(resultCode, resultData);
        }

    }
}
  • We first ask for the Permission for External Storage. For this, we need to add the WRITE_EXTERNAL_STORAGE permission in the Manifest file.

    我们首先要求外部存储权限。 为此,我们需要在清单文件中添加WRITE_EXTERNAL_STORAGE权限。
  • In our edit text we set the URL for the image to be downloaded.

    在我们的编辑文本中,我们设置要下载图像的URL。
  • On Button click the IntentService is invoked and the url is passed.

    在“按钮”上单击,将调用IntentService并传递url。
  • The IntentService returns the result to the ResultReceiver.

    IntentService将结果返回到ResultReceiver。
  • In the ResultReceiver we check for the result code.

    在ResultReceiver中,我们检查结果代码。
  • If it’s a success we add the bitmap to the ListView Adapter and update it.

    如果成功,则将位图添加到ListView适配器并更新它。

The code for the ImagesAdapter.java class is given below:

下面给出了ImagesAdapter.java类的代码:

package com.journaldev.androidintentserviceresultreceiver;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;

public class ImagesAdapter extends ArrayAdapter<Bitmap> {

    private Context mContext;
    private List<Bitmap> bitmapList;

    public ImagesAdapter(@NonNull Context context, ArrayList<Bitmap> list) {
        super(context, R.layout.listview_item_row, list);
        mContext = context;
        bitmapList = list;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        if (convertView == null)
            convertView = LayoutInflater.from(mContext).inflate(R.layout.listview_item_row, parent, false);

        Bitmap bitmap = bitmapList.get(position);

        ImageView image = convertView.findViewById(R.id.imageView);
        image.setImageBitmap(bitmap);

        return convertView;
    }


    @Override
    public int getCount() {
        return bitmapList.size();
    }
}

Android ResultReceiver应用程序输出 (Android ResultReceiver App Output)

The output of the above application in action is given below:

上面应用程序的输出如下:

This brings an end to this tutorial. You can download the final AndroidIntentServiceResultReceiver project from the link below:

本教程到此结束。 您可以从下面的链接下载最终的AndroidIntentServiceResultReceiver项目:

GitHub Repository. GitHub Repository下载Android Studio项目代码。

翻译自: https://www.journaldev.com/20743/android-intentservice-resultreceiver

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值