2014-09-01 135 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函數中放置了一個斷點,但似乎它從來沒有那麼遠。下面是這個類的代碼:

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類的註釋行告訴,我使用隱含的意圖,而不是一個明確的一個嘗試。我也讀了this question並嘗試了那裏提到的一切。我在這裏錯過了什麼嗎?我在模擬器上運行(Nexus7 API L)。

有什麼我在這裏失蹤?

+0

對不起,究竟是這些代碼的目的是什麼?它有什麼作用? – 2015-05-30 12:05:18

回答

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> 
+0

謝謝,如果我只知道那很簡單。 我想知道爲什麼我沒有得到任何形式的錯誤。 – overactor 2014-09-01 08:56:27

+0

@overactor最受歡迎:) – 2014-09-01 08:58:40

相關問題