2017-06-11 8 views
0

私はNotificationServiceクラスを拡張していますServiceと私はサービスwhitメソッドを起動するが、私は現在のサービスを停止する方法がわからない、私はいくつかのガイドと例を読むが、stopService()メソッドで私は私のサービスを停止することはできません。これは私のクラスのコードです、どうやって止めることができますか?別のクラスからNotificationServiceを停止するにはどうしたらいいですか?

public class NotificationService extends Service { 

private final long TIME_WAKE_UP = 6000;//60 * 60 * 1000; 
private long timeStart; 
private Esercizio es; 

@Override 
public void onCreate() { 
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this); 
    Notification mNt = mBuilder.build(); 
    startForeground(2, mNt); 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    timeStart = intent.getExtras().getLong("timeStart"); 
    es = (Esercizio) intent.getExtras().get("esercizio"); 

    while(System.currentTimeMillis() < timeStart + TIME_WAKE_UP); 

    atWork(); 

    stopForeground(true); 
    stopSelf(); 

    return START_STICKY; 
} 

private void atWork() { 
    Intent intent = new Intent(this, TransitionArchive.class); 
    Bundle bundle = new Bundle(); 
    bundle.putString("notificationService",""); 
    bundle.putString("KEY_EXCERSIZE",es.getNameEx()); 
    intent.putExtras(bundle); 

     NotificationCompat.Builder mBuilderOffline = 
       new NotificationCompat.Builder(this) 
         .setSmallIcon(R.drawable.logo_app_android) 
         .setContentTitle(es.getNameEx()) 
         .setContentText("C'è un nuovo contenuto per te!") 
         .setAutoCancel(true) 
         .setPriority(Notification.PRIORITY_MAX) 
         .setVisibility(NotificationCompat.VISIBILITY_PUBLIC); 

     PendingIntent pendingIntent = PendingIntent.getActivity(this, 
       1, 
       intent, 
       PendingIntent.FLAG_UPDATE_CURRENT); 
     mBuilderOffline.setContentIntent(pendingIntent); 

     //PendingIntent call the receive class 

     Notification note = mBuilderOffline.build(); 
     note.defaults |= Notification.DEFAULT_SOUND; 
     note.defaults |= Notification.DEFAULT_VIBRATE; 

     NotificationManager mNotificationManager = 
       (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     mNotificationManager.notify(1, note); 
} 

public static void startNotificationService(Context mContext, Esercizio es) { 
    Intent mIntent = new Intent(mContext, NotificationService.class); 
    mIntent.putExtra("esercizio", es); 
    mIntent.putExtra("timeStart", System.currentTimeMillis()); 
    startWakefulService(mContext, mIntent); 
} 

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

答えて

0

これは最善の方法ではないかもしれませんが、私はIntentServiceでフラグとしてパブリック静的変数を使用しました。フラグが設定されている場合は、必要なクリーンアップを実行し、スレッドを停止してループを終了し、stopSelf()およびstopForeground()に自然に(すでに持っている)フローを送ります。

How to check if a service is running on Android?

+0

のようにしてみて、私は、このフラグを使用することができますか?私は、NotificationServiceクラスではなく、別のクラスから私の現在のサービスを停止したい –

+0

Uはコードを投稿することができますか?私はこの静的変数をどこに入れますか?私は別のクラスから私のサービスを停止することはできません、私を助けてください –

+0

私は答えを更新しました。他のユーザーからすでに提供されている例を参照してください:[サービスがAndroid上で実行されているかどうかをチェックする方法](https://stackoverflow.com/questions/600207/how-to-check-if-a-service-on-on-on-android)を実行します。 –

0

この

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    timeStart = intent.getExtras().getLong("timeStart"); 
    es = (Esercizio) intent.getExtras().get("esercizio"); 

    while(System.currentTimeMillis() < timeStart + TIME_WAKE_UP); 

    atWork(); 

    stopForeground(true); 
    stopSelf(); 

    return START_NOT_STICKY; 
} 
関連する問題