ddoh___
Dr.Rick
采纳率75%
2020-12-22 13:31

歉烦问一下,大佬们这个地理围栏怎么做啊?

这个监测位置是否在范围里的是执行之后就不执行了还是一只会监测呢 ?

 主文件

package com.example.gps3;

import android.Manifest;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Vibrator;
import android.provider.AlarmClock;
import android.provider.Settings;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

public class MainActivity extends AppCompatActivity {
    Button button1, buttonn, delete1, delete2, delete3, delete4;
    TextView tings, tingsall, tings2, tingsall2, tings3, tingsall3, tings4, tingsall4;
    public String xlocat;
    public String ylocat;
    public String zlocat;
    String xlc, ylc, zlc;
    private LocationManager lm;
    String numll = "";
    boolean bool = false;
    public double xl, yl, zl, xlint, ylint, zlint, xl2, yl2, zl2, xlint2, ylint2, zlint2, xl3, yl3, zl3, xlint3, ylint3, zlint3, xl4, yl4, zl4, xlint4, ylint4, zlint4;

    //Vibrator vb = (Vibrator)getSystemService(Service.VIBRATOR_SERVICE);
    //AlarmManager alarmClock = (AlarmManager) getSystemService(ALARM_SERVICE);

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tings = findViewById(R.id.tings);
        tings2 = findViewById(R.id.tings2);
        tings3 = findViewById(R.id.tings3);
        tings4 = findViewById(R.id.tings4);
        tingsall = findViewById(R.id.tingsall);
        tingsall2 = findViewById(R.id.tingsall2);
        tingsall3 = findViewById(R.id.tingsall3);
        tingsall4 = findViewById(R.id.tingsall4);
        Intent intent2 = new Intent(MainActivity.this, MainActivity2.class);
        float radius = 7;
        Toast.makeText(this,"温馨提示:在一些手机的系统中可能会有系统保护,比如小米的miui12,导致定位不行",Toast.LENGTH_SHORT).show();

        delete1 = findViewById(R.id.delete1);
        delete2 = findViewById(R.id.delete2);
        delete3 = findViewById(R.id.delete3);
        delete4 = findViewById(R.id.delete4);
        shared();
        shared2();
        shared3();
        shared4();
        button1 = findViewById(R.id.button1);
        buttonn = findViewById(R.id.buttonn);
        startActivity(intent2);



        Intent intent233 = new Intent(this, ProximityReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, -1, intent233, 0);
        lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

            return;
        }
        lm.addProximityAlert(xl, yl, radius,-1,pendingIntent);
        lm.addProximityAlert(xl2,yl2,radius,-1,pendingIntent);
        lm.addProximityAlert(xl3,yl3,radius,-1,pendingIntent);
        lm.addProximityAlert(xl4,yl4,radius,-1,pendingIntent);




        buttonn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                shared();
                shared2();
                shared3();
                shared4();
                bool = true;
                /**
                 Intent intent3 =getIntent();
                 String Tings = intent3.getStringExtra("tings");
                 String Tingsall = intent3.getStringExtra("tingsall");
                 xlocat = intent3.getStringExtra("xl");
                 ylocat = intent3.getStringExtra("yl");
                 zlocat = intent3.getStringExtra("zl");
                 tings.setText(Tings);
                 tingsall.setText(Tingsall);**/


            }
        });
        /**
         if( xlint>xl-0.000009405717451407729 && xlint<xl+0.000008983152841195214  ){
         Toast.makeText(MainActivity.this,"yes",Toast.LENGTH_SHORT);


         //abstract void cancel():关闭或者停止振动器
         //abstract boolean hasVibrator():判断硬件是否有振动器
         //void vibrate(long milliseconds):控制手机振动为milliseconds毫秒
         //void vibrate(long[] pattern,int repeat):指定手机以pattern指定的模式振动! 比如:pattern为new int[200,400,600,800],就是让他在200,400,600,800这个时间交替启动与关闭振动器! 而第二个则是重复次数,如果是-1的只振动一次,如果是0的话则一直振动 还有其他两个方法用得不多~ 对了,使用振动器还需要在AndroidManifest.xml中添加下述权限: <uses-permission android:name="android.permission.VIBRATE"/>
         }**/
        //-------------------------------------------
        if (!isgpsable(lm)) {
            Toast.makeText(MainActivity.this, "请你在设置里打开gps权限", Toast.LENGTH_SHORT).show();
        }
        if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        Location lc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        updateshow(lc);
        if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 1, new LocationListener() {
            @Override
            public void onLocationChanged(@NonNull Location location) {
                updateshow(location);
            }
        });
        //------------------------------------------------------------------------
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent2 = new Intent(MainActivity.this, MainActivity2.class);
                startActivity(intent2);
            }
        });
        delete1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deletesaved();

            }
        });
        delete2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deletesaved2();
            }
        });


        delete3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deletesaved3();
            }
        });


        delete4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deletesaved4();
            }
        });
    }


    public void shared() {
        SharedPreferences sharedPreferences2 = getSharedPreferences("shared.xml", MODE_PRIVATE);
        String Tings = sharedPreferences2.getString("tings", "");
        String Tingsall = sharedPreferences2.getString("tingsall", "");
        String xlocat = sharedPreferences2.getString("xlocat", "");
        String ylocat = sharedPreferences2.getString("ylocat", "");
        String zlocat = sharedPreferences2.getString("zlocat", "");
        Boolean boole = sharedPreferences2.getBoolean("bool", false);

        /**xlint = Float.parseFloat(xlocat);
         ylint = Float.parseFloat(ylocat);
         zlint = Float.parseFloat(zlocat);**/
        if (bool = false) {
            xl = Double.parseDouble(xlocat);
            yl = Double.parseDouble(ylocat);
            zl = Double.parseDouble(zlocat);

        }

        tings.setText(Tings);
        tingsall.setText(Tingsall);

    }

    public void shared2() {
        SharedPreferences sharedPreferences2 = getSharedPreferences("shared.xml", MODE_PRIVATE);
        String Tings2 = sharedPreferences2.getString("tings2", "");
        String Tingsall2 = sharedPreferences2.getString("tingsall2", "");
        String xlocat2 = sharedPreferences2.getString("xlocat2", "");
        String ylocat2 = sharedPreferences2.getString("ylocat2", "");
        String zlocat2 = sharedPreferences2.getString("zlocat2", "");
        Boolean boole2 = sharedPreferences2.getBoolean("bool2", false);
        /**xlint = Float.parseFloat(xlocat);
         ylint = Float.parseFloat(ylocat);
         zlint = Float.parseFloat(zlocat);**/
        if (bool = false) {
            xl2 =  Double.parseDouble(xlocat2);
            yl2 =  Double.parseDouble(ylocat2);
            zl2 =  Double.parseDouble(zlocat2);

        }
        tings2.setText(Tings2);
        tingsall2.setText(Tingsall2);
    }

    public void shared3() {
        SharedPreferences sharedPreferences2 = getSharedPreferences("shared.xml", MODE_PRIVATE);
        String Tings3 = sharedPreferences2.getString("tings3", "");
        String Tingsall3 = sharedPreferences2.getString("tingsall3", "");
        String xlocat3 = sharedPreferences2.getString("xlocat3", "");
        String ylocat3 = sharedPreferences2.getString("ylocat3", "");
        String zlocat3 = sharedPreferences2.getString("zlocat3", "");
        Boolean boole3 = sharedPreferences2.getBoolean("bool3", false);
        /**xlint = Float.parseFloat(xlocat);
         ylint = Float.parseFloat(ylocat);
         zlint = Float.parseFloat(zlocat);**/
        if (bool = false) {
            xl3 =  Double.parseDouble(xlocat3);
            yl3 =  Double.parseDouble(ylocat3);
            zl3 =  Double.parseDouble(zlocat3);

        }
        tings3.setText(Tings3);
        tingsall3.setText(Tingsall3);
    }

    public void shared4() {
        SharedPreferences sharedPreferences2 = getSharedPreferences("shared.xml", MODE_PRIVATE);
        String Tings4 = sharedPreferences2.getString("tings4", "");
        String Tingsall4 = sharedPreferences2.getString("tingsall4", "");
        String xlocat4 = sharedPreferences2.getString("xlocat4", "");
        String ylocat4 = sharedPreferences2.getString("ylocat4", "");
        String zlocat4 = sharedPreferences2.getString("zlocat4", "");
        Boolean boole4 = sharedPreferences2.getBoolean("bool4", false);
        /**xlint = Float.parseFloat(xlocat);
         ylint = Float.parseFloat(ylocat);
         zlint = Float.parseFloat(zlocat);**/
        if (bool = false) {
            xl4 =  Double.parseDouble(xlocat4);
            yl4 =  Double.parseDouble(ylocat4);
            zl4 =  Double.parseDouble(zlocat4);

        }
        tings4.setText(Tings4);
        tingsall4.setText(Tingsall4);
    }

    public void deletesaved() {

        SharedPreferences sharedPreferences2 = getSharedPreferences("shared.xml", MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences2.edit();
        editor.putString("tings", numll);
        editor.putString("tingsall", numll);
        editor.putString("xlocat","");
        editor.putString("ylocat","");
        editor.putString("zlocat","");
        String Tings = sharedPreferences2.getString("tings", "");
        String Tingsall = sharedPreferences2.getString("tingsall", "");

        editor.commit();
        tings.setText(Tings);
        tingsall.setText(Tingsall);

    }

    public void deletesaved2() {
        SharedPreferences sharedPreferences2 = getSharedPreferences("shared.xml", MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences2.edit();
        editor.putString("tings2", numll);
        editor.putString("tingsall2", numll);
        editor.putString("xlocat2","");
        editor.putString("ylocat2","");
        editor.putString("zlocat2","");
        String Tings2 = sharedPreferences2.getString("tings2", "");
        String Tingsall2 = sharedPreferences2.getString("tingsall2", "");
        editor.commit();
        tings2.setText(Tings2);
        tingsall2.setText(Tingsall2);
    }

    public void deletesaved3() {
        SharedPreferences sharedPreferences2 = getSharedPreferences("shared.xml", MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences2.edit();
        editor.putString("tings3", numll);
        editor.putString("tingsall3", numll);
        editor.putString("xlocat3","");
        editor.putString("ylocat3","");
        editor.putString("zlocat3","");
        String Tings3 = sharedPreferences2.getString("tings3", "");
        String Tingsall3 = sharedPreferences2.getString("tingsall3", "");
        editor.commit();
        tings3.setText(Tings3);
        tingsall3.setText(Tingsall3);
    }

    public void deletesaved4() {
        SharedPreferences sharedPreferences2 = getSharedPreferences("shared.xml", MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences2.edit();
        editor.putString("tings4", numll);
        editor.putString("tingsall4", numll);
        editor.putString("xlocat4","");
        editor.putString("ylocat4","");
        editor.putString("zlocat4","");
        String Tings4 = sharedPreferences2.getString("tings4", "");
        String Tingsall4 = sharedPreferences2.getString("tingsall4", "");
        editor.commit();
        tings4.setText(Tings4);
        tingsall4.setText(Tingsall4);
    }

    public void onStatusChanged(String s, int i, Bundle bundle) {
    }

    public void addProximityAlert() {

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }

        Intent intent233 = new Intent(this,ProximityReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this,-1,intent233,0);

        Toast.makeText(MainActivity.this,"hhhhhh",Toast.LENGTH_SHORT).show();
        lm.addProximityAlert(xl,yl,1,-1,pendingIntent );

    }


    public void onProviderEnabled(String provider) {
        if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
    }



    public void onProviderDisabled(String s) {
        updateshow(null);
    }




    private void updateshow(Location location) {

        if (location != null) {
            StringBuilder sb1 = new StringBuilder();
            StringBuilder sb2 = new StringBuilder();
            StringBuilder sb3 = new StringBuilder();
            sb1.append(location.getLongitude());
            sb2.append(location.getLatitude());
            sb3.append(location.getAltitude());
            xlc = String.valueOf(sb1);xlint = Double.parseDouble(xlc);
            ylc = String.valueOf(sb2);ylint = Double.parseDouble(ylc);
            zlc = String.valueOf(sb3);zlint = Double.parseDouble(zlc);

        }

    }



    private boolean isgpsable(LocationManager lm) {
        return lm.isProviderEnabled(LocationManager.GPS_PROVIDER) ? true : false;
    }





    private void opengps() {
        Intent intent = new Intent(Settings.ACTION_LOCALE_SETTINGS);
        startActivityForResult(intent, 0);
    }


}

2323

proximity receiver文件

package com.example.gps3;

import android.app.AlarmManager;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.os.Vibrator;
import android.widget.Toast;

import static android.content.Context.ALARM_SERVICE;

public class ProximityReceiver extends BroadcastReceiver {
        private Vibrator vb;
    public void onReceive(Context context, Intent intent) {
        AlarmManager alarmClock = (AlarmManager) context.getSystemService(ALARM_SERVICE);
        boolean isEnter = intent.getBooleanExtra( LocationManager.KEY_PROXIMITY_ENTERING, false);
        if(isEnter) {
            vb = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
            vb.cancel();
            vb.vibrate(new long[]{100,200,100,200},0);

            Toast.makeText(context, "你有在这里的事情", Toast.LENGTH_LONG).show();


        }
        else {


            Toast.makeText(context, "您已离开确定地,确保有三米的空间以及空旷的空间", Toast.LENGTH_LONG).show();



        }
    }
}

有的时候那个proximity receiver文件里的语句会开始进入的时候就显示

求大佬解答,不受恩感激!!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • qq_40062618 qq_1113502097 4月前

    不封装看着头疼,java代码是顺序执行,只执行一次。如果想要一直监听,解决方法有两种,一种是观察者模式,当被观察者发生变化时候,通知观察者,我数据发生了变化。另外一种就是,用定时器,定时去检查数据。

    点赞 1 评论 复制链接分享
  • qq_40062618 qq_1113502097 4月前

    一开始就会提示,你看下你是不是在程序执行的开始就发送了一次广播

    点赞 1 评论 复制链接分享

相关推荐