2014-09-01 13 views
6

Mam aplikację, którą powinieneś móc odtworzyć całkowicie i bardzo łatwo za pomocą kodu, który opublikuję w tym pytaniu. Oto plik manifestu:Jak uruchomić IntentService od WakefulBroadcastReceiver

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

Jak widać, zawiera aktywność, A (czuwający) odbiornik broadcast oraz intentservice, wszystkie w tym samym opakowaniu. Aktywność rozkręci na szalupie, oto kod:

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

Ten pomyślnie wywołuje funkcję onReceive z 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); 
    } 

} 

To gdzie coś pójdzie nie tak jednak, że umieszczony punkt przerwania w funkcji onReceive i to na pewno jest wywoływana. Jednak klasa MonitorService nigdy nie zostanie osiągnięta. Umieściłem punkt przerwania w funkcji onHandleEvent, ale wygląda na to, że nigdy nie jest tak daleko. Oto kod dla tej klasy:

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

    } 

} 

Jak widać z komentowanej linii w klasie TestReceiver, próbowałem za pomocą niejawnego intencji zamiast wyraźnego jeden. Przeczytałem też this question i wypróbowałem wszystko tam wymienione. Czy coś mi umyka? Używam tego na emulatorze (Nexus7 API L).

Czy czegoś tu brakuje?

+0

przykro mi, co dokładnie jest cel tych kodów? Co robi? –

Odpowiedz

9

Brak tagu jako <intentservice> w Application Manifest. IntentService jest podklasą Service, więc musisz zadeklarować ją jako usługę w manifeście.


Zmień

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

do

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

Dzięki, gdybym tylko wiedział, że to takie proste. Zastanawiam się, dlaczego nie dostałem żadnego błędu. – overactor

+0

@overactor Serdecznie witamy :) –

Powiązane problemy