2014-09-01 36 views
6

Bu soruda göndereceğim kodla tamamen ve çok kolay bir şekilde yeniden oluşturabileceğiniz bir uygulamam var. İşte Bildiri dosyası var:Bir WakefulBroadcastReceiver'dan IntentService nasıl başlatılır

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

Gördüğünüz gibi, aynı paket içinde bir etkinlik, bir (uykusuz) yayın alıcısı ve bir intentservice, tümünü içerir. etkinlik burada kod, açılışında başladı alır:

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

Bu başarıyla TestReceiver ait onReceive fonksiyonunu tetikler. şeyler olsa yanlış burasıdır

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

} 

, ben onReceive işlevinde bir kesme noktası yerleştirilir ve kesinlikle çağrılır. Ancak, MonitorService sınıfına asla ulaşılamıyor. onHandleEvent işlevinde bir kesme noktası yerleştirdim, ancak bu kadar uzağa gitmiyor gibi görünüyor. İşte bu sınıf için kod: Eğer TestReceiver sınıfında yorumladı hat anlaşılacağı gibi

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

    } 

} 

, ben örtük bir niyet yerine açık bir birini kullanarak denedim. Ayrıca this question'u okudum ve orada bahsedilen her şeyi denedim. Burada bir şey mi eksik? Bunu bir taklitçide çalıştırıyorum (Nexus7 API L).

Burada eksik olduğum bir şey var mı?

+0

Üzgünüm için,? Ne işe yarar? –

cevap

9

Application Manifest adresinde <intentservice> etiketi bulunmamaktadır. IntentService, Service'un bir alt sınıfıdır, bu nedenle bildirimde hizmet olarak bildirmeniz gerekir. Tam olarak bu kodların amacı ne


Değişim

<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

Teşekkürler, sadece bu kadar basit olduğunu biliyordum. Acaba neden bir türlü hata alamadım? – overactor

+0

@overactor En çok hoşgeldiniz :) –

İlgili konular