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 に答える