因为内容有点多,所以我想分开写,没错,我就是来骗访问量的,但里面的功能真的可以用,希望大家采纳😊😊

效果演示:

android 签到按钮 安卓定位签到_android 签到按钮

首先,我这里使用的是百度定位,百度定位需要获取秘钥,也就是配置AK,获取方法:http://lbsyun.baidu.com/index.php?title=android-locsdk/guide/create-project/key

百度地图定位的开发包下载地址:http://lbs.baidu.com/index.php?title=sdk/download&action#selected=location_all 选择基础定位或全量定位,然后下载,下载完成后参照下面链接所示的方法进行配置:

Eclipse配置:http://lbsyun.baidu.com/index.php?title=android-locsdk/guide/create-project/eclipse

Android
studio配置:http://lbsyun.baidu.com/index.php?title=android-locsdk/guide/create-project/android-studio


接下来就是开始写代码了,具体方法如下:

1、先来设计签到入口和签到页面的布局

(1)签到入口activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/login_bg"
    tools:context="${relativePackage}.${activityClass}" >

    <RelativeLayout 
   	    android:layout_width="match_parent"
        android:layout_height="60dp"
        android:gravity="center_vertical">
        
   	    <TextView 
        	android:id="@+id/tv_sign_in"
        	android:layout_width="25dp"
        	android:layout_height="25dp"
            android:layout_centerVertical="true"
            android:layout_marginLeft="20dp"
            android:contentDescription="@null"
        	android:background="@drawable/sign_in"
        	android:textColor="#3b3b3b"/>
   	</RelativeLayout>
</RelativeLayout>

(2)签到界面布局

先创建一个标题栏:main_title_bar.xml,界面效果如下图所示:

android 签到按钮 安卓定位签到_移动开发_02


在res/layout文件夹中,创建一个布局文件 main_title_bar.xml。在该布局文件中,放

置2个TextView控件,分别用于显示后退按钮(后退按钮的样式采用背景选择器的方式)和当前界面标题,并设置标题栏背景透明

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/title_bar"
    android:layout_width="match_parent"
    android:layout_height="50dp" 
    android:background="@android:color/transparent">
    
    <TextView
        android:id="@+id/tv_back"
        android:background="@drawable/go_back_selector"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv_main_title"
        android:textColor="@android:color/white"
        android:textSize="20sp"
        android:text="@string/app_name"
        android:layout_centerInParent="true"/>
</RelativeLayout>

创建背景选择器:go_back_selector.xml,根据按钮按下和弹起的状态来切换它的背景图片,由此实现动态效果。当按钮按下时显示灰色图片(iv_back_selected.png),当按钮弹起时显示白色图片(iv_back.png),具体代码如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >  
    <item android:state_pressed="true" android:drawable="@drawable/iv_back_selected">
    </item>
    <item android:drawable="@drawable/iv_back"></item>
</selector>

/res/layout下新建activity_check.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f1f2f2"
    android:orientation="vertical" >

    <include layout="@layout/main_title_bar" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:orientation="vertical" >
        
        <TextView
            android:id="@+id/tv_time"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#ffffff"
            android:drawablePadding="10dp"
            android:gravity="center_vertical"
            android:padding="10dp"
            android:text="当前时间..." >
        </TextView>

        <TextView
            android:id="@+id/tv_location"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#ffffff"
            android:drawableLeft="@drawable/ic_local"
            android:drawablePadding="10dp"
            android:gravity="center_vertical"
            android:padding="10dp"
            android:text="正在进行定位..." >
        </TextView>

        <LinearLayout
            android:id="@+id/ll_popup"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#ffeeeeee"
            android:orientation="vertical" >

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#fff" >

                <TextView
                    android:id="@+id/popupwindow_calendar_month"
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    android:gravity="center"
                    android:textColor="#000"
                    android:textSize="18sp" />
                
                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="400dp"
                    android:gravity="center"
                    android:background="@drawable/bg_check"/>
            </RelativeLayout>
			
           

            <Button
                android:id="@+id/btn_signIn"
                android:layout_width="match_parent"
                android:layout_height="45dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="20dp"
                android:background="@drawable/btn_sign_bg_selector"
                android:text="签到"
                android:textColor="@android:color/white"
                android:textSize="16sp" />
            
            <Button
                android:id="@+id/check_history"
                android:layout_width="match_parent"
                android:layout_height="35dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="30dp"
                android:background="#3459"
                android:text="签到记录"
                android:textColor="@android:color/white"
                android:textSize="16sp" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

2、实现定位的类

哇,不会写了。加一个工具类LocationUtils.java,代码如下:

package com.example.yuyun.utils;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.LocationClientOption.LocationMode;
import com.example.yuyun.info.LocationInfo;

public class LocationUtils {

	Handler handler;
	public LocationClient mLocationClient = null;
	public BDLocationListener myListener;
	LocationInfo info;
	Context context;

	@SuppressWarnings("deprecation")
	public LocationUtils(Handler handler, Context context) {
		this.handler = handler;
		this.context = context;
		info = new LocationInfo();
		myListener = new MyLocationListener();
		// 第一步,初始化LocationClient类
		mLocationClient = new LocationClient(context); // 声明LocationClient类
		mLocationClient.registerLocationListener(myListener); // 注册监听函数
		initLocation();
	}

	// 第四步,开始定位
	public void start() {
		mLocationClient.start();
	}

	// 第五步,停止定位
	public void stop() {
		mLocationClient.stop();
	}

	// 第二步,配置定位SDK参数
	private void initLocation() {
		LocationClientOption option = new LocationClientOption();
		option.setLocationMode(LocationMode.Hight_Accuracy);
		// 可选,默认高精度,设置定位模式,高精度,低功耗,仅设备

		option.setCoorType("bd09ll");
		// 可选,默认gcj02,设置返回的定位结果坐标系

		int span = 0;
		option.setScanSpan(span);
		// 可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的

		option.setIsNeedAddress(true);
		// 可选,设置是否需要地址信息,默认不需要

		option.setOpenGps(true);
		// 可选,默认false,设置是否使用gps

		option.setLocationNotify(true);
		// 可选,默认false,设置是否当GPS有效时按照1S/1次频率输出GPS结果

		option.setIsNeedLocationDescribe(true);
		// 可选,默认false,设置是否需要位置语义化结果,可以在BDLocation.getLocationDescribe里得到,结果类似于“在北京天安门附近”

		option.setIsNeedLocationPoiList(true);
		// 可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到

		option.setIgnoreKillProcess(false);
		// 可选,默认true,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认不杀死

		option.SetIgnoreCacheException(false);
		// 可选,默认false,设置是否收集CRASH信息,默认收集

		option.setEnableSimulateGps(false);
		// 可选,默认false,设置是否需要过滤GPS仿真结果,默认需要

		mLocationClient.setLocOption(option);
	}

	// 第三步,实现BDLocationListener接口
	public class MyLocationListener implements BDLocationListener {

		@Override
		public void onReceiveLocation(BDLocation location) {
			String latitude = String.valueOf(location.getLatitude());
			String longitude = String.valueOf(location.getLongitude());
			String add = location.getAddrStr();
			String locDescribution = location.getLocationDescribe();
			info.setLatitude(String.valueOf(location.getLatitude()));
			info.setLongitude(String.valueOf(location.getLongitude()));
			info.setAdd(location.getAddrStr());
			info.setLocDescribution(location.getLocationDescribe());
			info.setCity(location.getCity());
			Log.i("location", "经度:" + longitude + "\n纬度:" + latitude + "\n位置:"
					+ location + locDescribution);
			Message msg = new Message();
			msg.what = 0;
			msg.obj = info;
			handler.sendMessage(msg);
		}
		public void onConnectHotSpotMessage(String arg0, int arg1) {
			// TODO Auto-generated method stub
			
		}
	}
}

再加一个LocationInfo.java类,具体的代码也在下面:

package com.example.yuyun.info;

public class LocationInfo {
	String latitude;// 纬度
	String longitude;// 经度
	String add;
	String locDescribution;
	String city;

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getLatitude() {
		return latitude;
	}

	public void setLatitude(String latitude) {
		this.latitude = latitude;
	}

	public String getLongitude() {
		return longitude;
	}

	public void setLongitude(String longitude) {
		this.longitude = longitude;
	}

	public String getAdd() {
		return add;
	}

	public void setAdd(String add) {
		this.add = add;
	}

	public String getLocDescribution() {
		return locDescribution;
	}

	public void setLocDescribution(String locDescribution) {
		this.locDescribution = locDescribution;
	}
}

3、签到数据库

因为本签到是有历史记录滴,所以还要写个数据库代码,不过因为这里只有签到,没有登录,所以我将用户名注释掉,大家用的时候可以将注释去掉。

(1)创建数据库bxg.db,逻辑代码如下:SQLiteHelper.java

package com.example.yuyun.sqlite;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class SQLiteHelper extends SQLiteOpenHelper {

	private static final int DB_VERSION = 1;
    public static String DB_NAME = "bxg.db";
    public static final String U_CHECKS = "checks";// 签到
    public SQLiteHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }
    
	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		
        // 创建签到记录表
        db.execSQL("CREATE TABLE  IF NOT EXISTS " + U_CHECKS + "( "
                + "_id INTEGER PRIMARY KEY AUTOINCREMENT, "
//                +" userName VARCHAR,"//用户名
                + "times VARCHAR, "// 时间
                + "location VARCHAR"// 位置
                + ")");
	}

	/**
     * 当数据库版本号增加时才会调用此方法
     */
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS " + U_CHECKS);
        onCreate(db);
	}
}

(2)数据库创建好了呢,还有写入数据和读取数据

先创建一个TimeBean

package com.example.yuyun.bean;

public class TimeBean {
//	public  String userName; //用户名
    public  String times;	//时间
    public  String location;	//地点
}

创建一个DBUtils类,用来写入数据和读取数据库数据,具体代码如下:

package com.example.yuyun.utils;

import java.util.ArrayList;
import java.util.List;

import com.example.yuyun.bean.TimeBean;
import com.example.yuyun.sqlite.SQLiteHelper;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DBUtils {

	private static DBUtils instance = null;
    private static SQLiteHelper helper;
    private static SQLiteDatabase db;
    public DBUtils(Context context) {
        helper = new SQLiteHelper(context);
        db = helper.getWritableDatabase();
    }
    public static DBUtils getInstance(Context context) {
        if (instance == null) {
            instance = new DBUtils(context);
        }
        return instance;
    }
    
    //保存签到信息
    public void saveTime(TimeBean bean) {
        ContentValues cv = new ContentValues();
//        cv.put("userName", bean.userName); //保存用户名
        cv.put("times", bean.times);
        cv.put("location", bean.location);
        db.insert(SQLiteHelper.U_CHECKS, null, cv);
    }
    
    /**
             * 获取签到记录信息
     */
    public List<TimeBean> getCheckHistory() {
        String sql = "SELECT * FROM " + SQLiteHelper.U_CHECKS;
        Cursor cursor = db.rawQuery(sql, new String[]{});
        List<TimeBean> vbl = new ArrayList<TimeBean>();
        TimeBean bean = null;
        while (cursor.moveToNext()) {
            bean = new TimeBean();
//            bean.userName=cursor.getString(cursor.getColumnIndex("userName")); //读取用户名
            bean.times=cursor.getString(cursor.getColumnIndex("times"));
            bean.location=cursor.getString(cursor
                    .getColumnIndex("location"));
            vbl.add(bean);
            bean = null;
        }
        cursor.close();
        return vbl;
    }
}

4、签到入口的逻辑代码

MainActivity.java

package com.example.yuyun;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {

	private TextView tv_sign_in;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
	}
	
	//获取界面控件
	private void initView() {
		tv_sign_in = (TextView)findViewById(R.id.tv_sign_in);
		
		//签到的点击事件
        tv_sign_in.setOnClickListener(new View.OnClickListener() {
        	@Override
            public void onClick(View v) {
        		//跳转到签到界面
               Intent intent = new Intent(MainActivity.this,CheckActivity.class); 
               startActivity(intent);
                
            }
        });
	}
}

5、签到页面逻辑代码

CheckActivity.Java

package com.example.yuyun;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.example.yuyun.bean.TimeBean;
import com.example.yuyun.info.LocationInfo;
import com.example.yuyun.utils.DBUtils;
import com.example.yuyun.utils.LocationUtils;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class CheckActivity extends Activity implements OnClickListener{

	private RelativeLayout rl_title_bar;
	private TextView mtitleTextView;
	private Button mSign,check_history;
	private TextView mLocation,ttime;
	private TextView mBlack;
	LocationUtils location;
	String address;
	String loc;
	String lon;
	String lat;
	String latitude;// 纬度
	String longitude;// 经度
	String add;
	String locDescribution;
	
	private String times,loctions;
	private DBUtils db;
	
	private String spUserName;

	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			LocationInfo info = (LocationInfo) msg.obj;
			latitude = info.getLatitude();
			longitude = info.getLongitude();
			add = info.getAdd();
			locDescribution = info.getLocDescribution();
			if (locDescribution == null || locDescribution.equals("")
					|| longitude == null || longitude.equals("")
					|| latitude == null || latitude.equals("")) {
				return;
			}
			if (add == null || add.equals("")) {
				return;
			}
			
			mLocation.setText(add + locDescribution);
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日  HH:mm:ss");// HH:mm:ss
			//获取当前时间
			Date date = new Date(System.currentTimeMillis());
			ttime.setText(simpleDateFormat.format(date));
			checklist();
			
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_check);
		
		initView();
		initData();
	}
	
	private void checklist() {
		times = ttime.getText().toString();//获取时间控件上的数据
		loctions = mLocation.getText().toString();//获取定位控件上的数据
		// 创建数据库工具类的对象
        db = DBUtils.getInstance(CheckActivity.this);
	}

	private void initView() {
		mtitleTextView = (TextView) findViewById(R.id.tv_main_title);
		mSign = (Button) findViewById(R.id.btn_signIn);
		check_history =(Button)findViewById(R.id.check_history);
		mLocation = (TextView) findViewById(R.id.tv_location);
		ttime = (TextView) findViewById(R.id.tv_time);
		mBlack = (TextView) findViewById(R.id.tv_back);
		mBlack.setOnClickListener(this);
		mSign.setOnClickListener(this);
		check_history.setOnClickListener(this);
	}

	private void initData() {
		mtitleTextView.setText("签  到");
		rl_title_bar = (RelativeLayout) findViewById(R.id.title_bar);
        rl_title_bar.setBackgroundColor(Color.parseColor("#30B4FF"));
		location = new LocationUtils(handler, CheckActivity.this);
		location.start();
	}

	private void initCheck() {
		TimeBean bean = new TimeBean();
//		bean.userName=spUserName;
		bean.times=times;
		bean.location=loctions;
		DBUtils.getInstance(this).saveTime(bean);
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.tv_back:// 返回
			this.finish();
			break;
		case R.id.btn_signIn:// 签到
			initCheck();
			Toast.makeText(CheckActivity.this,"签到成功",Toast.LENGTH_SHORT).show();
			Intent intent1 = new Intent(CheckActivity.this,CheckHistoryActivity.class);
        	CheckActivity.this.startActivityForResult(intent1,1);
			this.finish();
			break;
		case R.id.check_history://设置播放条目的点击事件
			//跳转到历史记录界面
        	Intent intent = new Intent(CheckActivity.this,CheckHistoryActivity.class);
        	CheckActivity.this.startActivityForResult(intent,1);
		}
	}
}

6、AndroidManifest.xml文件中注册

<activity android:name="com.example.yuyun.CheckActivity"></activity>

到这里就可以将签到的时间和地点保存到数据库中,如图:

android 签到按钮 安卓定位签到_xml_03