方法onHandleIntent()不会被调用 [英] Method onHandleIntent() does not get called

查看:815
本文介绍了方法onHandleIntent()不会被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几个小时的研究后,我终于咨询官方帮助。为什么不 onHandleIntent()被调用?是不是有什么错在这里?

在主要活动的onCreate()

  MSERVICE =新的意图(背景下,xyz.class);
startService(MSERVICE);
 

这是国际空间站的。该 onStartCommand()被调用,而不是 onHandleIntent()

 包com.autoalbumwallaperplus;

进口android.app.IntentService;
进口android.content.Intent;
进口android.widget.Toast;

公共类XYZ扩展IntentService {
    公共XYZ(){
        超级(BMP);
    }

    @覆盖
    公众诠释onStartCommand(意向意图,诠释标志,诠释startId){
        Toast.makeText(这一点,onStartCommand的作品!,Toast.LENGTH_SHORT).show();
        返回super.onStartCommand(意向,标志,startId);
    }

    @覆盖
    保护无效onHandleIntent(意向workIntent){
        Toast.makeText(这一点,!onHandleIntent作品,Toast.LENGTH_SHORT).show();
    }
}
 

这是OnHandleIntent里面

 字符串的ImagePath = workIntent.getStringExtra(字符串);
    Toast.makeText(这一点,它的工作原理,Toast.LENGTH_SHORT).show();
    DisplayMetrics displayMetrics =新DisplayMetrics();
    窗口管理器,HI =((窗口管理器)getBaseContext()getSystemService(Context.WINDOW_SERVICE)。);
    INT高= displayMetrics.heightPixels;
    INT宽度= displayMetrics.widthPixels<< 2;

    // ...首先去code与inJustDe codeBounds = true来检查尺寸
    最后BitmapFactory.Options选项=新BitmapFactory.Options();
    options.inJustDe codeBounds = TRUE;
    位图德codedSampleBitmap = BitmapFactory.de codeFILE(的ImagePath,期权);

    // ...计算inSampleSize
    options.inSampleSize = calculateInSampleSize(选项,宽,高);

    与inSampleSize集// ...德code位图
    options.inJustDe codeBounds = FALSE;
    德codedSampleBitmap = BitmapFactory.de codeFILE(的ImagePath,期权);

    // ... 设置壁纸
    //上下文语境= getApplicationContext();
    WallpaperManager WM = WallpaperManager.getInstance(本);

    尝试 {
        wm.setBitmap(德codedSampleBitmap);
    }赶上(IOException异常E){
    }
 

解决方案

可能是你的意图服务没有启动,因为你是覆盖 onStartCommand()方法的android文档说:

  

你不应该重写此方法(onStartCommand())为您   IntentService。相反,覆盖 onHandleIntent(意向),其中,   当IntentService接收到启动请求系统调用。


希望如此,这将帮助你。

After many hours of researching I am finally consulting official help. Why does not onHandleIntent() get called? Is there something wrong here?

In main activity onCreate():

mService = new Intent(context, xyz.class);
startService(mService);

That iss it. The onStartCommand() gets called, but not onHandleIntent()

package com.autoalbumwallaperplus;

import android.app.IntentService;
import android.content.Intent;
import android.widget.Toast;

public class xyz extends IntentService {
    public xyz() {
        super("bmp");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this,"onStartCommand works!", Toast.LENGTH_SHORT).show();
        return super.onStartCommand(intent,flags,startId);
    }

    @Override
    protected void onHandleIntent(Intent workIntent) {
        Toast.makeText(this,"onHandleIntent works!", Toast.LENGTH_SHORT).show();
    }
}

This is inside the OnHandleIntent

    String imagepath = workIntent.getStringExtra("String");
    Toast.makeText(this, "it works" , Toast.LENGTH_SHORT).show();
    DisplayMetrics displayMetrics = new DisplayMetrics();
    WindowManager hi = ((WindowManager) getBaseContext().getSystemService(Context.WINDOW_SERVICE));
    int height = displayMetrics.heightPixels;
    int width = displayMetrics.widthPixels << 2;

    // ... First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    Bitmap decodedSampleBitmap = BitmapFactory.decodeFile(imagepath, options);

    // ... Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, width, height);

    // ... Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    decodedSampleBitmap = BitmapFactory.decodeFile(imagepath, options);

    // ... Set Wallpaper
    //Context context = getApplicationContext();
    WallpaperManager wm = WallpaperManager.getInstance(this);

    try {
        wm.setBitmap(decodedSampleBitmap);
    } catch (IOException e) {
    }

解决方案

May be your intent service isn't starting because you are overriding onStartCommand() method as android documentation says:

"You should not override this method(onStartCommand()) for your IntentService. Instead, override onHandleIntent(Intent), which the system calls when the IntentService receives a start request."


Hope so this will help you

这篇关于方法onHandleIntent()不会被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆