【问题标题】:I'm trying to initiate a class and pass the result to my intent then pass two values through my intent我正在尝试启动一个类并将结果传递给我的意图,然后通过我的意图传递两个值
【发布时间】:2013-06-13 22:26:55
【问题描述】:

我已经为此苦苦挣扎了一段时间,并决定寻求一些帮助。我已经阅读了文献,但似乎仍然无法让这段代码为我工作。我正在使用Fedor's 代码尝试返回位置并按照我的意图显示它。我希望在下一个活动中显示时间/日期和位置。时间/日期工作正常我无法启动 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);
    }
}

这是我的第一个应用程序

【问题讨论】:

    标签: android class android-intent location


    【解决方案1】:

    您好,我认为应该是这样的:

    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% 确定所使用的方法,但它应该会给您一个良好的开端。

    【讨论】:

    • 我尝试了代码并将再次尝试感谢帮助人员刚刚开始调整它以使其工作感谢您的回复。
    【解决方案2】:

    您可以使用捆绑软件添加它

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

    【讨论】:

    • 很抱歉,我花了这么长时间才与你们联系。我尝试了一个捆绑包,但它不能正常工作。现在我在 mylocation.getLocation 行上不断收到一个错误,说它正在寻找一些无法确定是代码还是我的 java 损坏的东西。
    猜你喜欢
    • 1970-01-01
    • 2014-12-28
    • 1970-01-01
    • 1970-01-01
    • 2021-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多