实例介绍
【实例简介】
【实例截图】
【核心代码】
package com.markypq.gpshook;
import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
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.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Random;
import de.robv.android.xposed.XSharedPreferences;
import de.robv.android.xposed.XposedBridge;
public class MainActivity extends AppCompatActivity {
TextView tv;
EditText lan, lon,acc;
CheckBox enableHook;
TestLocationListener mlistener = new TestLocationListener();
LocationManager locationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.show);
lan = (EditText) findViewById(R.id.lan);
lon = (EditText) findViewById(R.id.lon);
acc= (EditText) findViewById(R.id.acc);
enableHook = (CheckBox) findViewById(R.id.enableHook);
initView();
locationManager = (LocationManager) MainActivity.this.getSystemService(Context.LOCATION_SERVICE);
getLocation(null);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode==0x01&&resultCode==Activity.RESULT_OK){
SharedPreferences sp = getSharedPreferences("markypq", MODE_WORLD_READABLE);
SharedPreferences.Editor e = sp.edit();
e.putString("lan", data.getDoubleExtra("lan",0) "");
e.putString("lon", data.getDoubleExtra("lon",0) "");
e.commit();
initView();
}
super.onActivityResult(requestCode, resultCode, data);
}
private void initView() {
SharedPreferences sp = getSharedPreferences("markypq", MODE_WORLD_READABLE);
lan.setText(sp.getString("lan", ""));
lon.setText(sp.getString("lon", ""));
acc.setText(sp.getString("acc",""));
enableHook.setChecked(sp.getBoolean("enableHook",true));
}
@TargetApi(23)
public void getLocation(View view) {
if (Build.VERSION.SDK_INT >= 23)
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// Activity#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 Activity#requestPermissions for more details.
return;
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 200, 1, mlistener);
String bestProvider = locationManager.getBestProvider(getCriteria(), true);
Location location = locationManager.getLastKnownLocation(bestProvider);
if (location != null)
tv.setText(location.getLongitude() "|" location.getLatitude());
/* try {
Field localField = Class.forName("android.os.ServiceManager")
.getDeclaredField("sCache");
localField.setAccessible(true);
HashMap<String, IBinder> map = (HashMap<String, IBinder>) localField.get(null);
for (String s:map.keySet()){
Log.d("local",s map.get(s).toString());
}
}catch (Exception e){
}*/
}
public void save(View view) {
SharedPreferences sp = getSharedPreferences("markypq", MODE_WORLD_READABLE);
SharedPreferences.Editor e = sp.edit();
e.putString("lan", lan.getText().toString());
e.putString("lon", lon.getText().toString());
e.putString("acc",acc.getText().toString());
e.putBoolean("enableHook",enableHook.isChecked());
e.commit();
Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
// getLocation(null);
}
public void change(View view) {
if (Build.VERSION.SDK_INT >= 23)
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// Activity#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 Activity#requestPermissions for more details.
return;
}
locationManager.removeUpdates(mlistener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 200,1, mlistener);
}
public void map(View view) {
Intent intent =new Intent(MainActivity.this,MapActivity.class);
SharedPreferences sp = getSharedPreferences("markypq", MODE_WORLD_READABLE);
double latitude = Double.valueOf(sp.getString("lan","117.536246"));
double longtitude = Double.valueOf(sp.getString("lon","36.681752"));
intent.putExtra("lan",latitude);
intent.putExtra("lon",longtitude);
startActivityForResult(intent,0x01 );
}
private class TestLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location){
//当位置发生改变时调用
tv.setText("经度: " location.getLatitude() " ,纬度: " location.getLongitude());
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderDisabled(String provider){
//当适配器禁用时调用
}
@Override
public void onProviderEnabled(String provider){
//当适配器有效时调用
}
public void onStatusChanged(String provider){
//当状态改变时调用
}
}
/* * 返回查询条件
*
* @return
*/
private Criteria getCriteria() {
Criteria criteria = new Criteria();
// 设置定位精确度 Criteria.ACCURACY_COARSE比较粗略,Criteria.ACCURACY_FINE则比较精细
criteria.setAccuracy(Criteria.ACCURACY_FINE);
// 设置是否要求速度
criteria.setSpeedRequired(false);
// 设置是否允许运营商收费
criteria.setCostAllowed(false);
// 设置是否需要方位信息
criteria.setBearingRequired(false);
// 设置是否需要海拔信息
criteria.setAltitudeRequired(false);
// 设置对电源的需求
criteria.setPowerRequirement(Criteria.POWER_LOW);
return criteria;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main,menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.setting:
case R.id.about:
try {
String url="mqqwpa://im/chat?chat_type=wpa&uin=2039609991";
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
}catch (Exception e){
ClipboardManager myClipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);;
myClipboard.setPrimaryClip(ClipData.newPlainText("text", "2039609991"));
Toast.makeText(MainActivity.this,"作者QQ已经复制",Toast.LENGTH_SHORT).show();
}
break;
case R.id.donate:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("HTTPS://QR.ALIPAY.COM/FKX04652BHHIVRVZIABFB1".toLowerCase())));
break;
}
return super.onOptionsItemSelected(item);
}
}
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论