2014-09-01 2 views
6

나는이 질문에 게시 할 코드로 완전히 쉽고 아주 재현 할 수있는 응용 프로그램을 가지고 있습니다. 여기에 매니페스트 파일입니다 :WakefulBroadcastReceiver에서 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: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> 

당신이 볼 수있는 바와 같이,이 같은 패키지에서 활동하는 (깨어) 방송 수신기와 intentservice, 서비스를 제공합니다. 활동은 여기에 코드입니다, 발사에서 시작됩니다 :

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

이 성공적으로는 TestReceiveronReceive 기능을 트리거합니다. 일이 비록 잘못 곳입니다

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 함수에 중단 점을 배치했지만 결코 그렇게까지는 얻을 수없는 것처럼 보입니다. 다음은이 클래스의 코드는 다음과 같습니다 당신이 TestReceiver 클래스의 주석 줄에서 알 수 있듯이

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

    } 

} 

, 나는 암시 적 의도 대신 명시 적 하나를 사용하여 시도했습니다. 나는 또한 this question을 읽고 거기에 언급 된 모든 것을 시도했다. 내가 여기서 뭔가를 놓치고 있니? 나는 이것을 에뮬레이터 (Nexus7 API L)에서 실행하고 있습니다.

내가 여기에없는 것이 있습니까?

+0

미안 해요에,?는 무엇입니까? –

답변

9

<intentservice>이라는 태그가 Application Manifest에 없습니다. 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> 
+0

고마워, 내가 그걸 간단하게 알았다면. 나는 왜 내가 어떤 종류의 에러도 내지 않았는지 궁금하다. – overactor

+0

@overactor 대부분 환영합니다 :) –

관련 문제