如何获取当前位置和去电摘机时间(确切的呼叫选择时间)?
How to get the current location and outgoing call off-hook time(exact call picked time)?
我想在 android.Here 中获取拨出电话的位置、时间和日期 我正在使用一个广播接收器来检测新的拨出电话,在广播接收器中我正在启动一项服务实现位置接收器,为获取每个新呼出的当前位置,它会触发。
我的问题是:
- 我要当前位置,时间,去电日期
广播接收器
public class OutGoingCallReceiver extends BroadcastReceiver {
public static String phoneNumber;
@Override
public void onReceive(Context context, Intent intent) {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (state == null) {
phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Intent serviceIntent = new Intent(context, NarenService.class);
context.startService(serviceIntent);
Toast.makeText(context, "Calling..to " + phoneNumber, Toast.LENGTH_SHORT).show();
}
}
}
服务
public class NarenService extends Service implements LocationListener {
SQLiteDatabase db;
double lat, lon;
LocationManager locationManager;
String provider;
DataProvider dataProvider;
Cursor cursor;
private static final String TAG = "NarenService";
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
getLocation();
// OutGoingCallReceiver.phoneNumber = str;
// OutGoingCallReceiver outGoingCallReceiver = new OutGoingCallReceiver()
dataProvider = new DataProvider(this);
if (lon != 0.0 && lat != 0.0) {
dataProvider.open();
dataProvider.insertEntry(lon, lat, OutGoingCallReceiver.phoneNumber);
Log.v(TAG, "Inserted data");
// dataProvider.close();
Toast.makeText(getBaseContext(), "Lat : " + lat + "\n lon : " + lon + "\n num :" + OutGoingCallReceiver.phoneNumber, Toast.LENGTH_SHORT).show();
}
stopSelf();
return START_STICKY;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
private void getLocation() {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
if (provider != null && !provider.equals("")) {
// Get the location from the given provider
Location location = locationManager.getLastKnownLocation(provider);
locationManager.requestLocationUpdates(provider, 60000, 10, this);
if (location != null) {
Log.v(TAG, "Entered into loc");
onLocationChanged(location);
} else
Toast.makeText(getBaseContext(), "Location can't be retrieved", Toast
.LENGTH_SHORT).show();
} else {
Toast.makeText(getBaseContext(), "No Provider Found", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onLocationChanged(Location location) {
lat = location.getLatitude();
lon = location.getLongitude();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onDestroy() {
super.onDestroy();
// dataProvider.close();
Toast.makeText(this, "service stopped", Toast.LENGTH_SHORT).show();
}
}
使用此代码结束通话
case TelephonyManager.CALL_STATE_IDLE:
//Went to idle- this is the end of a call. What type depends on previous state(s)
if(lastState == TelephonyManager.CALL_STATE_RINGING){
//Ring but no pickup- a miss
onMissedCall(savedNumber, callStartTime);
}
else if(isIncoming){
onIncomingCallEnded(savedNumber, callStartTime, new Date());
}
else{
onOutgoingCallEnded(savedNumber, callStartTime, new Date());
}
break;
Google 通过其 Google Play Service Framework 引入了 Fused Location Provider api,它比 GPS / Network Location providers 更易于使用。
最高兼容 API 级别 8,现在当我遇到网络提供商的这个奇怪问题时,同时 Fused Location 会为我提供准确的位置(无需重启设备)。
我在这里提交了一个可用的 FusedLocation 测试项目,你可以自己克隆和测试..
LocationListener of NETWORK_PROVIDER is enabled but , onLocationChanged is never called
我想在 android.Here 中获取拨出电话的位置、时间和日期 我正在使用一个广播接收器来检测新的拨出电话,在广播接收器中我正在启动一项服务实现位置接收器,为获取每个新呼出的当前位置,它会触发。
我的问题是:
- 我要当前位置,时间,去电日期
广播接收器
public class OutGoingCallReceiver extends BroadcastReceiver {
public static String phoneNumber;
@Override
public void onReceive(Context context, Intent intent) {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (state == null) {
phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Intent serviceIntent = new Intent(context, NarenService.class);
context.startService(serviceIntent);
Toast.makeText(context, "Calling..to " + phoneNumber, Toast.LENGTH_SHORT).show();
}
}
}
服务
public class NarenService extends Service implements LocationListener {
SQLiteDatabase db;
double lat, lon;
LocationManager locationManager;
String provider;
DataProvider dataProvider;
Cursor cursor;
private static final String TAG = "NarenService";
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
getLocation();
// OutGoingCallReceiver.phoneNumber = str;
// OutGoingCallReceiver outGoingCallReceiver = new OutGoingCallReceiver()
dataProvider = new DataProvider(this);
if (lon != 0.0 && lat != 0.0) {
dataProvider.open();
dataProvider.insertEntry(lon, lat, OutGoingCallReceiver.phoneNumber);
Log.v(TAG, "Inserted data");
// dataProvider.close();
Toast.makeText(getBaseContext(), "Lat : " + lat + "\n lon : " + lon + "\n num :" + OutGoingCallReceiver.phoneNumber, Toast.LENGTH_SHORT).show();
}
stopSelf();
return START_STICKY;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
private void getLocation() {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
if (provider != null && !provider.equals("")) {
// Get the location from the given provider
Location location = locationManager.getLastKnownLocation(provider);
locationManager.requestLocationUpdates(provider, 60000, 10, this);
if (location != null) {
Log.v(TAG, "Entered into loc");
onLocationChanged(location);
} else
Toast.makeText(getBaseContext(), "Location can't be retrieved", Toast
.LENGTH_SHORT).show();
} else {
Toast.makeText(getBaseContext(), "No Provider Found", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onLocationChanged(Location location) {
lat = location.getLatitude();
lon = location.getLongitude();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onDestroy() {
super.onDestroy();
// dataProvider.close();
Toast.makeText(this, "service stopped", Toast.LENGTH_SHORT).show();
}
}
使用此代码结束通话
case TelephonyManager.CALL_STATE_IDLE:
//Went to idle- this is the end of a call. What type depends on previous state(s)
if(lastState == TelephonyManager.CALL_STATE_RINGING){
//Ring but no pickup- a miss
onMissedCall(savedNumber, callStartTime);
}
else if(isIncoming){
onIncomingCallEnded(savedNumber, callStartTime, new Date());
}
else{
onOutgoingCallEnded(savedNumber, callStartTime, new Date());
}
break;
Google 通过其 Google Play Service Framework 引入了 Fused Location Provider api,它比 GPS / Network Location providers 更易于使用。
最高兼容 API 级别 8,现在当我遇到网络提供商的这个奇怪问题时,同时 Fused Location 会为我提供准确的位置(无需重启设备)。
我在这里提交了一个可用的 FusedLocation 测试项目,你可以自己克隆和测试..
LocationListener of NETWORK_PROVIDER is enabled but , onLocationChanged is never called