【问题标题】:How to stop back ground service completely..?如何完全停止后台服务..?
【发布时间】:2012-05-25 06:15:59
【问题描述】:

嗨朋友们,我有一个问题要解决...我想彻底销毁服务,一旦我从 Activity 调用 onDestroy() 方法。但我的问题是我无法完全摧毁它。在后台它继续运行,我正在分享我尝试过的示例代码..

    //Activity Class
    public class ServiceToAct extends Activity {
    private static final String TAG = "BroadcastEvent";
    private Intent intent;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        intent = new Intent(this, BroadcastService.class);
        startService(intent);
        registerReceiver(broadcastReceiver, new       IntentFilter(myService.BROADCAST_ACTION));
    }

    /*@Override
    protected void onResume() {
        super.onResume();

    }*/

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(broadcastReceiver);

    }

    @Override
    protected void onDestroy() {

        stopService(intent);

        Toast.makeText(this, "Destroy Completely", Toast.LENGTH_LONG).show();
    }

    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

        }
    };
}

// service class
public class myService extends Service {

    private static final String TAG = "BroadcastEvent";
    public static final String BROADCAST_ACTION = "com.service.activity.myService";
    private final Handler handler = new Handler();
    Intent intent;


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

    @Override
    public void onCreate() {
        super.onCreate();
        Toast.makeText(this, "created", Toast.LENGTH_LONG).show();
        intent = new Intent(BROADCAST_ACTION);
    } 

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        Toast.makeText(this, "start", Toast.LENGTH_LONG).show();
        handler.removeCallbacks(sendToUI);
        handler.postDelayed(sendToUI, 1000); 
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        stopSelf();
        //stopService(intent);
        Toast.makeText(this, "Destroy", Toast.LENGTH_LONG).show();
    }

    private Runnable sendToUI = new Runnable() {

        @Override
        public void run() {
            myData();
            handler.postDelayed(this, 10000); 
        }
    };

    private void myData() {

        Log.d(TAG, "keep on entering");
        Toast.makeText(this, "Keep on despling in UI", Toast.LENGTH_LONG).show();

        sendBroadcast(intent);
    }
}

这里实际上我想从服务更新我的 UI,我的一切都在工作,但如果我销毁服务,它会继续调用 myData() 方法,如果我也关闭应用程序,我会收到 Toast 消息。 我的问题是,一旦服务被破坏,我不想要那个 toast msg 我使用了 stopService(intent) 方法,它破坏了服务,但后台方法 myData() 一直在调用

【问题讨论】:

  • 如果您想要一个将其生命周期绑定到您的活动的Service,您应该考虑调用bindService 来启动它,而不是startService。这样Service 将在最后一个绑定Activity 调用unbindService 时完成。

标签: android service android-service


【解决方案1】:

为了停止服务完全使用这个..

myActivity.java

@Override
    public void onDestroy() {
      stopService(intent); 
        super.onDestroy();
        Toast.makeText(this, "Destroy", Toast.LENGTH_LONG).show();
    }

service.java

@Override
    public void onDestroy()
    {

        stopSelf();
        super.onDestroy();
    }

【讨论】:

  • 是我已经使用过的......但它没有停止服务,它继续在后台运行
  • 您必须在您的活动的 onDestroy 方法中写入从您开始服务的方法
  • 是的,我在活动课上写过 onDestroy,你是对的。但它现在仍在工作,我也尝试了上面的代码......我仍然从 myData() 获取 Toast 消息(参考我上面的代码)
  • 尝试从onCreate方法而不是onResume启动服务并检查
  • 我从 onCreate 方法启动服务,但还是同样的问题
【解决方案2】:

你最好永远不要直接调用 onXxx()。 在您的活动中使用 stopService(Intent i) 并在您的服务中使用 stopSelf() 来停止。

【讨论】:

  • 是的,我在 Activity 中使用了 stopService(intent),在服务中使用了 stopSelf(),但现在正在工作
【解决方案3】:

更新 UI 后使用 stopService() 方法

或

在活动中使用 bindService 代替 startService。当activity销毁时,service也销毁

【讨论】:

  • 在活动中使用 bindService 代替 startService。当activity销毁时,service也销毁
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-03
  • 2016-08-12
相关资源
最近更新 更多