Java 如何从WakefulBroadcastReceiver启动IntentService
我有一个应用程序,您应该能够使用我将在这个问题中发布的代码完全轻松地重新创建它。这是清单文件:Java 如何从WakefulBroadcastReceiver启动IntentService,java,android,broadcastreceiver,android-intentservice,Java,Android,Broadcastreceiver,Android Intentservice,我有一个应用程序,您应该能够使用我将在这个问题中发布的代码完全轻松地重新创建它。这是清单文件: <?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:versi
<?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>
这将成功触发TestReceiver
的onReceive
功能
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);
}
}
这就是问题所在,我在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);
}
}
}
从TestReceiver
类中的注释行可以看出,我尝试使用隐式意图而不是显式意图。我也读过并尝试过那里提到的所有东西。我是不是遗漏了什么?我在模拟器(Nexus7API L)上运行这个
这里有我遗漏的东西吗?中没有标记为
IntentService
是Service
的子类,因此需要在清单中将其声明为Service
改变
<intentservice
android:name="com.example.broadcasttest.MonitorService"
android:enabled="true" >
<intent-filter>
<action android:name="com.example.broadcasttest.MonitorService" />
</intent-filter>
</intentservice>
到
谢谢,要是我知道事情就这么简单就好了。我想知道为什么我没有收到任何错误。对不起,这些代码的目的是什么?它做什么?
<intentservice
android:name="com.example.broadcasttest.MonitorService"
android:enabled="true" >
<intent-filter>
<action android:name="com.example.broadcasttest.MonitorService" />
</intent-filter>
</intentservice>
<service
android:name="com.example.broadcasttest.MonitorService"
android:enabled="true" >
<intent-filter>
<action android:name="com.example.broadcasttest.MonitorService" />
</intent-filter>
</service>