2017-08-17 3 views
1

アクティビティから閉じるよう依頼するまで、実行したいアンドロイドサービスを作成しました。私はSTART_STICKY返信onStartcommand()とそれを作成し、正常に動作し、アプリケーションを閉じても実行し続けます。しかし、問題はボタンをクリックしてサービスを停止したいということです(ラジオボタン:バックグラウンドで実行するはい/いいえ)。あなたのボタンのonClick()方法でアクティビティからSTART_STICKYアンドロイドサービスを停止する方法

public class MyService extends Service { 
    public MyService() { 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     return START_STICKY; 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     // TODO: Return the communication channel to the service. 
     throw new UnsupportedOperationException("Not yet implemented"); 
    } 
} 

答えて

0

、この操作を行います。

Intent intent = new Intent(MyActivity.this, MyService.class); 
stopService(intent); 
+0

をそれが動作思って:)。 –

関連する問題