嗨,我的项目有问题,我有一项活动和服务,我使用 Messenger 与我们沟通。当我收到来自服务的消息时,我会查看一个警报对话框,但没有出现此对话框。在日志猫上,我看到一个带有文本的日志。你有解决方案吗 。我的代码是:
import java.lang.ref.WeakReference;
import java.util.List;
import java.util.Map;
import ztl.Bologna.activity.Database.DBopenHelper;
import ztl.Bologna.OverlayList;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.os.Messenger;
import android.provider.BaseColumns;
import android.app.AlertDialog;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.Menu;
public class MapsActivity extends MapActivity{
private MyLocationOverlay mOverlay;
protected static MapView gmap;
private DBopenHelper db;
OverlayList overlay;
private GuiHandler guiHandler;
private Messenger messenger;
public double destLat = 44.497592;
public double destLong = 11.356151;
private double Long = 11.352693;
private double Lat = 44.497271;
Intent serviceIntent;
public Cursor cursor;
int version ;
//final MapController control = gmap.getController();
GeoPoint gp = new GeoPoint((int)(Lat * 1e6),(int)(Long * 1e6));
//Context c = getApplicationContext();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
Context context;
context = getApplicationContext();
Log.e("context",""+context);
//db = new DBopenHelper(context);
db = new DBopenHelper(this,1);
guiHandler = new GuiHandler(this);
Log.d("put", "database");
gmap=(MapView)findViewById(R.id.mapview);
gmap.setClickable(true);
gmap.setBuiltInZoomControls(true);
mOverlay=new MyLocationOverlay(this,gmap);
//control.setCenter(gp);
// control.animateTo(gp);
gmap.getOverlays().add(mOverlay);
Lat=44.497271;
Long=11.352693;
updateMapOverlay();
db.close();
messenger = new Messenger(guiHandler);
serviceIntent = new Intent(MapsActivity.this,MessaggeService.class);
serviceIntent.putExtra(MessaggeService.CALLER, MessaggeService.POSITION);
serviceIntent.putExtra(MessaggeService.MESSENGER, messenger);
startService(serviceIntent);
Log.e("Debug", "Service Started from activity");
//ms = new MessaggeService(overlay,gmap,c);
///destLat = mOverlay.getMyLocation().getLatitudeE6();
//destLong = mOverlay.getMyLocation().getLongitudeE6();
}
private void addOpenPoint(GeoPoint g) {
OverlayItem overlayitem = new OverlayItem(g, "", "");
overlay.addOverlay(overlayitem);
Log.e("over",""+overlayitem);
}
protected void onResume() {
super.onResume();
mOverlay.enableMyLocation();
mOverlay.runOnFirstFix(new Runnable() {
// @Override
public void run() {
//gmap.invalidate();
gmap.getController().animateTo(mOverlay.getMyLocation());
gmap.getController().setZoom(16);
//updateMapOverlay();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_maps, menu);
return true;
}
void updateMapOverlay() {
List<Overlay> mapOverlays = gmap.getOverlays();
Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher);
//mapOverlays.clear();
overlay = new OverlayList(drawable, this);
mapOverlays.add(overlay);
Cursor cursor = db.getZtlStreet();
if (cursor.moveToFirst()) {
while(cursor.moveToNext()) {
Integer lat = (int) (cursor.getDouble(cursor.getColumnIndex("latitude")) * 1e6);
Integer lon = (int) (cursor.getDouble(cursor.getColumnIndex("longitude")) * 1e6);
GeoPoint p = new GeoPoint(lat, lon);
addOpenPoint(p);
}
}
}
public static class GuiHandler extends Handler {
// TODO da unificare con quello ChooseEVSEActivity
public static final int FALSE = 0;
public static final int TRUE = 1;
// tiene il conto del tempo che è passato dall'ultima volta;
WeakReference<MapsActivity> wrActivity;
public GuiHandler(MapsActivity activity) {
wrActivity = new WeakReference<MapsActivity>(activity);
}
public void handleMessage(android.os.Message msg) {
final MapsActivity activity = wrActivity.get();
/*Create alert dialog to access on ztl street*/
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setMessage("Accesso Ztl non puoi accedere");
builder.setPositiveButton("chiudi",new DialogInterface.OnClickListener() {
//@Override
public void onClick(DialogInterface dialog, int id) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
switch (msg.what) {
case MessaggeService.UPDATE_UI_MSG:
AlertDialog alertMsg = builder.create();
alertMsg.show();
break;
}
}
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
My service with message is :
for(int i = 0 ; i< overlay.size();i++){
Log.e("item : ","n"+overlay.size());
OverlayItem item = overlay.getItem(i);
Location l1 = new Location("");
l1.setLatitude(item.getPoint().getLatitudeE6());
Log.e("l1", "l1"+l1.getLatitude());
l1.setLongitude(item.getPoint().getLongitudeE6());
Location l2 = new Location("");
l2.setLatitude(destLat);
l2.setLongitude(destLong);
Log.e("l2", "l2"+l2.getLatitude());
try {
String caller = "generic";
try {
caller = intent.getStringExtra(CALLER);
Bundle extras = intent.getExtras();
if (extras != null) {
if (POSITION.equals(caller)) {
positionMessenger = (Messenger) extras.get(MESSENGER);
Log.e("position",""+positionMessenger);
}
}
System.out.println("Instantiating " + caller + " Messenger OK :" + positionMessenger);
} catch (Exception e) {
//Log.e(UpdaterService.class.getName(), "Error instantiating " + caller + " Messenger" + e.getMessage());
//se il servizio non è ancora partito allora esco da esso in modo che venga istanziato correttamente
if(!doUpdate) {
stopSelf();
}
}
if (l2.distanceTo(l1) >100) {
Log.e("distance","Ztl non puoi accedere");
Message msg = Message.obtain();
Log.e("msg",""+msg);
msg.what = UPDATE_UI_MSG;
positionMessenger.send(msg);
Log.e("send","send");
}} catch (RemoteException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}