OnHandleIntent()没有在IntentService中调用 [英] OnHandleIntent() not called in IntentService

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

问题描述

我知道之前已经问过这个问题,但我已经找到了我能找到的所有答案,但仍然无法解决问题。

I know this question has been asked before, but I've been over all of the answers I could find and still haven't been able to solve the problem.

问题是当BroadcastReceiver启动时,不会调用IntentService onHandleIntent()。奇怪的是构造函数运行(我可以通过Log输出看到)。
这是我的代码:

The issue is that when by BroadcastReceiver starts the IntentService onHandleIntent() isn't called. Weirdly enough the constructor does run (as I can see by the Log output). This is my code:

NoLiSeA.class
(此类包含启动我服务的BroadcastReceiver)

NoLiSeA.class (This class contains the BroadcastReceiver that starts my service)

public void toProcess(StatusBarNotification sbn) {  
    LocalBroadcastManager.getInstance(this).registerReceiver(notificationForwarder, new IntentFilter("to_forward"));
    Intent intent = new Intent("to_forward");
    intent.putExtra("sbn", sbn);
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
    Log.i("NoLiSe.TAG", "toProcess");
}

private BroadcastReceiver notificationForwarder = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
            Log.i("NoLiSe.TAG", "BroadCastReceiver.onReceive");
            Intent i = new Intent(context, CoreTwoA.class);
            i.putExtras(intent);
            startService(i);
        }
    }
};

CoreTwoA.class
(这是IntentService.onHandleIntent由于控制台中没有日志文本,因此我无法调用()。

CoreTwoA.class (This is the IntentService. onHandleIntent() is not called as I can see due to no log text in the console.)

public class CoreTwoA extends IntentService {

   private TextToSpeech mtts;

   public CoreTwoA() {
       super("TheCoreWorker");
       Log.d("source", "exception", new Exception());
       Log.i("CoreTwoA.TAG", "Constructor");
   }

   @Override
   protected void onHandleIntent(Intent intent) {
       Log.i("CoreTwoA.TAG", "onHandleIntent");

   }
}

AndroidManifest.xml

    <service
        android:name=".CoreTwoA"
        android:label="@string/service_name"
        android:exported="false">
    </service>

更新

基于下面的讨论,我能够将问题缩小到BroadCastReceiver中的以下代码行:

So based on discussions below, I was able to narrow down the problem to the following line of code in the BroadCastReceiver:

i.putExtra("sbn", sbn) 

如果我将其删除,即不添加额外内容intent,然后我的IntentService中的onHandleIntent()方法运行。

If I remove it, i.e. add no extras to the intent, then my the onHandleIntent() method in my IntentService does run.

如果包含它,则onHandleIntent()不会运行,并且我的IntentService的构造函数中的Log.d()将以下内容写入logcat

If it is included, onHandleIntent() doesn't run and the following is written to logcat by the Log.d() in the Constructor of my IntentService

06-10 19:40:35.355 25094-25094/com.dezainapps.myapp D/source: exception
                                                                       java.lang.Exception
                                                                           at com.dezainapps.myapp.CoreTwoA.<init>(CoreTwoA.java:20)
                                                                           at java.lang.Class.newInstance(Native Method)
                                                                           at android.app.ActivityThread.handleCreateService(ActivityThread.java:2859)
                                                                           at android.app.ActivityThread.-wrap4(ActivityThread.java)
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1427)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                           at android.os.Looper.loop(Looper.java:148)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
06-10 19:40:35.355 25094-25094/com.dezainapps.myapp I/CoreTwoA.TAG: Constructor

为什么通过Intent将实现Parcelable的StatusBarNotification对象传递给IntentService的任何想法都不起作用?
奇怪的是,通过intent(参见代码)从我的toProcess()方法广播相同的StatusBarNotfication sbn对象确实有效。

Any ideas why passing a StatusBarNotification object, that implements Parcelable, to a IntentService via an Intent doesn't work? Oddly enough broadcasting the same StatusBarNotfication sbn object from my toProcess() method via an intent (see code) does work.

推荐答案

我遇到与OP相同的问题。

I had the same problem as the OP.

问题原因是我传递了一个巨大的 ArrayList (约4000 Parcelable 元素)通过 Intent 启动 IntentService 。由于它在客户的设备上无声地失败,因此诊断非常困难,因此我没有得到ACRA错误报告。

The issue turned out to be that I was passing a huge ArrayList (of about 4000 Parcelable elements) via the Intent that starts the IntentService. It was extremely difficult to diagnose as it failing 'silently' on a customer's device, so I got no ACRA error report.

无论如何,我使用了一个快速而肮脏的技巧来修复问题 - 基本上将 ArrayList 存储在我用来设置/获取ArrayList的静态元素中,而不是尝试通过Intent传递它。

Anyway, I used a quick and dirty trick to fix the problem - basically to store the ArrayList in a static element that I used to set/get the ArrayList rather than try to pass it through the Intent.

在进一步调查(对其他设备进行一些测试)时,我发现抛出了 TransactionTooLargeException 此处有更多讨论

On further investigation (doing some tests other devices) I found a TransactionTooLargeException being thrown. More discussion on that here.

如果有人想复制,这是我的测试代码:

Here's my test code if anyone would like to replicate:

测试代码

如果您想使其正常工作,请将 4000 值更改为更小的值。

If you want to make it work, change the 4000 value to something much smaller.

ArrayList<ParcelableNameValuePair> testArrayList = new ArrayList<>();
for (int i = 0; i < 4000; i++) {
    testArrayList.add(new ParcelableNameValuePair("name" + i, "value" + i));
}
TestIntentService.startAction(AdminHomeActivity.this, testArrayList);

TestIntentService.java

public class TestIntentService extends IntentService {

    private static final String LOG_TAG = TestIntentService.class.getSimpleName();

    private static final String TEST_ACTION = "com.example.action.FOO";

    private static final String EXTRA_PARAM1 = "com.example.extra.PARAM1";
    private static final String EXTRA_PARAM2 = "com.example.extra.PARAM2";

    public TestIntentService() {
        super("TestIntentService");
    }

    public static void startAction(Context context, ArrayList<ParcelableNameValuePair> testArrayList) {

        Log.d(LOG_TAG, "1. startAction()");
        Utilities.makeToast(context, "1. startAction()");

        try {
            int arrayListSize = (testArrayList == null) ? -1 : testArrayList.size();
            Log.d(LOG_TAG, "2. arrayListSize: " + arrayListSize);
            Utilities.makeToast(context, "2. arrayListSize: " + arrayListSize);

            Intent intent = new Intent(context, TestIntentService.class);
            intent.setAction(TEST_ACTION);
            //intent.putExtra(EXTRA_PARAM1, testArrayList);
            intent.putParcelableArrayListExtra(EXTRA_PARAM2, testArrayList);

            /**
             * This line should result in a call to onHandleIntent() but, if we're sending a huge ArrayList, it doesn't...
             */
            context.startService(intent);
        }
        catch(Exception e) {
            Log.e(LOG_TAG, "Exception starting service", e);
            Utilities.makeToast(context, "Exception starting service: " + e);
        }
    }

    @Override
    protected void onHandleIntent(Intent intent) {

        Log.d(LOG_TAG, "3. onHandleIntent()");
        Utilities.makeToast(getApplicationContext(), "3. onHandleIntent()");

        try {
            if (intent != null) {
                final String action = intent.getAction();
                if (TEST_ACTION.equals(action)) {

                    ArrayList<ParcelableNameValuePair> testArrayList = intent.getParcelableArrayListExtra(EXTRA_PARAM1);
                    int testArrayListSize = (testArrayList == null) ? -1 : testArrayList.size();
                    Log.d(LOG_TAG, "4. testArrayListSize: " + testArrayListSize);
                    Utilities.makeToast(getApplicationContext(), "4. testArrayListSize: " + testArrayListSize);

                    ArrayList<ParcelableNameValuePair> testArrayList2 = intent.getParcelableArrayListExtra(EXTRA_PARAM2);
                    int testArrayList2Size = (testArrayList2 == null) ? -1 : testArrayList2.size();
                    Log.d(LOG_TAG, "5. testArrayList2Size: " + testArrayList2Size);
                    Utilities.makeToast(getApplicationContext(), "5. testArrayList2Size: " + testArrayList2Size);

                }
            }
        }
        catch(Exception e) {
            Log.e(LOG_TAG, "Exception handling service intent", e);
            Utilities.makeToast(getApplicationContext(), "Exception handling service intent: " + e);
        }
    }

}

ParcelableNameValuePair.java

public class ParcelableNameValuePair implements Parcelable {

    private String name, value;

    public ParcelableNameValuePair(String name, String value) {
        this.name = name;
        this.value = value;
    }

    public String getName() {
        return name;
    }

    public String getValue() {
        return value;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel out, int flags) {
        out.writeString(name);
        out.writeString(value);
    }

    public static final Parcelable.Creator<ParcelableNameValuePair> CREATOR = new Parcelable.Creator<ParcelableNameValuePair>() {
        public ParcelableNameValuePair createFromParcel(Parcel in) {
            return new ParcelableNameValuePair(in);
        }

        public ParcelableNameValuePair[] newArray(int size) {
            return new ParcelableNameValuePair[size];
        }
    };

    private ParcelableNameValuePair(Parcel in) {
        name = in.readString();
        value = in.readString();
    }

}

就像我说的那样,我使用了一个快速而肮脏的解决方案来解决这个问题。我认为更好的解决方案是让应用程序将 ArrayList 写入文件系统,然后通过Intent传递对该文件的引用(例如,文件名/路径)然后让 IntentService 检索文件内容并将其转换回 ArrayList

Like I say, I used a quick and dirty solution to get around this problem. I think a better solution would be for the app to write the ArrayList to the file system, then pass a reference to that file (e.g., filename/path) via the Intent to the IntentService and then let the IntentService retrieve the file contents and convert it back to an ArrayList.

IntentService 完成了文件,它应该删除它或通过本地广播将指令传回应用程序以删除它创建的文件(传回提供给它的相同文件引用)。

When the IntentService has done with the file, it should either delete it or pass the instruction back to the app via a Local Broadcast to delete the file that it created (passing back the same file reference that was supplied to it).

这篇关于OnHandleIntent()没有在IntentService中调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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