在重新启动活动时获取运行服务实例的引用

时间:2014-08-13 14:54:04

标签: android android-activity service

我必须跟踪编辑特定文本字段的时间,而不管设备时间(用户可以更改)以及他没有连接到互联网。 因此,我尝试在服务中运行的计时器启动活动时启动服务。当用户更改文本字段时,我可以在计时器值的帮助下获得正确的时间。 所以我在创建服务方面非常成功,在服务和活动之间进行双向沟通。 我在这里遇到的唯一一个大问题是,当我在杀戮后重新启动活动时,我无法将计数器变量的值恢复到我离开它之前杀死活动的位置。

服务:

public class CounterService extends Service {

/** Keeps track of all current registered clients. */
ArrayList<Messenger> mClients = new ArrayList<Messenger>();
static final int MSG_REGISTER_CLIENT = 1;
static final int MSG_UNREGISTER_CLIENT = 2;
static final int MSG_SET_VALUE = 3;

static final int MSG_GET_COUNT = 4;

/**
 * Target we publish for clients to send messages to IncomingHandler.
 */
final Messenger mMessenger = new Messenger(new IncomingHandler());

private static Timer timer;
private HashMap<Integer, Integer> countMissionId = new HashMap<Integer, Integer>();

/**
 * Class used for the client Binder. 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 {

}


@Override
public void onCreate() {
    super.onCreate();
    // setCount(0);

}

private void startTimerService() {
    if (timer == null) {
        timer = new Timer();
    }
    timer.scheduleAtFixedRate(new mainTask(), 0, 1000);
}

private void endTimerService() {
    if (timer != null)
        timer.cancel();
}

private class mainTask extends TimerTask {
    public void run() {
        for (Entry<Integer, Integer> entry : countMissionId.entrySet()) {
            entry.setValue(entry.getValue() + 1);
            Log.d("sg",
                    "Count:" + entry.getValue() + "Mission Id"
                            + entry.getKey());
        }

    }
}


@Override
public IBinder onBind(Intent intent) {
    return mMessenger.getBinder();
}

public void sendMessageActivity(int mission_id, int service_type) {
    try {
        if (mMessenger != null) {
            Message msg = Message
                    .obtain(null, service_type, mission_id, -1);
            msg.replyTo = mMessenger;
            mMessenger.send(msg);

        }
    } catch (RemoteException e) {
        // In this case the service has crashed before we could even
        // do anything with it; we can count on soon being
        // disconnected (and then reconnected if it can be restarted)
        // so there is no need to do anything here.
    }
}

class IncomingHandler extends Handler {
    private int mValue;

    @Override
    public void handleMessage(Message msg) {
        int missionId = msg.arg1;
        switch (msg.what) {
        case MSG_REGISTER_CLIENT:
            countMissionId.put(missionId, 0);
            break;
        case MSG_UNREGISTER_CLIENT:
            countMissionId.remove(missionId);
            if (countMissionId.size() == 0) {
                stopSelf();
            }
            break;
        case MSG_SET_VALUE:
            // endTimerService();
            startTimerService();
            break;
        case MSG_GET_COUNT:
            Log4Android.l(this, "In service" + countMissionId.get(missionId));
            sendMessageActivity(missionId, -1);

        case 6:
            mClients.add(msg.replyTo);
            break;
        case 7:
            mClients.remove(msg.replyTo);
            break;
        case 8:
            mValue = msg.arg1;
            for (int i = mClients.size() - 1; i >= 0; i--) {
                try {
                    mClients.get(i).send(
                            Message.obtain(null, MSG_SET_VALUE, mValue, 0));
                } catch (RemoteException e) {
                    // The client is dead. Remove it from the list;
                    // we are going through the list from back to front
                    // so this is safe to do inside the loop.
                    mClients.remove(i);
                }
            }
        default:
            super.handleMessage(msg);
        }
    }
}

}

的活动:

    class IncomingHandler extends Handler {
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
        case -1:
            Log.d("sg","almost there volla vollla");
        default:
            super.handleMessage(msg);
        }
    }
}


/**
 * Target we publish for clients to send messages to IncomingHandler.
 */
final Messenger mMessenger = new Messenger(new IncomingHandler());
/** Messenger for communicating with service. */
Messenger mService = null;
/** Flag indicating whether we have called bind on the service. */
boolean mIsBound;
/** Some text view we are using to show state information. */

private ServiceConnection mConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName className,
            IBinder service) {
        // This is called when the connection with the service has been
        // established, giving us the service object we can use to
        // interact with the service.  We are communicating with our
        // service through an IDL interface, so get a client-side
        // representation of that from the raw service object.
        mService = new Messenger(service);

        try {
            Message msg = Message.obtain(null,
                    6);
            msg.replyTo = mMessenger;
            mService.send(msg);

            // Give it some value as an example.
            msg = Message.obtain(null,
                    8, this.hashCode(), 0);
            mService.send(msg);
        } catch (RemoteException e) {
            // In this case the service has crashed before we could even
            // do anything with it; we can count on soon being
            // disconnected (and then reconnected if it can be restarted)
            // so there is no need to do anything here.
        }

        // We want to monitor the service for as long as we are
        // connected to it.

        // As part of the sample, tell the user what happened.
        Toast.makeText(getApplicationContext(), "Volla",
                Toast.LENGTH_SHORT).show();
    }

    public void onServiceDisconnected(ComponentName className) {
        // This is called when the connection with the service has been
        // unexpectedly disconnected -- that is, its process crashed.
        mService = null;

        // As part of the sample, tell the user what happened.
        Toast.makeText(getApplicationContext(), "Volla Disconnected",
                Toast.LENGTH_SHORT).show();        }
};
private Intent counterService;

void doBindService() {
    // Establish a connection with the service.  We use an explicit
    // class name because there is no reason to be able to let other
    // applications replace our component.
    counterService  =new Intent(MainActivity.this,CounterService.class);
    startService(counterService);
    bindService(counterService, mConnection, application.BIND_AUTO_CREATE);
    mIsBound = true;
}

void doUnbindService() {
    if (mIsBound) {
        // If we have received the service, and hence registered with
        // it, then now is the time to unregister.
        if (mService != null) {
            try {
                Message msg = Message.obtain(null,
                        8);
                msg.replyTo = mMessenger;
                mService.send(msg);
            } catch (RemoteException e) {
                // There is nothing special we need to do if the service
                // has crashed.
            }
        }

        // Detach our existing connection.
        unbindService(mConnection);
        mIsBound = false;
    }
}

public void sendMessage(int mission_id,int service_type){
 try {
     if(mService!=null){
        Message msg = Message.obtain(null,
                service_type,mission_id,-1);
        msg.replyTo = mMessenger;
        mService.send(msg);

     }   
    } catch (RemoteException e) {
        // In this case the service has crashed before we could even
        // do anything with it; we can count on soon being
        // disconnected (and then reconnected if it can be restarted)
        // so there is no need to do anything here.
    }

}

1 个答案:

答案 0 :(得分:1)

在Android中,如果需要在活动重新启动时保持值,则需要将值存储在文件,首选项或数据库中。然后,您可以在Activity重新启动时加载该值。

以下是有关保存数据的文档:http://developer.android.com/training/basics/data-storage/index.html