本人为高中学生一名,希望求大佬来解释,感激涕零!!!
MainActivity
package com.example.gps3;
import android.Manifest;
import android.app.Activity;
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, bool1, bool2, bool3, bool4 = 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;
float radius = 7;//半径多调试,猜疑问题在这里
//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);
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//--------------初始化----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Intent intent2 = new Intent(MainActivity.this, MainActivity2.class);
Toast.makeText(this, "温馨提示:在一些手机的系统中可能会有系统保护,比如小米的miui12,导致定位不行", Toast.LENGTH_SHORT).show();
//-------------startActivity(intent2);初始化--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
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);
//-----------在这里获取打包的信息,并与范围做比较-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Intent intent233 = new Intent(MainActivity.this, ProximityReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, -1, intent233, 0);
lm.removeProximityAlert(pendingIntent);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent2 = new Intent(MainActivity.this, MainActivity2.class);
startActivity(intent2);
}
});
buttonn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
shared();
shared2();
shared3();
shared4();
bool = true;
}
});
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
/**
//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, 3, new LocationListener() {
@Override
public void onLocationChanged(@NonNull Location location) {
updateshow(location);
Intent intent233 = new Intent(MainActivity.this, ProximityReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, -1, intent233, 0);
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;
}
//Toast.makeText(MainActivity.this, (int) xl,Toast.LENGTH_LONG).show();
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if (bool1 = true) {
lm.addProximityAlert(xl, yl, radius, -1, pendingIntent);
}
if (bool2 = true) {
lm.addProximityAlert(xl2, yl2, radius, -1, pendingIntent);
}
if (bool3 = true) {
lm.addProximityAlert(xl3, yl3, radius, -1, pendingIntent);
}
if (bool4 = true) {
lm.addProximityAlert(xl4, yl4, radius, -1, pendingIntent);
//上面这个要加到if里面实时监测,因为为一次性函数
}
}
});
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
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 boole1 = sharedPreferences2.getBoolean("bool1", 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);
bool1 = boole1;
}
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);
bool2=boole2;
}
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);
bool3=boole3;
}
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);
bool4 = boole4;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public void deletesaved() {
Intent intent233 = new Intent(MainActivity.this, ProximityReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, -1, intent233, 0);
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", "");
editor.putBoolean("boole1",false);
String Tings = sharedPreferences2.getString("tings", "");
String Tingsall = sharedPreferences2.getString("tingsall", "");
editor.commit();
tings.setText(Tings);
tingsall.setText(Tingsall);
bool1 = false;
lm.removeProximityAlert(pendingIntent);
}
public void deletesaved2() {
Intent intent233 = new Intent(MainActivity.this, ProximityReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, -1, intent233, 0);
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", "");
editor.putBoolean("boole2",false);
String Tings2 = sharedPreferences2.getString("tings2", "");
String Tingsall2 = sharedPreferences2.getString("tingsall2", "");
editor.commit();
tings2.setText(Tings2);
tingsall2.setText(Tingsall2);
bool2 = false;
lm.removeProximityAlert(pendingIntent);
}
public void deletesaved3() {
Intent intent233 = new Intent(MainActivity.this, ProximityReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, -1, intent233, 0);
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", "");
editor.putBoolean("boole3",false);
String Tings3 = sharedPreferences2.getString("tings3", "");
String Tingsall3 = sharedPreferences2.getString("tingsall3", "");
editor.commit();
tings3.setText(Tings3);
tingsall3.setText(Tingsall3);
bool3 = false;
lm.removeProximityAlert(pendingIntent);
}
public void deletesaved4() {
Intent intent233 = new Intent(MainActivity.this, ProximityReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, -1, intent233, 0);
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", "");
editor.putBoolean("boole4",false);
String Tings4 = sharedPreferences2.getString("tings4", "");
String Tingsall4 = sharedPreferences2.getString("tingsall4", "");
editor.commit();
tings4.setText(Tings4);
tingsall4.setText(Tingsall4);
bool4 = false;
lm.removeProximityAlert(pendingIntent);
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
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, 10, -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);
}
}
MainActivity2
package com.example.gps3;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import java.util.ArrayList;
import java.util.List;
//现在要解决的问题:1.生成textview,2.跨页面传输数据(sharedprefences不行闪退),3.位置监测,4.闹铃————大概有这么多要做
public class MainActivity2 extends AppCompatActivity {
Button button2, button3;
TextView thingsall, things;
TextView xlocation, ylocation, zlocation,text;
private LocationManager lm;
String THINGS,THINGSALL,XLOCATION,YLOCATION,ZLOCATION;
Switch sw1,sw2,sw3,sw4;
public int a=0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
button2 = findViewById(R.id.button2);
button3 = findViewById(R.id.button3);
things = findViewById(R.id.things);
thingsall = findViewById(R.id.thingsall);
text=findViewById(R.id.text);
xlocation = findViewById(R.id.xlocation);
ylocation = findViewById(R.id.ylocation);
zlocation = findViewById(R.id.zlocation);
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
sw1= findViewById(R.id.switch1);
sw2= findViewById(R.id.switch2);
sw3= findViewById(R.id.switch3);
sw4= findViewById(R.id.switch4);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
/**
Intent intent = new Intent(MainActivity2.this, MainActivity.class);
Bundle bundle = new Bundle();
intent.setClass(MainActivity2.this,MainActivity.class);
//bundle.putString("id","值");
intent.putExtra("xl", String.valueOf(xlocation.getText()));
intent.putExtra("yl", String.valueOf(ylocation.getText()));
intent.putExtra("zl", String.valueOf(zlocation.getText()));
intent.putExtra("tings", String.valueOf(things.getText()));
intent.putExtra("tingsall", String.valueOf(thingsall.getText()));
//删除第二个页面的跳转按钮,因为可以靠退出键退出,同时可以保存数据
startActivity(intent);**/
XLOCATION= (String) xlocation.getText();
if (a==1){
shared();
} else if (a==2) {
shared2();
}else if(a==3){
shared3();
}else if(a==4){
shared4();
}
}
});
sw1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if(isChecked==true){
a=1;}
}
});
sw2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if(isChecked==true) {a=2;}
}
});
sw3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if(isChecked==true){a=3;}
}
});
sw4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if(isChecked==true) {a=4;}
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!isgpsable(lm)) {
Toast.makeText(MainActivity2.this, "请你在设置里打开gps权限", Toast.LENGTH_SHORT).show();
}
if (ActivityCompat.checkSelfPermission(MainActivity2.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MainActivity2.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
Location lc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
updateshow(lc);
if (ActivityCompat.checkSelfPermission(MainActivity2.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MainActivity2.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10, 3, new LocationListener() {
@Override
public void onLocationChanged(@NonNull Location location) {
updateshow(location);
}
});
}
});
//--------------------------------------------------------------
//---------------------------------------------------------------
}
//-----------------------------------------------------------------------------
public void shared (){
SharedPreferences sharedPreferences = getSharedPreferences("shared.xml",MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("tings",things.getText().toString());
editor.putString("tingsall",thingsall.getText().toString());
editor.putString("xlocat",xlocation.getText().toString());
editor.putString("ylocat",ylocation.getText().toString());
editor.putString("zlocat",zlocation.getText().toString());
editor.putBoolean("bool1",true);
editor.commit();
}
public void shared2 (){
SharedPreferences sharedPreferences = getSharedPreferences("shared.xml",MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("tings2",things.getText().toString());
editor.putString("tingsall2",thingsall.getText().toString());
editor.putString("xlocat2",xlocation.getText().toString());
editor.putString("ylocat2",ylocation.getText().toString());
editor.putString("zlocat2",zlocation.getText().toString());
editor.putBoolean("bool2",true);
editor.commit();
}
public void shared3 (){
SharedPreferences sharedPreferences = getSharedPreferences("shared.xml",MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("tings3",things.getText().toString());
editor.putString("tingsall3",thingsall.getText().toString());
editor.putString("xlocat3",xlocation.getText().toString());
editor.putString("ylocat3",ylocation.getText().toString());
editor.putString("zlocat3",zlocation.getText().toString());
editor.putBoolean("bool3",true);
editor.commit();
}
public void shared4 (){
SharedPreferences sharedPreferences = getSharedPreferences("shared.xml",MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("tings4",things.getText().toString());
editor.putString("tingsall4",thingsall.getText().toString());
editor.putString("xlocat4",xlocation.getText().toString());
editor.putString("ylocat4",ylocation.getText().toString());
editor.putString("zlocat4",zlocation.getText().toString());
editor.putBoolean("bool4",true);
editor.commit();
}
//---------------------------------------------------------------------------------
public void onStatusChanged(String s, int i, Bundle bundle) {
}
public void onProviderEnabled(String provider) {
if (ActivityCompat.checkSelfPermission(MainActivity2.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MainActivity2.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());
xlocation.setText(sb1.toString());
ylocation.setText(sb2.toString());
zlocation.setText(sb3.toString());
}else {
}
}
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);
}
}
ProximityRecever
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},4);
vb.cancel();
Toast.makeText(context, "你有在这里的事情", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(context, "您已离开确定地,确保有三米的空间以及空旷的空间", Toast.LENGTH_LONG).show();
}
}
}