에게 다른 사람들에게 유용 할 수도 있기 때문에 제 질문에 대답 해 드리겠습니다. (활성 상태를 유지해야하므로 IntentService가 아닌 일반 서비스를 사용하고 있습니다)
서비스에서 메시지를 수신하는 활동에 대해서는, Handler를 인스턴스화해야합니다 ...
private Handler handler = new Handler()
{
public void handleMessage(Message message)
{
Object path = message.obj;
if (message.arg1 == 5 && path != null)
{
String myString = (String) message.obj;
Gson gson = new Gson();
MapPlot mapleg = gson.fromJson(myString, MapPlot.class);
String astr = "debug";
astr = astr + " ";
}
};
};
위의 코드는 내 디버그 항목으로 구성되어 있습니다. 이 서비스는 메시지를 활동으로 보냅니다. ...
MapPlot mapleg = new MapPlot();
mapleg.fromPoint = LastGeoPoint;
mapleg.toPoint = nextGeoPoint;
Gson gson = new Gson();
String jsonString = gson.toJson(mapleg); //convert the mapleg class to a json string
debugString = jsonString;
//send the string to the activity
Messenger messenger = (Messenger) extras.get("MESSENGER");
Message msg = Message.obtain(); //this gets an empty message object
msg.arg1 = 5;
msg.obj = jsonString;
try
{
messenger.send(msg);
}
catch (android.os.RemoteException e1)
{
Log.w(getClass().getName(), "Exception sending message", e1);
}
방금 메시지 식별자로 5 번을 선택했습니다. 이 경우 JSON 문자열에 복합 클래스를 전달한 다음 해당 작업을 다시 작성합니다.
여기에이 엑스트라가 무엇입니까? –
접근법의 문제점은 활동을 다시 작성하면 응답을받을 수있는 능력이 느슨해집니다. 방송 수신기 방식이 더 좋습니다. 어떤 사람들은 핸들러를 정적으로 만들 수 있다고 제안합니다. 나는 아니오라고 말하고 싶습니다. 왜냐하면 그 활동이 진정으로 사용자에 의해 닫힐 때 당신은 여전히 메시지를받을 것이기 때문입니다. – user210504