Android——WebView地图回调刷新位置+接口回调

Android——WebView地图回调刷新位置+接口回调



<span style="font-size:14px;">package com.example.dell.jreduch07.util;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.util.Log;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

/**
 * Created by 冲天之峰 on 2016/9/7.
 */
public class LocationInfo {

    private Context context;
    private LocationManager lm;
private  Location location2;


 private  IfloadWebView ifloadWebView;
    public LocationInfo(Context context) {
        this.context = context;
        ifloadWebView= (IfloadWebView) context;


    }

    public Location getLocation() {
        lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);//定位精度
        criteria.setAltitudeRequired(false);//是否需要海拔信息;
        criteria.setPowerRequirement(Criteria.POWER_LOW);  //功耗

        String provider = lm.getBestProvider(criteria, true);  //最好功能提供给服务者
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return null;
        }
        lm.requestLocationUpdates(provider, 4000 * 10,0f, locationListener);//米
       Location location= lm.getLastKnownLocation(provider);
        return  location;

    }
    private  final LocationListener locationListener=new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            if (location!=null){
                Log.d("=======纬度",""+location.getLatitude());
                Log.d("=======经度",""+location.getLongitude());
//              location2=location;
//                if (location2!=location) {
                    ifloadWebView.loadLocation(location);
//                }
            }else {

                Log.d("=======纬度","定位失败");
            }
getAddress(location);
        }

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

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }
    };




    public List<Address> getAddress(Location location){
        List<Address> result=null;
        if (location!=null){
            Geocoder gc=new Geocoder(context, Locale.getDefault());
            try {
              result=  gc.getFromLocation(location.getLatitude()
                        ,location.getLongitude(),10);
                for (Address s:result){
                    Log.d("=======++++中国",s.getCountryName());
                 //  Log.d("=======++++",s.getFeatureName());
                   Log.d("=======++++山东省",s.getAdminArea());
                    Log.d("=======++++烟台市",s.getLocality());
                   // Log.d("=======++++",s.getSubAdminArea());
                 //   Log.d("=======++++",s.getPremises());
                    Log.d("=======++++","================");
                }

            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return  result;
    }

    public interface  IfloadWebView{
        public  void  loadLocation(Location location);


    }



}</span><span style="font-size:24px;">
</span>
package com.example.dell.jreduch07;

import android.location.Location;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.example.dell.jreduch07.util.LocationInfo;

public class WebViewActivity extends AppCompatActivity implements LocationInfo.IfloadWebView{
    private WebView wv1;
    private ProgressBar pb;
    private LocationInfo locationInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);
        pb=(ProgressBar)findViewById(R.id.pb);
        wv1=(WebView)findViewById(R.id.wv1);
        WebSettings ws=wv1.getSettings();
        //缩放控件
        ws.setDisplayZoomControls(true);
        ws.setSupportZoom(true);
        //支持缩放
        ws.setJavaScriptEnabled(true);
        wv1.setWebViewClient(new WebViewClient());//否则在手机自带浏览器打开
      //  wv1.setWebChromeClient(new WebChromeClient());
        wv1.setWebChromeClient(new MyWebChromeClient());
       // wv1.loadUrl("http://www.baidu.com");
       // wv1.loadUrl("http://m.amap.com/?q=31.234527,121.287689");
       // wv1.loadUrl("http://m.amap.com/?q=37.466975,121.436982");

       locationInfo=new LocationInfo(this);
      Location location= locationInfo.getLocation();
//        if (location!=null){
//            wv1.loadUrl("http://m.amap.com/?q="
//                    +""+location.getLatitude()+","+location.getLongitude());
//
//        }else {
//            wv1.loadUrl("http://m.amap.com/?q=31.234527,121.287689");
//        }

    }

    @Override
    public void loadLocation(Location location) {
        wv1.loadUrl("http://m.amap.com/?q="
                +""+location.getLatitude()+","+location.getLongitude());

    }

    public  class MyWebChromeClient extends WebChromeClient{

        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            super.onProgressChanged(view, newProgress);

            Log.d("=====newProgress:",""+newProgress);
            if (newProgress==100){
                pb.setVisibility(View.GONE);

            }
        }
    }


}

<?xml version="1.0" encoding="utf-8"?>
<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"

    tools:context="com.example.dell.jreduch07.WebViewActivity">

    <WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/wv1"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"></WebView>

    <ProgressBar
        android:id="@+id/pb"
        android:layout_width="100dp"
        android:layout_height="100dp"
        style="@android:style/Widget.ProgressBar.Large"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>




Android——接口+回调

<span style="font-size:24px;"><strong>//1先写接口
//2实现接口
//3另一个里面调用接口对象
//4关联</strong></span>



package com.example.dell.jreduch07;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class OneHDActivity extends AppCompatActivity implements TwoHDActivity.CallBack {
private TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_one_hd);
        tv=(TextView)findViewById(R.id.tv);
        TwoHDActivity.setCallBack(this);

        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getBaseContext()
                        ,TwoHDActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public void changeText(String resultStr) {
        tv.setText(resultStr);
    }
}
//1先写接口
//2实现接口
//3另一个里面调用接口对象
//4关联
package com.example.dell.jreduch07;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class TwoHDActivity extends AppCompatActivity {
private Button bt;
    private static CallBack callBack;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_two_hd);
        bt=(Button)findViewById(R.id.bt);

        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.changeText("回调成功。放学啦!");
                finish();
            }
        });
    }
    public static  void  setCallBack(Context context){
     callBack= (CallBack) context;

    }


    public interface  CallBack{
        public  void changeText(String resultStr);
    }

}






  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值