1

嗨朋友们,我有一个问题要解决...我想彻底销毁服务,一旦我从 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() 继续调用

4

3 回答 3

0

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

我的活动.java

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

服务.java

@Override
    public void onDestroy()
    {

        stopSelf();
        super.onDestroy();
    }
于 2012-05-25T06:17:50.023 回答
0

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

于 2012-05-25T06:22:27.587 回答
0

更新 UI 后使用 stopService() 方法

或者

在活动中使用 bindService 而不是使用 startService。当活动销毁时,服务也销毁

于 2012-05-25T06:19:31.643 回答