Java 如何从 WakefulBroadcastReceiver 启动 IntentService

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25601973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:44:39  来源:igfitidea点击:

How to start an IntentService from a WakefulBroadcastReceiver

javaandroidbroadcastreceiverandroid-intentservice

提问by overactor

I have an application, which you should be able to recreate entirely and very easily with the code I'll post in this question. Here's the Manifest file:

我有一个应用程序,您应该能够使用我将在此问题中发布的代码完全且非常轻松地重新创建该应用程序。这是清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.broadcasttest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="19"
        android:targetSdkVersion="21" />

    <uses-permission android:name="android.permission.WAKE_LOCK"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:name="com.example.broadcasttest.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:name="com.example.broadcasttest.TestReceiver"
            android:label="@string/app_name"
            android:enabled="true" >
        </receiver>

        <intentservice 
            android:name="com.example.broadcasttest.MonitorService"
            android:enabled="true" >
            <intent-filter>
                <action android:name="com.example.broadcasttest.MonitorService" />
            </intent-filter>
        </intentservice>
    </application>

</manifest>

As you can see, the contains an activity, a (wakeful) broadcast receiver and an intentservice, all in the same package. The activity gets started at launch, here's the code:

如您所见,它包含一个活动、一个(唤醒的)广播接收器和一个意图服务,所有这些都在同一个包中。活动在启动时开始,代码如下:

package com.example.broadcasttest;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends Activity {

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

        sendBroadcast(new Intent(this, TestReceiver.class));
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

This succesfully triggers the onReceivefunction of TestReceiver.

这成功地触发了 的onReceive功能TestReceiver

package com.example.broadcasttest;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.WakefulBroadcastReceiver;

public class TestReceiver extends WakefulBroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        //Intent service = new Intent("com.example.broadcasttest.MonitorService");
        Intent service = new Intent(context, MonitorService.class);
        startWakefulService(context, service);
    }

}

This is where things go wrong though, I placed a breakpoint in the onReceivefunction and it definitely gets called. However, the MonitorServiceclass never gets reached. I placed a breakpoint in the onHandleEventfunction, but it seems like it never gets that far. Here's the code for this class:

不过,这就是出错的地方,我在onReceive函数中放置了一个断点,它肯定会被调用。但是,MonitorService该类永远不会到达。我在onHandleEvent函数中放置了一个断点,但它似乎永远不会那么远。下面是这个类的代码:

package com.example.broadcasttest;

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

public class MonitorService extends IntentService {

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

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

    @Override
    protected void onHandleIntent(Intent intent) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            TestReceiver.completeWakefulIntent(intent);
        }

    }

}

As you can tell from the commented line in the TestReceiverclass, I've tried using an implicit intent instead of an explicit one. I've also read this questionand tried everything mentioned there. Am I missing something here? I'm running this on an emulator (Nexus7 API L).

TestReceiver课程中的注释行可以看出,我尝试使用隐式意图而不是显式意图。我也读过这个问题并尝试了那里提到的所有内容。我在这里错过了什么吗?我在模拟器(Nexus7 API L)上运行它。

Is there anything I'm missing here?

有什么我在这里想念的吗?

采纳答案by Pankaj Kumar

There is no tag as <intentservice>in Application Manifest. IntentServiceis a subclass of Service, so you need to declare it as service in manifest.

没有标记为<intentservice>应用程序清单IntentService是 的子类Service,因此您需要在清单中将其声明为服务。



Change

改变

<intentservice 
    android:name="com.example.broadcasttest.MonitorService"
    android:enabled="true" >
        <intent-filter>
            <action android:name="com.example.broadcasttest.MonitorService" />
        </intent-filter>
</intentservice>

to

<service 
    android:name="com.example.broadcasttest.MonitorService"
    android:enabled="true" >
       <intent-filter>
           <action android:name="com.example.broadcasttest.MonitorService" />
       </intent-filter>
</service>