1

サービスがあります。サービスは別のスレッドを開始します。このスレッドが完了すると、Google マップを表示する MapPage.class という新しい fragmentactivity を開始する必要があります。スレッドは、サーバーから GPS 座標を含む json を取得します。これらの座標をシングルトン クラスに保存しています。そのため、スレッドが完了したら、シングルトン クラスのデータを使用して MapPage fragmentactivity を開始する必要があります。

バックグラウンド スレッドから MapPage.class を開始する方法がわかりません。メイン アクティビティのハンドラーの使用例を見てきましたが、問題にどのように適用できるかわかりません。私が見つけた最も近い答えはこれでした。

ハンドラーを作成することが唯一のオプションかどうかを知りたいですか? 他に選択肢があれば?アプリケーション クラスでハンドラーを使用することは、私の問題に対して正しいことですか?

私のサービスクラス:

     public class TrackingService extends Service {
    private class MyLocationListener implements LocationListener {
        private final Context context;

        public MyLocationListener(Context context) {
            this.context = context;
        }

        @Override
        public void onLocationChanged(Location location) {
            double latitude = location.getLatitude();
            double longtude = location.getLongitude();
            double altitude = location.getAltitude();
            double accuracy = location.getAccuracy();
            float bearing = location.getBearing();
            String provider = location.getProvider();
            Cood cood = new Cood(latitude, longtude, altitude, accuracy,
                    new Date().getTime(), bearing, provider);

            LocationInformation.getInstanceof().getMapPoint().getCood()
                    .add(cood);

            int size = LocationInformation.getInstanceof().getMapPoint()
                    .getCood().size();

            mBuilder.setContentTitle("GPS Tracker - " + size + "/"
                    + SystemSettings.getInstanceOf().getSize());
            NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            manager.notify(1, mBuilder.build());

            if (size >= SystemSettings.getInstanceOf().getSize()) {
                Toast.makeText(context, "before creating new thread for Track",
                        Toast.LENGTH_SHORT).show();
                new Thread(new Track(context)).start();
                Toast.makeText(context, "after creating new thread for Track",
                        Toast.LENGTH_SHORT).show();

            }
        }

        @Override
        public void onProviderDisabled(String provider) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    }

    LocationManager manag;
    LocationListener locationListener;
    private Context context;
    private Long frequency;
    private Integer minDistance;
    private String deviceRegNo;
    private Integer pointCount;
    private Boolean mode;
    private Builder mBuilder;

    @Override
    public void onCreate() {
        super.onCreate();
        context = this;
    }

    @Override
    public IBinder onBind(Intent arg0) {

        return null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }

    @Override
    public boolean onUnbind(Intent intent) {

        return super.onUnbind(intent);
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {

    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();

    }

    @Override
    public void onRebind(Intent intent) {
        super.onRebind(intent);

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        if (intent == null) {

            return Service.START_STICKY;
        }

        manag = (LocationManager) context
                .getSystemService(Context.LOCATION_SERVICE);

        locationListener = new MyLocationListener(context);

        frequency = (Long) intent.getExtras().get("frequency");
        minDistance = (Integer) intent.getExtras().get("minDistance");
        deviceRegNo = (String) intent.getExtras().get("deviceRegNo");
        pointCount = (Integer) intent.getExtras().get("pointCount");
        mode = (Boolean) intent.getExtras().get("prodMode");

        if (frequency == null) {
            Toast.makeText(context, "frequency null", Toast.LENGTH_SHORT)
                    .show();
            return Service.START_STICKY;
        }

        if (minDistance == null) {

            return Service.START_STICKY;
        }

        if (deviceRegNo == null) {

            return Service.START_STICKY;
        }

        if (pointCount == null) {
            Toast.makeText(context, "pointCount null", Toast.LENGTH_SHORT)
                    .show();
            return Service.START_STICKY;
        }
        if (mode == null) {
            Toast.makeText(context, "mode is null", Toast.LENGTH_SHORT).show();
            return Service.START_STICKY;
        }

        if (LocationInformation.getInstanceof().getMapPoint().getDevice() == null) {
            Device device = new Device();
            device.setDeviceRegNo(deviceRegNo);
            LocationInformation.getInstanceof().getMapPoint().setDevice(device);
        }

        SystemSettings.getInstanceOf().setSize(pointCount);
        SystemSettings.getInstanceOf().setProdMode(mode);
        Toast.makeText(context, "On the line before pendingIntent statement",
                Toast.LENGTH_SHORT).show();
        Intent resultIntent = new Intent(context, SettingScreen.class);
        PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0,
                resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        int size = LocationInformation.getInstanceof().getMapPoint().getCood()
                .size();

        mBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("GPS Tracker - " + size + "/" + pointCount)
                .setContentText(
                        LocationInformation.getInstanceof().getMapPoint()
                                .getDevice().getDeviceRegNo())
                .setContentIntent(resultPendingIntent);
        Toast.makeText(context, "On the line after NotificationCompat.builder",
                Toast.LENGTH_SHORT).show();

        Notification notification = mBuilder.build();
        Toast.makeText(context, "On the line after mBuilder.build",
                Toast.LENGTH_SHORT).show();
        startForeground(1, notification);
        Toast.makeText(context, "On the line after startForeground",
                Toast.LENGTH_SHORT).show();

        manag.requestLocationUpdates(LocationManager.GPS_PROVIDER, frequency,
                minDistance, locationListener);
        return Service.START_STICKY;
    }
}

私のスレッドコードは次のとおりです。

     HttpResponse response = httpClient.execute(httpPost);
        BufferedReader br = new BufferedReader(new InputStreamReader(
                response.getEntity().getContent()));
        String line = "";
        while ((line = br.readLine()) != null) {
            text += line;
            System.out.println("RESULT: " + line);
        }
        MapCoords.getInstanceof().setMapPoint(
                (gson.fromJson(jsonString, MapCoords.class)).getMapPoint());

ここで MapPage アクティビティを開始する必要があります。マイ MapPage アクティビティ コードは次のとおりです。

        public class MapPage extends FragmentActivity {
    ArrayList<Cood> cd = MapCoords.getInstanceof().getMapPoint().getCood();
    Double latitude = cd.get(0).getLatitude();
    Double longitude = cd.get(0).getLongitude();
    LatLng point1;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.basic_demo);
        Toast.makeText(this, "Entered MapPage Activity", Toast.LENGTH_LONG)
                .show();
        setUpMapIfNeeded();

    }

    @Override
    protected void onResume() {
        super.onResume();
        setUpMapIfNeeded();
    }

    private void setUpMapIfNeeded() {

        if (mMap == null) {
            mMap = ((SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map)).getMap();
            point1 = new LatLng(latitude, longitude);
            mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(point1, 17));
            if (mMap != null) {
                setUpMap();
            }
        }
    }

    private void setUpMap() {
        mMap.addMarker(new MarkerOptions()
                .position(point1)
                .title("Point1")
                .snippet("This is the first GPS co-ordinate"));
    }

}
4

1 に答える 1

2

好きな場所からアクティビティを開始できます。インテントに new_task フラグを追加する必要がある場合があります。

    Intent intent = new Intent(context, MapPage.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intent); 

バックグラウンド スレッドがサービス内にある場合、 your_service_class.this をコンテキストとして渡すことができます。それ以外の場合は、非アクティビティまたは非サービス クラスの場合、コンテキストをバックグラウンド スレッドに渡す必要があります。

于 2014-03-26T07:56:26.317 に答える