package GohnStudio.Demo;
import java.io.IOException;
import GohnStudio.Lib.PhoneService;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class DemoActivity extends Activity {
/** Called when the activity is first created. */
//NO 1
Button button;
Button buttonStart;
Button buttonStop;
Button buttonCancel;
TextView textview;
LocationManager manager;
Location location;
//NO 2
LocationManager _locationManager;
String _bestProvider;
Location _location;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textview=(TextView)findViewById(R.id.textview);
button=(Button)findViewById(R.id.button);
buttonStart=(Button)findViewById(R.id.button1);
buttonStop=(Button)findViewById(R.id.button2);
buttonCancel=(Button)findViewById(R.id.button3);
manager=(LocationManager)getSystemService(LOCATION_SERVICE);
//从GPS_PROVIDER获取最近的定位信息
location=manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
updateView(location);
//判断GPS是否可用
if(!manager.isProviderEnabled(LocationManager.GPS_PROVIDER))
{
Toast.makeText(getApplicationContext(), "请开启GPS。", Toast.LENGTH_SHORT).show();
}
buttonStart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("Test", "onClick: starting service");
startService(new Intent(DemoActivity.this,PhoneService.class));
}
});
buttonStop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("Test", "onClick: stopping service");
stopService(new Intent(DemoActivity.this,PhoneService.class));
}
});
buttonCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("Studio", "onClick: 退出");
stopService(new Intent(DemoActivity.this, PhoneService.class));
cancelNotification();
//退出
ExitSystem();
}
});
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//设置每60秒,每移动十米向LocationProvider获取一次GPS的定位信息
//当LocationProvider可用,不可用或定位信息改变时,调用updateView,更新显示
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 1, new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
//
updateView(manager.getLastKnownLocation(provider));
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
updateView(null);
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
//location为变化完的新位置,更新显示
updateView(location);
}
});
}
});
_locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setBearingRequired(false);
_bestProvider = _locationManager.getBestProvider(criteria, false);
_location = _locationManager.getLastKnownLocation(_bestProvider);
}
//不退出
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if(false)
{
ExitSystem();
}else
{
//Icon并且提醒内存常驻
finish();
Toast.makeText(getApplicationContext(), "您可以在任务栏看到我。", Toast.LENGTH_SHORT).show();
//setNotification
setNotification();
}
return true;
}else{
return super.onKeyDown(keyCode, event);
}
}
@Override
protected void onResume() {
_locationManager.requestLocationUpdates(_bestProvider, 2000, 1, new PhoneLocationListener());
super.onResume();
};
@Override
protected void onPause() {
_locationManager.removeUpdates(new PhoneLocationListener());
super.onPause();
};
//ExitSystem
public void ExitSystem()
{
//结束所有并退出
finish();
System.runFinalizersOnExit(true);
System.exit(0);
}
//更新显示内容的方法
public void updateView(Location location)
{
StringBuffer buffer=new StringBuffer();
if(location==null)
{
textview.setText("未获得服务");
return;
}
buffer.append("经度:"+location.getLongitude()+"\n");
buffer.append("纬度:"+location.getLatitude()+"\n");
buffer.append("高度:"+location.getAltitude()+"\n");
buffer.append("速度:"+location.getSpeed()+"\n");
buffer.append("方向:"+location.getBearing()+"\n");
textview.setText(buffer.toString());
}
// 添加常驻通知
private void setNotification() {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon,
getString(R.string.app_name), System.currentTimeMillis());
Intent intent = new Intent(this,DemoActivity.class);
notification.flags = Notification.FLAG_ONGOING_EVENT; // 设置常驻 Flag
PendingIntent contextIntent = PendingIntent.getActivity(this, 0,
intent, 0);
notification.setLatestEventInfo(getApplicationContext(),
getString(R.string.app_name), getString(R.string.information),
contextIntent);
notificationManager.notify(R.string.app_name, notification);
}
// 取消通知
private void cancelNotification() {
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancel(R.string.app_name);
}
//内部类
public class PhoneLocationListener implements LocationListener{
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
updateView(location);
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Demo.rar_DEMO_android 监听
共44个文件
class:21个
java:7个
png:5个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 146 浏览量
2022-09-20
20:26:06
上传
评论
收藏 715KB RAR 举报
温馨提示
AndRoid 实现定位以及监听电话录音的Demo
资源推荐
资源详情
资源评论
收起资源包目录
Demo.rar (44个子文件)
Demo
bin
GohnStudio
Lib
PhoneService$PhoneListener.class 3KB
WebServerHelper$trace.class 3KB
PhoneService.class 1010B
BroadcastReceiver.class 814B
WebServerHelper.class 1KB
PublicHelper.class 291B
Demo
DemoActivity$4.class 1KB
LoadingActivity.class 3KB
DemoActivity$2.class 1KB
R$layout.class 416B
R$id.class 536B
R$drawable.class 455B
DemoActivity$PhoneLocationListener.class 1KB
R$attr.class 331B
R.class 510B
DemoActivity$1.class 1KB
LoadingActivity$LoadingThread.class 2KB
DemoActivity$4$1.class 2KB
DemoActivity$3.class 1KB
R$string.class 453B
DemoActivity.class 6KB
classes.dex 18KB
resources.ap_ 221KB
Demo.apk 232KB
res
drawable-ldpi
icon.png 2KB
drawable-hdpi
studio.png 21KB
gohnstudio.png 176KB
icon.png 32KB
values
strings.xml 200B
drawable-mdpi
icon.png 3KB
layout
loading.xml 291B
main.xml 1KB
assets
default.properties 362B
gen
GohnStudio
Demo
R.java 1KB
proguard.cfg 1KB
src
GohnStudio
Lib
BroadcastReceiver.java 352B
WebServerHelper.java 5KB
PhoneService.java 3KB
PublicHelper.java 61B
Demo
LoadingActivity.java 3KB
DemoActivity.java 7KB
.project 840B
.classpath 280B
AndroidManifest.xml 2KB
共 44 条
- 1
资源评论
局外狗
- 粉丝: 66
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功