2014-03-06 22 views
2

我正在開發一個應用程序,我想要顯示地圖,當我打開通知,我收到通知成功,但是當我打開通知沒有打開地圖與lat值長。如何在打開通知時在地圖上顯示lat-lng點?

GCMIntent服務類

public class GcmIntentService extends IntentService{ 

Context context; 
public static int notify_no=0; 

//System.currentTimeMillis(); 

private NotificationManager mNotificationManager; 
NotificationCompat.Builder builder; 
public static final String TAG = "GCM NOTIFICATION"; 

public GcmIntentService() { 
    super("GcmIntentService"); 
    // TODO Auto-generated constructor stub 
} 

@Override 
protected void onHandleIntent(Intent intent) { 
    // TODO Auto-generated method stub 
    Bundle extras = intent.getExtras(); 
    String msg = intent.getStringExtra("message"); 
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this); 
    String messageType = gcm.getMessageType(intent); 


    if (!extras.isEmpty()) { 

     if (GoogleCloudMessaging. 
        MESSAGE_TYPE_SEND_ERROR.equals(messageType)) { 
       // sendNotification(RegIdDTO.REG_ID,"Send error: " + extras.toString()); 
      sendNotification(this,msg); 
      } else if (GoogleCloudMessaging. 
        MESSAGE_TYPE_DELETED.equals(messageType)) { 
       // sendNotification(RegIdDTO.REG_ID,"Deleted messages on server: " + 
       //   extras.toString()); 
       sendNotification(this,msg); 
      // If it's a regular GCM message, do some work. 
      } else if (GoogleCloudMessaging. 
        MESSAGE_TYPE_MESSAGE.equals(messageType)) { 
       // This loop represents the service doing some work. 
       for (int i=0; i<5; i++) { 
        Log.i(TAG, "Working... " + (i+1) 
          + "/5 @ " + SystemClock.elapsedRealtime()); 
        try { 
         Thread.sleep(500); 
        } catch (InterruptedException e) { 
        } 
       } 
       Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime()); 
       // Post notification of received message. 
       //sendNotification("Received: " + extras.toString()); 
       // sendNotification(RegIdDTO.REG_ID,msg); 
       sendNotification(this,msg); 
       Log.i(TAG, "Received: " + extras.toString()); 
      } 
     } 
    GcmBroadcastReceiver.completeWakefulIntent(intent); 
} 


private static void sendNotification(Context context,String message) { 
    int icon = R.drawable.ic_stat_gcm; 
    long when = System.currentTimeMillis(); 
    NotificationCompat.Builder nBuilder; 
    Uri alarmSound = RingtoneManager 
      .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    nBuilder = new NotificationCompat.Builder(context) 
      .setSmallIcon(R.drawable.ic_launcher) 
      .setContentTitle("header") 
      .setLights(Color.BLUE, 500, 500).setContentText(message) 
      .setAutoCancel(true).setTicker("Notification from Traffic") 
      .setVibrate(new long[] { 100, 250, 100, 250, 100, 250 }) 
      .setSound(alarmSound); 
    String consumerid = null; 
    Integer position = null; 
      // write your click event here 
     Intent resultIntent = new Intent(context, ShowMapActivity.class); 

     resultIntent.putExtra("message", message); 
     // resultIntent.setData(Uri.parse("content://"+when)); 


PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 
     notify_no, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
     // Show the max number of notifications here 
if (notify_no < 9) { 
    notify_no = notify_no + 1; 
} else { 
    notify_no = 0; 
} 
nBuilder.setContentIntent(resultPendingIntent); 
NotificationManager nNotifyMgr = (NotificationManager) context 
     .getSystemService(context.NOTIFICATION_SERVICE); 
nNotifyMgr.notify(notify_no + 2, nBuilder.build()); 
} 

} 

ShowMap類

public class ShowMapActivity extends FragmentActivity implements LocationListener { 

    GoogleMap _googleMap; 

    LatLng myPosition; 
    LocationManager locationManger; 


    TextView addressTextView,cTimeTextView; 

     JSONObject json; 
     GcmIntentService serv; 
     Context mContext; 

    Intent noticeIntent = getIntent(); 

    String lat; 
    String lng; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_show_map); 

    //addressTextView = (TextView)findViewById(R.id.addressId); 
    //cTimeTextView = (TextView)findViewById(R.id.ctimeId); 

    String message = noticeIntent.getExtras().getString("message"); 

    try { 
     json = new JSONObject(message); 
     lat = json.getString("lat"); 

     Log.e("LLOONNGG", ""+lat); 
     lng = json.getString("lng"); 

     Log.e("LLOONNGG", ""+lng); 
     String adr = json.getString("address"); 
     addressTextView.setText(adr); 


     String ctime = json.getString("ctime"); 
     cTimeTextView.setText(ctime); 


    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 


    _googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(
      R.id.map)).getMap(); 

    LocationManager service = (LocationManager)getSystemService(LOCATION_SERVICE); 

    boolean enableGPS = service.isProviderEnabled(LocationManager.GPS_PROVIDER); 
    boolean enableWiFi= service.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

    if(!enableGPS || !enableWiFi){ 
     Toast.makeText(ShowMapActivity.this, "GPS signal not found", Toast.LENGTH_LONG).show(); 
     Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
     startActivity(intent); 
    } 

    locationManger = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    Criteria criteria = new Criteria(); 
    String provider = service.getBestProvider(criteria, true); 


    service.requestLocationUpdates(provider, 0, 0, this); 


    if(_googleMap==null){ 
     Toast.makeText(getApplicationContext(), "Google Map Not Available", Toast.LENGTH_LONG).show(); 
     } 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.show_map, menu); 
    return true; 
} 

@Override 
public void onLocationChanged(Location location) { 
    // TODO Auto-generated method stub 

    //locationManger.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this); 
    _googleMap.clear(); 
    ArrayList<HashMap<String, String>> arl = (ArrayList<HashMap<String, String>>) 
      getIntent().getSerializableExtra("arrayList"); 


    /*if(location!=null){ 
     double latitude = location.getLatitude(); 
     double langitude = location.getLongitude(); 

     LatLng latlang = new LatLng(latitude, langitude); 
     //LatLngBounds curScreen =  
      _googleMap.getProjection().getVisibleRegion().latLngBounds; 
     //curScreen.contains(latlang); 
     myPosition = new LatLng(latitude, langitude); 

      // Show only Current Location 

     _googleMap.moveCamera(CameraUpdateFactory.newLatLng(myPosition)); 
     _googleMap.addMarker(new 
     MarkerOptions().position(myPosition).title("start")); 

    }*/ 

    if (!lat.trim().equals("") && !lng.trim().equals("")) { 
     double Hlat = Double.parseDouble(lat.trim()); 
     double Hlong= Double.parseDouble(lng.trim()); 

     LatLng dabaseLocations =new LatLng(Hlat, Hlong); 

     Log.e("LLAATTAA", ""+Hlat); 
     Log.e("LLOONNGG", ""+Hlong); 


     // Show current location with database locations 

      _googleMap.moveCamera(CameraUpdateFactory.newLatLng(dabaseLocations)); 
     _googleMap.addMarker(new  
     MarkerOptions().position(dabaseLocations).title("start")); 
      } 
} 

@Override 
public void onProviderDisabled(String provider) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void onProviderEnabled(String provider) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void onStatusChanged(String provider, int status, Bundle extras) { 
    // TODO Auto-generated method stub 

} 
@Override 
protected void onResume() { 
    // TODO Auto-generated method stub 
    super.onResume(); 
    serv=new GcmIntentService(); 
    //serv.CancelNotification(getApplicationContext()); 

} 

} 

我logcat的

03-06 13:03:40.187: E/AndroidRuntime(18546): java.lang.RuntimeException: Unable to start activity 

logcat的

ComponentInfo{com.technowellServices.traffic/com.technowellServices.traffic.ShowMapActivity}: 
     java.lang.NullPointerException 
     03-06 13:03:40.187: E/AndroidRuntime(18546): at 
     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1728) 
     03-06 13:03:40.187: E/AndroidRuntime(18546): at 
     android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1747) 
     03-06 13:03:40.187: E/AndroidRuntime(18546): at 
     android.app.ActivityThread.access$1500(ActivityThread.java:155) 
     03-06 13:03:40.187: E/AndroidRuntime(18546): at 
     android.app.ActivityThread$H.handleMessage(ActivityThread.java:993) 
    03-06 13:03:40.187: E/AndroidRuntime(18546): at 
    android.os.Handler.dispatchMessage(Handler.java:130) 
    03-06 13:03:40.187: E/AndroidRuntime(18546): at android.os.Looper.loop(SourceFile:351) 
    03-06 13:03:40.187: E/AndroidRuntime(18546): at 
     android.app.ActivityThread.main(ActivityThread.java:3814) 
    03-06 13:03:40.187: E/AndroidRuntime(18546): at 
     java.lang.reflect.Method.invokeNative(Native Method) 
    03-06 13:03:40.187: E/AndroidRuntime(18546): at j 
    ava.lang.reflect.Method.invoke(Method.java:538) 
    03-06 13:03:40.187: E/AndroidRuntime(18546): at 
    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901) 
    03-06 13:03:40.187: E/AndroidRuntime(18546): at 
    com.android.internal.os.ZygoteInit.main(ZygoteInit.java:659) 
    03-06 13:03:40.187: E/AndroidRuntime(18546): at dalvik.system.NativeStart.main(Native 
    Method) 
    03-06 13:03:40.187: E/AndroidRuntime(18546): Caused by: java.lang.NullPointerException 
    03-06 13:03:40.187: E/AndroidRuntime(18546): at 
    com.technowellServices.traffic.ShowMapActivity.onCreate(ShowMapActivity.java:66) 
    03-06 13:03:40.187: E/AndroidRuntime(18546): at 
    android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082) 
    03-06 13:03:40.187: E/AndroidRuntime(18546): at 
    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1692) 
+0

是你的活動開放? –

+0

是'manifest.xml'中的活動註冊嗎? –

+0

不看我的活動 – Durga

回答

1

你不能稱之爲

Intent noticeIntent = getIntent(); 

onCreate()方法之前。

你應該把它放在 onCreate()方法中。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_show_map); 
    Intent noticeIntent = getIntent(); 
    // ... 
} 

改變這種從

String message = noticeIntent.getExtras().getString("message"); 

String message = noticeIntent.getStringExtras("message"); 
+0

sry爲遲來的響應其即將到來的地圖,但我想直接去我的位置,而不從地圖縮小,.how可能請告訴我 – Durga

+0

你可以使用CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng,10); map.animateCamera(cameraUpdate); – Piyush

+0

雅它工作只有一次,從第二次通知起,不直接顯示 – Durga

1

您應該創建noticeIntentonCreate(....)ShowMapActivity

Intent noticeIntent = getIntent(); 
String message = noticeIntent.getStringExtras("message"); 
相關問題