2017-07-01 4 views
1

私は、次のしようとしています私はstopself();を試してみましたStopself止めないサービスのAndroid

TxtService extends Service implements View.OnClickListener{ 
    private RelativeLayout floatingControls; 
    private View controls; 
    private ImageButton CloseMainButton; 


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

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE); 
     LayoutInflater li = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); 
     floatingControls = (RelativeLayout) li.inflate(R.layout.paintimgtxtservice, null); 
     controls = floatingControls.findViewById(R.id.controls); 
     CloseMainButton = (ImageButton) controls.findViewById(R.id.CloseMainButton); 
     CloseMainButton.setOnClickListener(this); 
     final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
       WindowManager.LayoutParams.WRAP_CONTENT, 
       WindowManager.LayoutParams.WRAP_CONTENT, 
       WindowManager.LayoutParams.TYPE_PHONE, 
       WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, 
       PixelFormat.TRANSLUCENT); 
     params.gravity = Gravity.TOP | Gravity.START; 
     windowManager.addView(floatingControls, params); 
     return START_NOT_STICKY; 
    } 

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

    @Override 
    public void onClick(View v) { 
     int id = v.getId(); 
     switch (id) { 
      case R.id.CloseMainButton: 
       stopForeground(true); 
       this.stopSelf(); 
       Toast.makeText(PaintImgTxtService.this, "stop", Toast.LENGTH_SHORT).show(); 
       break; 
} 
} 

    @Override 
    public void onDestroy() 
    { 


     super.onDestroy(); 
    } 


} 

コード、this.stopself()stopForeground(true);、何startForegroundはありませんが、それでもサービスは、私は、このサービスを停止することができますどのように、ストップをdoesnot

ボタンをクリックしてトーストを表示しますが、まだサービスは閉じていません。

画面上にウィンドウを作成するためのウィンドウマネージャがあります

+0

のような画面

のremoveウィンドウマネージャこのhttps://stackoverflow.com/questions/3165190/how-to-call-stopservice-method-of-service-class-fromを参照してください。 - 呼び出しアクティビティ - クラス – Anil

答えて

3

public void onClick(View v) { 
      int id = v.getId(); 
      switch (id) { 
       case R.id.CloseMainButton: 

        if (floatingControls!= null) 
        windowManager.removeView(floatingControls); 
        this.stopSelf(); 


        break; 
    } 
関連する問題