简体   繁体   English

在仿真器上工作但不在真正的Android设备上

[英]Working on Emulator but not on the real Android device

I have made an Android Application which shows a Google Map on the Top Half of the Android Screen and in the bottom half of the Android screen it shows a TextView. 我制作了一个Android应用程序,它在Android Screen的上半部分显示了一个Google Map ,在Android Screen的下半部分显示了一个TextView。

And on my Google Maps(on the Top half of the android screen) , I am drawing a Circle on my current location, and I am passing my current location (lat and long values) from the DDMS perspective in the emulator . 在我的Google Maps(on the Top half of the android screen) ,我在当前位置绘制一个Circle ,我DDMS perspective in the emulatorDDMS perspective in the emulator传递我current location (lat and long values) And it is working fine in the emulator. 它在模拟器中运行良好。

When I launch an application on the emulator, first of all it shows a Google Map on the Top Half of the android screen and in the bottom half, it shows a TextView. 当我在模拟器上启动应用程序时,首先它在Android屏幕的上半部分显示了一个Google Map,在下半部分显示了一个TextView。 And then with the use of DDMS Perspective, I am passing my current location (lat and long values) and after passing, it draws a Circle on my current location on the Google maps. 然后使用DDMS Perspective,我传递了我当前的位置(lat和long值),并且在传递之后,它在Google地图上的当前位置绘制了一个Circle。

And everything works fine in the emulator. 一切都在模拟器中正常工作。

Problem Statement:- 问题陈述:-

But when I am trying the same thing on the real Android Phone , only my Google Maps is getting shown properly on the Top half of the android screen and the TextView on the Bottom Half of the Android Screen. 但是当我在真正的Android Phone上尝试同样的事情时,只有我的Google Maps在Android屏幕的上半部分和Android屏幕下半部分的TextView上正确显示。 But it is not drawing the Circle on my Current Location . 但它并没有Circle on my Current Location绘制Circle on my Current Location

I am connected to wi-fi on my Android phone. 我在Android手机上连接了Wi-Fi。 And I don't think so I need to pass the Latitude and Longitude values on the Android Phone, right? 我不这么认为我需要在Android手机上传递纬度和经度值,对吗? It should take the current location automatically right? 它应该自动采取当前位置吗?

Or Is there anything I need to do on my Android Phone? 或者我的Android手机上有什么需要做的吗?

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);    

    locationListener = new GPSLocationListener();

    locationManager.requestLocationUpdates(
            LocationManager.GPS_PROVIDER, 
            0, 
            0, 
            locationListener);

    mapView = (MapView) findViewById(R.id.mapView);
    listView = (ListView) findViewById(R.id.mylist);
    mapView.setStreetView(true);
    mapView.setBuiltInZoomControls(true);

    mapController = mapView.getController();
    mapController.setZoom(15);
}

private class GPSLocationListener implements LocationListener {
    @Override
    public void onLocationChanged(Location location) {
        if (location != null) {
            GeoPoint point = new GeoPoint(
                    (int) (location.getLatitude() * 1E6), 
                    (int) (location.getLongitude() * 1E6));

            findUsersInCurrentRadius(4,location.getLatitude(),location.getLongitude());

            mapController.animateTo(point);
            mapController.setZoom(15);

            // add marker
            MapOverlay mapOverlay = new MapOverlay(this,android.R.drawable.star_on,R.drawable.tenm,R.drawable.twentym,R.drawable.thirtym,R.drawable.fourtym);
            mapOverlay.setPointToDraw(point);
            List<Overlay> listOfOverlays = mapView.getOverlays();
            listOfOverlays.clear();
            listOfOverlays.add(mapOverlay);

            String address = ConvertPointToLocation(point);
            Toast.makeText(getBaseContext(), address, Toast.LENGTH_SHORT).show();

            mapView.invalidate();
        }
    }


class MapOverlay extends Overlay {
    private GeoPoint pointToDraw;
    int[] imageNames=new int[6];

    public MapOverlay(GPSLocationListener gpsLocationListener,
            int currentUser, int tenm, int twentym, int thirtym, int fourtym) {
        imageNames[0]=currentUser;
        imageNames[1]=tenm;
        imageNames[2]=twentym;
        imageNames[3]=thirtym;
        imageNames[4]=fourtym;
    }

    public void setPointToDraw(GeoPoint point) {
        pointToDraw = point;
    }

    public GeoPoint getPointToDraw() {
        return pointToDraw;
    }

    @Override
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
        super.draw(canvas, mapView, shadow);
        //---translate the GeoPoint to screen pixels---
        Point screenPts = new Point();
        mapView.getProjection().toPixels(pointToDraw, screenPts);
        //--------------draw circle----------------------
        Point pt = mapView.getProjection().toPixels(pointToDraw,screenPts);
        Paint circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        circlePaint.setColor(0x30000000);
        circlePaint.setStyle(Style.FILL_AND_STROKE);

        int totalCircle=4;
        int radius=40;
        int centerimagesize=35;

        for (int i = 1; i <= totalCircle; i ++) { 
            canvas.drawCircle(screenPts.x,screenPts.y, i*radius, circlePaint); 
        } 
        canvas.drawBitmap(BitmapFactory.decodeResource(getResources(),imageNames[0]), (screenPts.x-(centerimagesize/2)),(screenPts.y-(centerimagesize/2)), null);
        super.draw(canvas,mapView,shadow);


        return true;
    }
} 

And my AndroidManifest.xml file- 而我的AndroidManifest.xml文件 -

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.circlemapandroid"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="3"
        android:targetSdkVersion="8" />

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:debuggable="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <uses-library android:name="com.google.android.maps" />

        <activity
            android:name=".ThesisProjectAndroid"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

I can see from your code, that the circle is only drawn after the first fix is received. 我可以从您的代码中看到,只有在收到第一个修复后才会绘制圆圈。 Do you have GPS enabled in the phone settings (Location and security)? 您是否在手机设置中启用了GPS(位置和安全性)? If not, enable it. 如果没有,请启用它。

You will get the circle only after you get the first fix from GPS. 只有在从GPS获得第一个修复后,您才能获得圆圈。 You need to take the GPS outdoor to get it. 您需要将GPS带到室外才能获得它。

You have a Toast in Location Listener, so if you are getting a Toast with the value of the address, none of the above will solve your problem. 你在位置监听器中有一个Toast,所以如果你得到一个带有地址值的Toast,以上都不会解决你的问题。

Finally for efficiency , I would move the code to create and add the overlay out of the Location Listener, as it's somehow heavy to have it created and removed everytime you receive a new fix. 最后为了提高效率,我会移动代码来创建并添加覆盖范围内的侦听器,因为每次收到新修补程序时都会创建并删除它。

您缺少GPS的另一个权限,请在AndroidManifest.xml中添加此权限

<uses-permission android:name="android.permission.ACCESS_GPS"></uses-permission>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 应用程序在模拟器上运行,但无法在真实设备上运行 - App running on emulator but not working on real device Android小部件仅在模拟器上有效,而不能在真实设备上有效 - Android widget works only on emulator not on real device android 应用程序适用于模拟器但不适用于真实设备 - android app works on emulator but not in real device Android应用程序可以在模拟器上正常运行,但不能在真实设备上运行 - Android Application works fine on emulator but not on a real device Android Studio 应用程序可以在模拟器中运行,但不能在真实设备上运行 - Android Studio app works in emulator but not on a real device 我的应用程序正在我的Android模拟器上工作,但它不能在我的真实移动设备上工作 - My app is working on my android emulator however it is not working on my real mobile device [linphone-android]正在使用所有模拟器,但停止了在我的真实设备上工作4.4.2 - [linphone-android]working all emulator but stopped working my real device 4.4.2 尝试在 Android 设备上检索位置...适用于模拟器但不适用于真实设备 - Attempting to retrieve location on Android Device... works on emulator but not real device Android Studio 4.1.2 无法在模拟器或真机上运行 android studio - Android Studio 4.1.2 unable to run android studio on emulator or real device 后退按钮在模拟器上运行,但在Android设备中不运行 - back button is working on emulator but it is not working in device in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM