2013-02-15 230 views
0

我在網絡搜索中採用下面的示例。我對該示例進行了一些修改,但在重新啓動手機後顯示錯誤。我也我的示例中使用粘貼的源代碼,請幫我解決問題在android.permission.RECEIVE_BOOT_COMPLETED中實現服務

在此先感謝

xml文件

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.newtest" 
android:versionCode="1" 
android:versionName="1.0"> 

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> 
<application> 
    <receiver android:name=".BootCompletedIntentReceiver"> 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 
     </intent-filter> 
    </receiver> 
    <service android:name=".BackgroundService"/> 
</application> 
</manifest> 

廣播接收器類

package com.example.newtets; 

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 

public class BootCompletedIntentReceiver extends BroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent intent) { 
    if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) { 
      Intent pushIntent = new Intent(context, BackgroundService.class); 
      context.startService(pushIntent); 
    } 
} 
} 

服務等級

package com.example.newtets; 

import android.app.Service; 
import android.content.Intent; 
import android.os.IBinder; 
import android.widget.Toast; 

public class BackgroundService extends Service { 

@Override 
public IBinder onBind(Intent intent) { 
    // TODO Auto-generated method stub 
    return null; 
} 

@Override 
public void onCreate() { 
    Toast.makeText(this, "OK", Toast.LENGTH_LONG).show(); 
    super.onCreate(); 
} 

} 
+0

什麼錯誤你越來越PLZ也添加日誌的問題 – 2013-02-15 12:33:55

+0

我無法獲得日誌(我不知道如何獲取服務應用程序在啓動手機後執行日誌),重新啓動手機後,應用程序崩潰出現意外錯誤 – Riskhan 2013-02-15 12:35:05

+0

爲什麼如果您要啓動服務而不是活動,請設置FLAG_ACTIVITY_NEW_TASK? – Rafael 2013-02-15 12:43:26

回答

0

我不認爲你需要比較的意圖..

if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) { 
..... 
} 

因爲,它是在BootCompletedIntentReceiver應該BOOT_COMPLETED意圖執行的xml文件抓獲。