我有一个从broadcastReceiver扩展的类,它给我GPS坐标。我想显示手机的序列号及其坐标。
public class AlarmReceiver extends BroadcastReceiver{
long time = 10 * 1000;
@Override
public void onReceive(final Context context, Intent intent) {
LocationManager locationManager = (LocationManager)context
.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
locationManager.requestLocationUpdates(provider, time, minDistance, locationListener);
Location location = locationManager.getLastKnownLocation(provider);
String serialNumber = Build.SERIAL != Build.UNKNOWN ? Build.SERIAL : Secure.getString(getContentResolver(), Secure.ANDROID_ID); // returns device id of phone
String Text = "Latitude = " + location.getLatitude() +" Longitude = " location.getLongitude();
Toast.makeText(context, Text, Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(context, "SMS faild, please try again.",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
LocationListener locationListener = new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
}
为了获取设备序列号,我使用以下代码,但它显示了一个错误,即getContentResolver()未定义。我已经搜索了解决方案并尝试了getActivity()和getApplicationContext(),但这对我不起作用。
String serialNumber = Build.SERIAL != Build.UNKNOWN ? Build.SERIAL : Secure.getString(getContentResolver(), Secure.ANDROID_ID); // returns device id of phone
答案 0 :(得分:1)
嗯,应该是:
context.getContentResolver()
因为ContentResolver
属于Context
,Context
会通过回调传递。
答案 1 :(得分:1)
您可以使用context
或
context.getContentResolver();
答案 2 :(得分:0)
只需传递Context
并使用context.getContentResolver().
答案 3 :(得分:0)
将上下文从一个类传递到另一个类 使用context.getContextResolver()