如何使用unBind(mConnection)取消绑定服务?

时间:2012-03-02 07:08:36

标签: android

这是我为我的应用实施服务的代码。当我调用unBind(mConnection)时,整个应用程序越来越近,只有特定的Activiy必须关闭。请帮帮我。

package com.tvix.torrent.service;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.Handler;
import android.os.Parcel;
import android.os.RemoteException;
import android.util.Log;
import android.widget.Toast;


import com.tvix.torrent.R;
import com.tvix.torrent.ui.TvixBtpdView;


// Need the following import to get access to the app resources, since this
// class is in a sub-package.
//import com.tvix.torrent.R;


public class TvixBtpdService extends Service {

    private static final String LOG_TAG = "TvixBtpdService";

    private static final String TVIX_BTPD_WORKING_PATH = "/data/data/com.tvix.torrent/btpd";

    private NotificationManager mNM;

    private TvixBtpdUIAdapter mUIAdapter;


    /**
     * Class for clients to access.  Because we know this service always
     * runs in the same process as its clients, we don't need to deal with
     * IPC.
     */
    public class LocalBinder extends Binder {
        public TvixBtpdService getService() {
            return TvixBtpdService.this;
        }
    }


    @Override
    public void onCreate() 
    {
        mUIAdapter = new TvixBtpdUIAdapter(this);

        TvixBtpdJni.BtpdInit(TVIX_BTPD_WORKING_PATH);
        TvixBtpdJni.BtpdStart();

        mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        // Display a notification about us starting.  We put an icon in the status bar.
        showNotification();
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) 
    {
        Log.i(LOG_TAG, "Received start id " + startId + ": " + intent);
        // We want this service to continue running until it is explicitly
        // stopped, so return sticky.
        return START_STICKY;
    }


    @Override
    public void onDestroy() 
    {
        TvixBtpdJni.BtpdStop();

        mUIAdapter = null;

        // Cancel the persistent notification.
        mNM.cancel(R.string.local_service_started);

        // Tell the user we stopped.
        Toast.makeText(this, R.string.local_service_stopped, Toast.LENGTH_SHORT).show();
    }



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



    // This is the object that receives interactions from clients.  See
    // RemoteService for a more complete example.
    private final IBinder mBinder = new LocalBinder();


    /**
     * Show a notification while this service is running.
     */
    private void showNotification() {
        // In this sample, we'll use the same text for the ticker and the expanded notification
        CharSequence text = getText(R.string.local_service_started);

        // Set the icon, scrolling text and timestamp
        Notification notification = new Notification(R.drawable.stat_sample, text,
                System.currentTimeMillis());

        // The PendingIntent to launch our activity if the user selects this notification
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, TvixBtpdView.class), 0);

        // Set the info for the views that show in the notification panel.
        notification.setLatestEventInfo(this, getText(R.string.local_service_label),
                       text, contentIntent);

        // Send the notification.
        // We use a layout id because it is a unique number.  We use it later to cancel.
        mNM.notify(R.string.local_service_started, notification);
    }





    public TvixBtpdUIAdapter getUIAdapter() {
        return this.mUIAdapter;
    }



    public void registerUIHandler(Handler hdl) throws RemoteException 
    {
        if (hdl != null) {
            Log.e(LOG_TAG, "registerUIHandler()");
            mUIAdapter.mUIHandler = hdl;
        }
    }


    public void unregisterUIHandler() throws RemoteException 
    {
        Log.i(LOG_TAG, "unregisterUIHandler()");
        mUIAdapter.mUIHandler = null;
    }


}

//当我在进行unBind(mConnection)时,整个应用程序都将关闭。请帮忙 我。在这里,我在课堂上实现了与服务的连接。

private ServiceConnection mConnection = new ServiceConnection()
    {

        public void onServiceConnected(ComponentName className, IBinder service) {


            mService = ((StarzService.LocalBinder)service).getService();

            // We want to monitor the service for as long as we are connected to it.
            try {
                //Log.d(LOG_TAG, "registerUIHandler(mUIHandler)");
                mService.registerUIHandler(mUIHandler);
                mBound = true;
            } catch (Exception e) {

                e.printStackTrace();
            }

            mUIAdapter = mService.getUIAdapter();

            // Tell the user about this for our demo.
            Toast.makeText(StarzSettingsActivity.this, R.string.local_service_connected, Toast.LENGTH_SHORT).show();
        }

        /**
         * 
         * To disconnect the service
         */    
        public void onServiceDisconnected(ComponentName className) {
            mUIAdapter = null;

            try {
                mService.unregisterUIHandler();
            } catch (Exception e) {
                e.printStackTrace();
            }
            mService = null;
            mBound = false;
            Toast.makeText(StarzSettingsActivity.this, R.string.local_service_disconnected, Toast.LENGTH_SHORT).show();
        }
    };

//我将服务绑定为

if(!StarzService.IS_SERVICE_RUNNIG){
             bindService(starzSrvIntent, mConnection, Context.BIND_AUTO_CREATE);
        }

//在onDestroy()中我调用unBind(mConnection);

public void onDestroy(){
        super.onDestroy();
        unbindService(mConnection);

    }

0 个答案:

没有答案