zoukankan      html  css  js  c++  java
  • 每日健康打卡

    主要点:

    1、定位功能的实现,GPS和网络定位两种方式

    2、广告植入的实现 Intent类

    3、Timer TimerTask两个类

    4、隐藏标题栏

    5、okhttp包的使用

    6、json数据的传输格式

    广告界面的实现(Intent类 和 Timer TimerTask 类的实现):

    import android.content.Intent;
    import android.support.annotation.IdRes;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    import java.util.Random;
    import java.util.Timer;
    import java.util.TimerTask;
    
    public class ad extends AppCompatActivity {
    
        int imgs[] = {R.drawable.c1,R.drawable.c2};
        int i = 1;
        TextView tv;
        ImageView iv;
        Timer t = new Timer();
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_ad);
            Random r = new Random();
            iv = (ImageView) findViewById(R.id.image1);
            iv.setImageResource(imgs[r.nextInt(2)]);
            tv = (TextView)findViewById(R.id.text1);
            TimerTask tk = new TimerTask() {
                @Override
                public void run() {
                    i = i -1;
                    tv.setText("还剩" + i + "秒");
                    if(i == 0){
                        t.cancel();
                        Intent intent = new Intent(ad.this,HelathActivity.class);
                        startActivity(intent);
                    }
                }
            };
            t.schedule(tk,10,1000);
        }
    }
    
    

    登陆/注册界面的实现( okhttp,线程的实现 Json传输数据):

    package com.example.myapplication;
    
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import java.io.IOException;
    
    import okhttp3.MediaType;
    import okhttp3.OkHttpClient;
    import okhttp3.Request;
    import okhttp3.RequestBody;
    import okhttp3.Response;
    
    public class LoginActivity extends AppCompatActivity {
    
        private OkHttpClient client = new OkHttpClient();
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_register);
            Button btn01 = findViewById(R.id.button2);
    
            btn01.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    new Thread(new Runnable() {
                        public void run() {
                            EditText username = findViewById(R.id.username);
                            EditText password = findViewById(R.id.password);
                            String myJson = "{"username":"" + username.getText().toString() +  "","userpassword":"" + password.getText().toString() + ""}";
                            try {
                                String res = postJson("http://192.168.1.105/login.php",myJson);
                                Log.d("myRes",myJson);
                            } catch (IOException e) {
                                Log.d("myRes","有IO异常捕获!");
                            }
                        }
                    }).start();
                }
            });
    
        }
    
        // http方法
        private String postJson(String url, String myJson) throws IOException {
            MediaType JSON = MediaType.get("application/json; charset=utf-8"); //创建Content-Type类型为json
            RequestBody body = RequestBody.create(myJson, JSON); // 创建http请求报文 其中包含了请求体body和请求头JSON
            Request request = new Request.Builder().url(url).post(body).build(); //封装完整的http请求报文
            try{
                Response response = client.newCall(request).execute(); //最后通过client 发送request请求
                return response.body().string(); //返回相应包的内容
            }
            catch(Exception ex){
                return null;
            }
        }
    }
    

    定位健康打卡的界面实现( 动态申请权限 GPS/Internet定位 json数据的传输格式):

    package com.example.myapplication;
    
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.core.app.ActivityCompat;
    import android.Manifest;
    import android.content.Context;
    import android.content.pm.PackageManager;
    import android.location.Address;
    import android.location.Geocoder;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.CheckBox;
    import android.widget.EditText;
    import android.widget.RadioButton;
    import android.widget.RadioGroup;
    import android.widget.TextView;
    
    import org.jetbrains.annotations.NotNull;
    import org.jetbrains.annotations.Nullable;
    
    import java.io.IOException;
    import java.util.List;
    import java.util.Locale;
    
    import okhttp3.MediaType;
    import okhttp3.OkHttpClient;
    import okhttp3.Request;
    import okhttp3.RequestBody;
    import okhttp3.Response;
    import okio.BufferedSink;
    
    public class MainActivity extends AppCompatActivity {
    
        final int LOCATION_CODE=1;
        TextView tv1,tv2;
        Button tijiao;
        private double latitude = 0.0;
        private double longitude = 0.0;
        RadioButton radio1,radio2,radio3;
        TextView et1;
        CheckBox cb1,cb2,cb3,cb4,cb5;
    
    
    
        @Override
        protected void onCreate(final Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            radio1 = findViewById(R.id.radio1);
            radio2 = findViewById(R.id.radio2);
            radio3 = findViewById(R.id.radio3);
    
            tijiao = findViewById(R.id.btn_tijiao);
    
            et1 = findViewById(R.id.country);
            cb1 = findViewById(R.id.check1);
            cb2 = findViewById(R.id.check2);
            cb3 = findViewById(R.id.check3);
            cb4 = findViewById(R.id.check4);
            cb5 = findViewById(R.id.check5);
    
    
            tijiao.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String myJson = "{"";
                    if(radio1.isChecked()){
                        myJson += "UserStatus":"" + radio1.getText().toString() + "","";
                    }else if(radio2.isChecked()){
                        myJson += "UserStatus":"" + radio2.getText().toString() + "","";
                    }else{
                        myJson += "UserStatus":"" + radio3.getText().toString() + "","";
                    }
    
                    myJson += "county":"" + et1.getText().toString() + "","";
    
                    if(cb1.isChecked()){
                        myJson += "zhengzhuang":"" + cb1.getText().toString() + ""}";
                    }else if(cb2.isChecked()){
                        myJson += "zhengzhuang":"" + cb2.getText().toString() + ""}";
                    }else if(cb3.isChecked()){
                        myJson += "zhengzhuang":"" + cb3.getText().toString() + ""}";
                    }else if(cb4.isChecked()){
                        myJson += "zhengzhuang":"" + cb4.getText().toString() + ""}";
                    }else{
                        myJson += "zhengzhuang":"" + cb5.getText().toString() + ""}";
                    }
    
                    //Log.d("myJson",myJson);
    
                    MediaType Json = MediaType.get("application/json charset=utf-8");
                    OkHttpClient okHttpClient = new OkHttpClient();
    
                    RequestBody requestBody = RequestBody.create(myJson,Json);
                    Request request = new Request.Builder().url("http://192.168.1.135/go.php").post(requestBody).build();
                    try {
                        Response response = okHttpClient.newCall(request).execute();
                        Log.d("resp",response.body().toString());
                    }catch(Exception ex) {
                        return;
                    }
                }
    
            });
    
            Location location;
            tv1=findViewById(R.id.country);
            tv2=findViewById(R.id.address);
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                    && ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
            {
                ActivityCompat.requestPermissions(
                        this,
                        new String[]{
                                Manifest.permission.ACCESS_FINE_LOCATION,
                                Manifest.permission.ACCESS_COARSE_LOCATION
                        },
                        LOCATION_CODE
                );
            }
    
            // GPS实现定位的方法
            LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                try {
                    location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                }catch(Exception ex){
                    return;
                }
    
                if(location!=null){
                    Address address = getLocationAddress(location);
                    tv1.setText(address.getAddressLine(0));
                    tv2.setText(address.getAdminArea()+"-"+address.getLocality()+"-"+address.getSubLocality());
                }else{
                    tv1.setText("地址获取失败,请检查GPS定位是否开启!");
                    tv2.setText("地址获取失败,请检查GPS定位是否开启!");
                }
                /*
                final Address address =getLocationAddress(location);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        currentLocation.setText(address.getAddressLine(0));
                        currentDistrict.setText(address.getAdminArea()+"-"+address.getLocality()+"-"+address.getSubLocality());
                    }
                });
                */
            }
            else{
                //网络定位实现的方法
                LocationListener locationListener =new LocationListener() {
                    @Override
                    public void onStatusChanged(String provider, int status, Bundle extras) {
    
                    }
    
                    @Override
                    public void onProviderEnabled(String provider) {
    
                    }
    
                    @Override
                    public void onProviderDisabled(String provider) {
    
                    }
                    @Override
                    public void onLocationChanged(Location location) {
                        if(location!=null){
                            Log.e("NBCCMap","Location changed : Lat:"+location.getLatitude()+"Lng: " + location.getLongitude());
                        }
                    }
                };
    
    
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,1000,0,locationListener);
                location =  locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                if(location != null){
                    latitude = location.getLatitude();
                    longitude = location.getLongitude();
                    Log.d("getLocationAddress2","getLocationAddress2");
                    Address address=getLocationAddress(location);
                    tv1.setText(getLocationAddress(location).getAddressLine(0));
                    tv2.setText(address.getAdminArea()+"-"+ address.getLocality() + "-" +address.getSubLocality());
                }else{
                    tv1.setText("地址获取失败,请检查网络是否可用!");
                    tv2.setText("地址获取失败,请检查网络是否可用!");
                }
            }
        }
    
        private Address getLocationAddress(Location location) {
            Geocoder geocoder=new Geocoder(getBaseContext(), Locale.CHINESE);
            try {
                List<Address> addresses= geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
                return addresses.get(0);
            }
            catch (Exception ex){
                return  null;
            }
        }
    }
    
  • 相关阅读:
    hibernate理解
    struts理解
    网上书城项目
    编码过程中遇到的问题
    JS回调函数
    requirejs 一个拆分js项目的类库
    jq插件开发总结
    转载-- 魔兽哈希算法封装和测试
    转载--C# PLINQ 内存列表查询优化历程
    Oracle删除死锁进程的方法
  • 原文地址:https://www.cnblogs.com/zpchcbd/p/12599019.html
Copyright © 2011-2022 走看看