6

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

<?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>

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

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);
    }
}

这样就成功触发了 的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);
    }

}

这就是问题所在,我在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,我尝试使用隐式意图而不是显式意图。我也读过这个问题并尝试了那里提到的一切。我在这里错过了什么吗?我在模拟器(Nexus7 API L)上运行它。

我在这里有什么遗漏吗?

4

1 回答 1

9

Application Manifest<intentservice>中没有标签。是 的子类,因此您需要在清单中将其声明为服务。IntentServiceService


改变

<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>
于 2014-09-01T08:45:14.247 回答