数据来自通知时,Android MapFragment为空

时间:2018-10-03 16:26:39

标签: android google-maps notifications location fragment

我正在尝试从通知中打开地图片段。当我单击通知时,我打开地图片段,但地图从未加载。进度栏始终可见。如果我转到另一个片段,然后单击位置文本,则将完美打开地图片段。我该如何解决该错误?

通知:

    NotificationManager notificationManager=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    final String CHANNEL_ID="channel_05";


    if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
        CharSequence name=context.getString(R.string.app_name);
        NotificationChannel mChannel=new NotificationChannel(CHANNEL_ID,name,NotificationManager.IMPORTANCE_DEFAULT);

        assert notificationManager!=null;
        notificationManager.createNotificationChannel(mChannel);
    }

    NotificationCompat.Builder builder=new NotificationCompat.Builder(context,CHANNEL_ID);
    Intent resulIntent=new Intent(context, DrawerActivity.class);
    resulIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
    resulIntent.putExtra("From", "notifyFrag");
    resulIntent.putExtra("longitude",40.15242);
    resulIntent.putExtra("latitude",29.15242);

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    PendingIntent pendingIntent=PendingIntent.getActivity(context,3,resulIntent,PendingIntent.FLAG_ONE_SHOT);
    builder.setSmallIcon(R.drawable.ic_map_black);
    builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
    builder.setAutoCancel(true);

    builder.addAction(R.drawable.ic_map_black,"ViewOn The Map",pendingIntent);
    builder.setContentIntent(pendingIntent);

   ////
    assert notificationManager !=null;
    notificationManager.notify(NOTIFICATION_ID,builder.build());

在抽屉活动中编写代码部分以打开地图片段:

 String type = getIntent().getStringExtra("From");
    if (type != null) {
        switch (type) {
            case "notifyFrag":

                notificationManager.cancel(MainFragment.NOTIFICATION_ID);
                Fragment  fragment= new MapsFragment();
                Bundle bundle=new Bundle();
                bundle.putDouble("longitude",getIntent().getDoubleExtra("longitude",0.0));
                bundle.putDouble("latitude",getIntent().getDoubleExtra("latitude",0.0));
                bundle.putString("isim","emre");
                bundle.putLong("tarih",135486521421424L);
                fragment.setArguments(bundle);
                fragmentManager= getSupportFragmentManager();
                fragmentManager.beginTransaction().replace(R.id.container_content_drawer, fragment).commit();
                break;
}

还有我的地图片段:

public class MapsFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    mView=inflater.inflate(R.layout.fragment_maps, container, false);


    progressBar=mView.findViewById(R.id.ll_progress);
    Bundle bundle=getArguments();
    longitude=bundle.getDouble("longitude");
    latitude=bundle.getDouble("latitude");

    String status = getConnectivityStatusString(getActivity());
    internetConnected = status.equalsIgnoreCase("Wifi enabled") || status.equalsIgnoreCase("Mobile data enabled");
    isMapInstalled = internetConnected;

    if(isMapInstalled)
        progressBar.setVisibility(View.VISIBLE);

    return mView;
}

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    mapView=mView.findViewById(R.id.map);

        mapView.onCreate(null);
        mapView.onResume();
        mapView.getMapAsync(callback);

}

    private OnMapReadyCallback callback=new OnMapReadyCallback() {
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mapView.setVisibility(View.INVISIBLE);

        MapsInitializer.initialize(getContext());
        MgoogleMap = googleMap;
        googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        Marker marker = googleMap.addMarker(new MarkerOptions().position(new 
LatLng(latitude, longitude)));
        marker.showInfoWindow();

        CameraPosition cameraPosition = CameraPosition.builder().target(new 
LatLng(latitude, longitude))
                .zoom(15).bearing(0).tilt(45).build();
        googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition), 5000, null);

        googleMap.setOnMapLoadedCallback(onMapLoadedCallback);

    }
};
 GoogleMap.OnMapLoadedCallback onMapLoadedCallback=new 
     GoogleMap.OnMapLoadedCallback() {
    @Override
    public void onMapLoaded() {
        progressBar.setVisibility(View.GONE);
        mapView.setVisibility(View.VISIBLE);


    }
};

0 个答案:

没有答案