2013-03-25 102 views
5

我是Android編程新手。 我有一個接收器在啓動時啓動服務,但似乎從未啓動。你能告訴我我做錯了什麼嗎? 我不知道如何調試它。你能解釋我怎樣才能調試Android的啓動服務呢?Android啓動服務永遠不會啓動

這是我的代碼。謝謝你在前進

Recibidor.java:

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.util.Log; 
import android.widget.Toast; 

public class Recibidor extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Toast.makeText(context, "Iniciando Recibidor", Toast.LENGTH_LONG).show(); 
     final String TAG = "Recibidor"; 
     Log.i(TAG, "Iniciando Recibidor"); 

     if (intent.getAction().equalsIgnoreCase("android.intent.action.BOOT_COMPLETED")) { 
     Toast.makeText(context, "Iniciando Intent", Toast.LENGTH_LONG).show(); 
     Log.i(TAG, "Iniciando Intent"); 

     Intent servicio = new Intent(); 
     servicio.setAction("com.pruebas.Servicio"); 
     context.startService(servicio); 

     Log.i(TAG, "Iniciando Servicio"); 
     Toast.makeText(context, "Iniciando Servicio", Toast.LENGTH_LONG).show(); 
     } 

    } 
} 

Servicio.java

package com.pruebas; 

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

public class Servicio extends Service { 
    private final String TAG = "Servicio"; 


    @Override 
    public IBinder onBind(Intent arg0) { 
     return null; 
    } 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     Log.i(TAG, "ON CREATE"); 
     Toast.makeText(this, "ON CREATE", Toast.LENGTH_LONG).show(); 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     Log.i(TAG, "ON DESTROY"); 
     Toast.makeText(this, "ON DESTROY", Toast.LENGTH_LONG).show(); 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     startForeground(0, null); 
     Log.i(TAG, "ON START COMMAND"); 
     Toast.makeText(this, "ON START COMMAND", Toast.LENGTH_LONG).show(); 
     return START_STICKY; 
    } 
} 

AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.pruebas" 
    android:versionCode="1" 
    android:versionName="1.0" 
    android:installLocation="internalOnly" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="17" /> 

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 
    <application> 
     <service android:name=".Servicio"> 
      <intent-filter> 
       <action android:name="com.pruebas.Servicio"/> 
      </intent-filter> 
     </service> 

     <receiver android:name=".Recibidor" android:enabled="true" android:exported="true" 
android:permission="android.permission.RECEIVE_BOOT_COMPLETED"> 
      <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED"/> 
       <category android:name="android.intent.category.DEFAULT"/> 
      </intent-filter> 
     </receiver> 

    </application> 

</manifest> 
+0

您是否收到Recibidor的意圖? – Entreco 2013-03-25 08:29:24

+0

嗨Entreco。你能告訴我如何知道我是否收到意圖? – eloweyn 2013-03-25 08:51:56

+0

您可以將日誌記錄在onReceiver方法中 – tundundun 2013-03-25 09:23:03

回答

4

你張貼絕不會在Android上的更高版本的工作代碼。爲了防止惡意軟件,在Android的更高版本中,無法在清單中自動註冊BroadcastReceiverUNTIL用戶已從應用啓動器手動啓動您的應用。

您將需要創建一個Activity與主/啓動器<intent-filter>條目。一旦用戶手動啓動應用程序一次,您的BroadcastReceiver的清單註冊將發生,並且它將保持註冊,除非用戶使用設置的管理應用程序部分中的「強制停止」。

+0

謝謝Squonk! – eloweyn 2013-03-25 09:55:00

1

應用程序需要被安裝在內部存儲接收BOOT_COMPLETED意圖。看到這個問題:My BroadcastReceiver is not receiving the BOOT_COMPLETED intent after my N1 boots. Help Please!

+0

android:installLocation =「internalOnly」表示該應用程序僅安裝在內部..要重新調用任何broadcat u hav 2運行應用程序一次..請註冊其他人 – 2013-03-25 08:33:36

+0

應用程序有標籤android:installLocation =「internalOnly」。它安裝在內部存儲器 – eloweyn 2013-03-25 08:53:16