2016-06-16 11 views
-3

Ich möchte einen Dienst erstellen, der neue Benachrichtigungen jedes Mal überprüft, auch wenn die Haupt-App geschlossen ist.Dienst mit START_STICKY

Add

<service 
     android:name=".service.NotificationService" 
     android:enabled="true" 
     android:exported="true" 
     android:stopWithTask="false" 
     android:process=":notification"/> 

    <!-- Declaring broadcast receiver for BOOT_COMPLETED event. --> 
    <receiver android:name=".BootReceiver" 
     android:enabled="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> 

Zweitens ist Boot-Empfänger zu manifestieren, wird es auch Dienst nach dem Neustart starten.

NotificationService.class

import android.app.Service; 
import android.content.Context; 
import android.content.Intent; 
import android.net.ConnectivityManager; 
import android.os.IBinder; 
import android.util.Log; 

import com.tagwishes.fc.Notification; 
import com.tagwishes.fc.app.AppConfig; 
import com.tagwishes.fc.helper.SessionManager; 

import java.util.Timer; 
import java.util.TimerTask; 

public class NotificationService extends Service { 


    static int WIFI_NETWORK_ID = 1; 
    static int MOBILE_NETWORK_ID = 2; 
    static int NO_NETWORK_ID = 3; 
    private SessionManager sm; 


    final String LOG_TAG = "fc"; 

    public void onCreate() { 
     super.onCreate(); 
     Log.d(LOG_TAG, "MyService onCreate"); 
    } 

    public void onDestroy() { 
     super.onDestroy(); 
     Log.d(LOG_TAG, "MyService onDestroy"); 
    } 

    public int onStartCommand(Intent intent, int flags, int startId) { 
     Log.d(LOG_TAG, "MyService onStartCommand"); 


     readFlags(flags); 
     MyRun mr = new MyRun(startId); 
     new Thread(mr).start(); 


     Log.d("fc", "START_STICKY"); 

     return START_STICKY; 
    } 

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

    int chkStatus() { 
     final ConnectivityManager connMgr = (ConnectivityManager) 
       this.getSystemService(Context.CONNECTIVITY_SERVICE); 
     final android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI); 
     final android.net.NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); 
     if (wifi.isConnectedOrConnecting()) { 
      return WIFI_NETWORK_ID; 
     } else if (mobile.isConnectedOrConnecting()) { 
      return MOBILE_NETWORK_ID; 
     } else { 
      return NO_NETWORK_ID; 
     } 
    } 

    void readFlags(int flags) { 
     if ((flags&START_FLAG_REDELIVERY) == START_FLAG_REDELIVERY) 
      Log.d(LOG_TAG, "START_FLAG_REDELIVERY"); 
     if ((flags&START_FLAG_RETRY) == START_FLAG_RETRY) 
      Log.d(LOG_TAG, "START_FLAG_RETRY"); 
    } 

    class MyRun implements Runnable { 

     int startId; 

     public MyRun(int startId) { 
      this.startId = startId; 
      Log.d(LOG_TAG, "MyRun#" + startId + " create"); 
     } 

     public void run() { 
      Log.d(LOG_TAG, "MyRun#" + startId + " start"); 

      final int networkID = chkStatus(); 

      Log.d("fc", "networkID " + networkID); 

      if (sm == null) { 
       sm = new SessionManager(getApplication()); 
      } 

      int reCheckTime = AppConfig.NOTIFICATION_RECHECK_DEFAULT; 

      if (networkID == WIFI_NETWORK_ID) { 
       reCheckTime = AppConfig.NOTIFICATION_RECHECK_WIFI; 
      } else if (networkID == MOBILE_NETWORK_ID) { 
       reCheckTime = AppConfig.NOTIFICATION_RECHECK_MOBILE; 
      } 


      final Notification nt = new Notification(getApplicationContext());//Its my own class 


      Log.d("fc", "Timer check noti started " + reCheckTime); 
      new Timer().scheduleAtFixedRate(new TimerTask(){ 
       @Override 
       public void run(){ 

        if (networkID != NO_NETWORK_ID || !sm.isLoggedIn()) {//IF HAVE NETWORK CONNECTION 
         Log.d("fc", "checkNotifications "); 

         nt.checkNotifications(); 
        } 

       } 
      }, 0, reCheckTime*1000); 


     } 

    } 

} 

Boot-Receiver

public class BootReceiver extends BroadcastReceiver { 
@Override 
public void onReceive(Context context, Intent intent) { 

    Log.d("fc", "BootReceiver BroadcastReceiver"); 

    Intent myIntent = new Intent(context, NotificationService.class); 
    context.startService(myIntent); 
} 

}

Und das, wie ich Rufbereitschaft auf MainActivity

 Intent bindIntent = new Intent(getBaseContext(), NotificationService.class); 
     startService(bindIntent); 

Wenn ich app fr schließen om Apps-Verlauf, schließt es auch meine: Benachrichtigungsprozess und Dienst nicht erneut gestartet. Ich verwende START_STICKY dafür.

+0

starten, wenn Sie auf API23 entwickeln und oben, haben Sie dösen Modus zu handhaben. Dies könnte der Grund sein, warum Ihr Dienst gestoppt wurde ... – Opiatefuchs

+0

min API 14 ((( –

+0

) es ist wichtig, welches Ziel Sie setzen und welche Version auf Ihrem Gerät installiert ist .... nicht die Minipi. Vor allem welche Version Eine App mit niedrigerer API ist nicht vom Doze-Modus ausgeschlossen, sie hat einfach keine Möglichkeit, am Leben zu bleiben, weil die Methoden/Android-Pakete nicht verfügbar sind ... – Opiatefuchs

Antwort

0

Ihren Dienst innerhalb onDestroy Funktion

@Override 
protected void onDestroy() { 
super.onDestroy(); 
//startService() 
} 
+0

Funktioniert nicht. onDestroy wird aufgerufen, wenn die Zurück-Taste gedrückt wird. Wenn ich die App aus dem App-Verlauf schließe, wurde sie mit dem Benachrichtigungsdienst geschlossen und ohne onDestroy aufzurufen. –