0

我已经为此苦苦挣扎了一段时间,并决定寻求帮助。我已经阅读了文献,但似乎仍然无法让这段代码为我工作。我正在使用Fedor 的代码来尝试返回位置并按照我的意图显示它。我希望在下一个活动中显示时间/日期和位置。时间/日期工作正常我无法启动 MyLocation 类并将该值传递给意图。任何帮助或指向任何其他资源都会很酷。谢谢。

这是我到目前为止所拥有的。

package com.tree;

import java.util.Calendar;
import android.app.Activity;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.view.View;
import com.tree.MyLocation.LocationResult;

    public class MainActivity extends Activity {

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

   LocationResult locationResult = new LocationResult(){
        @Override
        public void gotLocation(Location location){
            //Got the location!
        }
    };
    MyLocation myLocation = new MyLocation();
    myLocation.getLocation(this, locationResult);


    public void clockin (View view){
        Intent intent = new Intent (this, Mainmenu.class);
        String timedate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime()) , locationResult;
        intent.putExtra("TIME_DATE", timedate);
        startActivity(intent);
    }
}

这是我的第一个应用程序

4

2 回答 2

0

嗨,我认为应该是这样的:

public class MainActivity extends Activity {

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

        // get the location
        MyLocation myLocation = new MyLocation();
        myLocation.getLocation(this, locationResult);

    }

   LocationResult locationResult = new LocationResult(){
        @Override
        public void gotLocation(Location location){
            //Got the location! Then send the location and time...
            Intent intent = new Intent (this, Mainmenu.class);
            String timedate =       java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime()) , locationResult;
            // set the time/date
            intent.putExtra("TIME_DATE", timedate);
            // set the location
            intent.putExtra("LOCATION_LAT", location.getLatitude());
            intent.putExtra("LOCATION_LONG", location.getLongitude());
            // start the new activity..
            startActivity(intent);
        }
    };



    public void clockin (View view){
        Intent intent = new Intent (this, Mainmenu.class);
        String timedate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime()) , locationResult;
        intent.putExtra("TIME_DATE", timedate);
        startActivity(intent);
    }
}

请阅读文档LocationIntent因为我从脑海中编写了这段代码,我不是 100% 确定使用的方法,但它应该给你一个好的开始。

于 2013-06-17T21:52:52.263 回答
0

您可以使用捆绑包添加它

...
Bundle b = new Bundle();
...
b.putSerializable( "location", LocationObject ); //make sure Location class implement Serializable

intent.putExtras( b );

...
于 2013-06-17T22:20:07.353 回答