2016-07-20 1 views
2

Comment Whatsapp service continuer à travailler en arrière-plan dans les téléphones Huawei?Comment Keep Alive Service Android

J'ai supprimé WhatsApp des applications protégées, mais le service Whatsapp n'est pas fermé à l'écran hors temps. Je suis en train d'écrire une application critique qui doit fonctionner à chaque fois, mais mon service est tué à l'écran. Je veux écrire un service comme WhatsApp ou le service AirDroid Quelqu'un peut-il expliquer à ce sujet?

Je veux dire comment écrire un service qui spécialement ne se ferment pas hors de l'écran dans les téléphones HUAWEI

Ceci est mon code de service

de AppLifeService

public class AppLifeService extends Service { 
@Override 
public IBinder onBind(Intent intent) { 

    return null; 
} 



@Override 
public void onCreate() { 
    super.onCreate(); 

} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 

    super.onStartCommand(intent, flags, startId); 


    startForeground(5, AppLifeReciever.createNotification(this)); 


    return START_STICKY; 
} 


@Override 
public void onDestroy() { 


    //startService(new Intent(this, AppLifeService.class)); Updated : not need 


    super.onDestroy(); 

} 
} 
+0

avez-vous vérifié sur les différentes versions d'android os? Dans quel service de version OS s'arrête-t-il? –

+0

si votre service est en effet au premier plan (comme vous appelez startForeground), alors il ne peut pas être tué et la notification non rejetable en est un témoignage. quand appelez-vous stopForeground? – Ramin

Répondre

4

service avec START_STICKY dans retrun onStartCommand() va redémarrer automatiquement vous n'avez pas besoin de le redémarrer à onDestroy()

@Override 
    public void onDestroy() { 

     // startService(new Intent(this, AppLifeService.class)); 
     super.onDestroy(); 

    } 
+1

Merci, Post mis à jour. mais je parle surtout de téléphones Huawei dans le temps que l'application ne pas ajouté à la liste "Applications protégées" – hamidjahandideh

+0

sory @hamidjahandideh je n'ai aucune idée spécifiquement sur les téléphones Huawei. –

2

Vous devez créer un Service « rouvrir » BroadcastService automatiquement quand il est fermé.

Par exemple:

BroadcastService

public class MyBroadcastService extends BroadcastReceiver 
{ 

@Override 
    public void onReceive(final Context context, Intent intent) 
    { 
    //do something 
    } 
} 

service de "rouvrir" automatiquement

public class MyService extends Service 
{ 

@Override 
    public void onCreate() 
    { 
     // Handler will get associated with the current thread, 
     // which is the main thread. 
     super.onCreate(); 
     ctx = this; 

    } 

@Override 
    public IBinder onBind(Intent arg0) 
    { 
     // TODO Auto-generated method stub 

     return null; 
    } 

@Override 
    public int onStartCommand(Intent intent, int flags, int startId) 
    { 
     Log.i(TAG, "onStartCommand"); 
     //Toast.makeText(this, "onStartCommand", Toast.LENGTH_LONG).show(); 

     return START_STICKY; 
    } 

//launch when its closed 
@Override 
    public void onDestroy() 
    { 
     super.onDestroy(); 
     sendBroadcast(new Intent("YouWillNeverKillMe")); 
     Toast.makeText(this, "YouWillNeverKillMe TOAST!!", Toast.LENGTH_LONG).show(); 
    } 
} 

Déclare sur votre AndroidManifest.XML

<receiver android:name=".BroadcastServicesBackground.MyBroadcastService"> 
      <intent-filter> 
       <!--That name (YouWillNeverKillMe) you wrote on Myservice--> 
       <action android:name="YouWillNeverKillMe"/> 

       <data android:scheme="package"/> 
      </intent-filter> 
      <intent-filter> 
       <!--To launch on device boot--> 
       <action android:name="android.intent.action.BOOT_COMPLETED"/> 
      </intent-filter> 
     </receiver> 

     <service android:name=".Services.MyService"/> 
+0

merci je vais le tester – hamidjahandideh

+0

j'ai testé dans les téléphones huawei et le service de proximité sans appeler surDestroy – hamidjahandideh

3

@Sohail Zahid réponse vous dit un moyen de repeatedly démarrer un service encore et encore à l'arrêt. Mais pour garder un service vivant comme jouer une chanson dans un fond.

La meilleure approche que j'ai trouvé est

startForeground(int, Notification) 

où la valeur int doit être unique pour chaque notification

Vous devez fournir un Notification la méthode qui est affichée dans la barre de notification dans la section En cours. De cette façon, l'application restera en arrière-plan sans interruption.