停止START_STICKY服务

时间:2016-07-06 09:59:08

标签: android service

当我按下"启动服务"我想在背景中运行一些应用程序。即使应用程序关闭或被杀,也会继续在后台运行。 为此,我使用START_STICKY并且它可以工作,但问题是当我想要终止服务时 - 应用程序已关闭(就像我想要的那样)然后我得到错误(不幸的是,流程服务已经停止)。并且该服务尝试再次运行。

如何停止服务?

P.S-I在网上搜索解决方案但未成功。

我的代码

MainActivty

public class MainActivity extends AppCompatActivity {
    private TextView textView;
    private  Intent i;
    private static Button killSerBut;
    private static final String TAG="com.example.elicahi.service";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView=(TextView) findViewById(R.id.textView1);
        killSerBut=(Button)findViewById(R.id.kill);
        killSerBut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG,"onClick");
                textView.setText("OnClick");
                killServ();
            }
        });
        //start the service
       i= new Intent(this, MyService.class);
        startService(i);

    }



    public void changeText(String msg)
    {

        textView.setText(msg);

    }
    public void killServ()
    {
        finish();
        Log.d(TAG,"OnStop");

        MyService myService=new MyService();
        myService.stopService(i);
    }
}

为MyService

    public class MyService extends Service {

        private static final String TAG="com.example.elicahi.service";
        private Intent intent;

        public MyService( ) {


        }


        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            Log.d(TAG, "onStartCommand method called");
            Runnable r= new Runnable() {
                @Override
                public void run() {
                    for (int i=0; i<10;i++){
                        long waitFor= System.currentTimeMillis()+1000;
                        while (System.currentTimeMillis()<waitFor){
                            synchronized (this){
                                try{
                                    wait(waitFor-System.currentTimeMillis());
                                    Log.d(TAG, "the service is doing something");

                                }catch (Exception e){}
                            }
                        }

                    }
                }
            };
            //start the thread that inside the run nethod
            Thread elichaiThread = new Thread(r);
            elichaiThread.start();
            //if the service is destroy- then restart it
            return START_STICKY;


        }




        @Override
        public void onDestroy() {
            Log.d(TAG,"onDestroy method called");
            stopSelf();
        }





       /* public void killServ(Intent intGet)
        {
            Log.d(TAG,"Killed");
        intent=intGet;
        }*/

        @Override
        public IBinder onBind(Intent intent) {
            // TODO: Return the communication channel to the service.
            return null;
        }

}

那就是我的清单

  <service
                android:name=".MyService"
                android:enabled="true"
                android:exported="true" >
            </service>

logcat的

07-06 13:07:11.197 1230-1299/com.example.elicahi.service D/com.example.elicahi.service: the service is doing something
07-06 13:07:12.197 1230-1299/com.example.elicahi.service D/com.example.elicahi.service: the service is doing something
07-06 13:07:13.198 1230-1299/com.example.elicahi.service D/com.example.elicahi.service: the service is doing something
07-06 13:07:13.720 1230-1230/com.example.elicahi.service D/com.example.elicahi.service: onClick
07-06 13:07:13.737 1230-1230/com.example.elicahi.service D/com.example.elicahi.service: OnStop
07-06 13:07:14.198 1230-1299/com.example.elicahi.service D/com.example.elicahi.service: the service is doing something
07-06 13:07:15.198 1230-1299/com.example.elicahi.service D/com.example.elicahi.service: the service is doing something
07-06 13:07:16.946 1557-1557/com.example.elicahi.service D/com.example.elicahi.service: onStartCommand method called
07-06 13:07:17.954 1557-1589/com.example.elicahi.service D/com.example.elicahi.service: the service is doing something
07-06 13:07:18.990 1557-1589/com.example.elicahi.service D/com.example.elicahi.service: the service is doing something
07-06 13:07:20.031 1557-1589/com.example.elicahi.service D/com.example.elicahi.service: the service is doing something
07-06 13:07:21.071 1557-1589/com.example.elicahi.service D/com.example.elicahi.service: the service is doing something
07-06 13:07:22.111 1557-1589/com.example.elicahi.service D/com.example.elicahi.service: the service is doing something
07-06 13:07:23.149 1557-1589/com.example.elicahi.service D/com.example.elicahi.service: the service is doing something
07-06 13:07:24.189 1557-1589/com.example.elicahi.service D/com.example.elicahi.service: the service is doing something
07-06 13:07:25.207 1557-1589/com.example.elicahi.service D/com.example.elicahi.service: the service is doing something
07-06 13:07:26.208 1557-1589/com.example.elicahi.service D/com.example.elicahi.service: the service is doing something
07-06 13:07:27.223 1557-1589/com.example.elicahi.service D/com.example.elicahi.service: the service is doing something

2 个答案:

答案 0 :(得分:0)

Service标记

下的清单文件中使用属性
android:stopWithTask="true"

答案 1 :(得分:-1)

请替换此方法

public void killServ()
{
  stopService(MainActivity.this,MyService.class);
}