2012-01-07 2 views
0

J'ai un problème que j'ai fait un service qui exécute une tâche en arrière-plan toutes les 5 secondes, pour cela j'ai fait un récepteur en classe manifest et autostarter mais il n'est pas excuté dans mon application . Signifie que le service n'est pas en cours d'exécution dans l'application. Je ne sais pas pourquoi? S'il vous plaît me suggérer pour la bonne réponse.Exécuter une tâche en arrière-plan toujours en android

applications manifeste:

<application android:icon="@drawable/icon" android:label="@string/app_name"> 
     <receiver android:name="com.halosys.TvAnyTime.ServiceAutoStarter"> 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 
     </intent-filter> 
    </receiver> 
     <activity android:name=".MainScreen" 

        android:label="@string/app_name" android:screenOrientation="portrait" android:theme="@android:style/Theme.NoTitleBar"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 

     </activity> 

      <activity android:name=".IntroVideo" android:label="@string/app_name" android:theme="@style/CustomTheme" android:screenOrientation="landscape"/> 
      <activity android:name=".WelcomeScreen1" android:theme="@android:style/Theme.NoTitleBar" android:screenOrientation="portrait"/> 
    <activity android:name=".IntroFaceBookScreen" android:theme="@android:style/Theme.NoTitleBar" android:screenOrientation="portrait"/> 
    </application> 

    <uses-permission xmlns:android="http://schemas.android.com/apk/res/android" 
     android:name="android.permission.INTERNET"></uses-permission> 
    <uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/> 

<uses-permission android:name="android.permission.READ_INPUT_STATE"></uses-permission> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.READ_PHONE_STATE"/> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 
    <uses-sdk android:minSdkVersion="3" /> 
</manifest> 

ServiceAutoStarter:

public class ServiceAutoStarter extends BroadcastReceiver { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      Intent myIntent = new Intent(context, ServiceTemplate.class); 
      myIntent.putExtra("extraData", "somedata"); 
      context.startService(myIntent); 
     } 
    } 

ServiceTemplate.java:

public class ServiceTemplate extends Service { 

    Runnable refresher; 
    public static int HashesInPattern=32;   // 32 64byte hashes in the pattern 
    public static int BytesInHash =64; 
    static byte[] hashPattern; 
    ArrayList<byte[]> finalHashafterXor = new ArrayList<byte[]>(); 
    byte[] finaldatafile = new byte[2097152]; 
    byte[] finalhash; 
    InputStream is; 
    byte[] bytesafterXor,bytes; 
    String strLine; 
    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 

    @Override 
    public void onCreate() { 
     //code to execute when the service is first created 
    } 

    @Override 
    public void onDestroy() { 
     //code to execute when the service is shutting down 
    } 

    @Override 
    public void onStart(Intent intent, int startid) { 
     //code to execute when the service is starting up 
     final Handler handler = new Handler(); 


     refresher = new Runnable() { 
     public void run() { 
      // some action 
      try{ 
       Encrypt(); 
       Toast.makeText(getApplicationContext(), "Hello World", Toast.LENGTH_SHORT).show(); 
      } 
      catch(Exception e) 
      { 
       e.printStackTrace(); 
      } 
      handler.postDelayed(refresher, 2000); 
     } 
     }; 

    } 

Merci d'avance.

+0

Je pense que vous devez mettre du code dans le thread pour l'exécuter toutes les 5 s –

+0

Harsh Je veux exécuter un service qui exécute automatiquement le code toutes les 5 s. toujours en arrière-plan. Si j'ai fait un fil pour le même, alors il est restreint pour l'application seulement. –

Répondre

1

Je n'ai pas passé par l'ensemble de votre code, mais j'ai vu fichier manifest

Comme les activités (et d'autres composants), vous devez déclarer tous les services dans le fichier manifeste de votre application.

Veuillez changer et essayer.

+0

Je n'ai pas compris ce que vous dites, parce que j'ai déjà ajouté des récepteurs dans Manifest. –

+0

Je ne suis pas sûr si ce n'est que le problème ou non, mais reportez-vous à ce lien http://developer.android.com/guide/topics/fundamentals/services.html#Declaring, vous comprendrez que vous n'avez pas déclaré votre service dans le fichier manifeste. – Ankit

+0

Ya Ankit, j'ai fait la même chose et mis à jour mon manifeste avec des services de mention, mais le problème reste le même. –

Questions connexes