您好,我正在尝试在 App Widget 中获取我的手机的当前位置 Widget 接收器工作正常。但我无法获得经度和纬度
public class MobielTrackingActivity extends AppWidgetProvider {
public static String ACTION_WIDGET_RECEIVER1 = "ActionReceiverWidget1";
private static final String DATABASE_TABLE_LOCATIES = "locaties";
DatabaseHelper mOpenHelper;
LocationManager locMgr = null;
LocationListener locListener = null;
private double lng = 0;
private double lat = 0;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {
mOpenHelper = new DatabaseHelper(context);
// Create a RemoteViews object from layout
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.main);
// For button #1
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
if (location != null)
{
lng = location.getLongitude();
lat = location.getLatitude();
}
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
Intent mIntent1 = new Intent(context, MobielTrackingActivity.class);
mIntent1.setAction(ACTION_WIDGET_RECEIVER1);
//mIntent1.putExtra("msg", "Uw locatie is opgeslagen met long "+ lng +" en lat " + lat);
mIntent1.putExtra("msg", "Uw locatie is opgeslagen met " + lat + " " + lng);
PendingIntent mPendingIntent1 = PendingIntent.getBroadcast(context,
0, mIntent1, 0);
remoteViews.setOnClickPendingIntent(R.id.button_saveLocation,mPendingIntent1);
// Update the widget
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
@Override
public void onReceive(Context context, Intent intent) {
// v1.5 fix that doesn't call onDelete Action
final String action = intent.getAction();
if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
final int appWidgetId = intent.getExtras().getInt(
AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
this.onDeleted(context, new int[] { appWidgetId });
}
}
// check, if our Action was called
// Save current location in database
else if (intent.getAction().equals(ACTION_WIDGET_RECEIVER1)) {
String msg = "null";
try {
msg = intent.getStringExtra("msg");
} catch (NullPointerException e) {
Log.e("Error", "msg = null");
}
Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
intent, 0);
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification noty = new Notification(R.drawable.ic_launcher, "Button 1 clicked", System.currentTimeMillis());
noty.setLatestEventInfo(context, "Notice", msg, contentIntent);
notificationManager.notify(1, noty);
// Add current location to database
/*SQLiteDatabase db = mOpenHelper.getWritableDatabase();
ContentValues newRow = new ContentValues();
newRow.put("naam", "locatie 1");
newRow.put("beschrijving", "Wielerwedstrijd door harelbeke");
newRow.put("lng", 10.254);
newRow.put("lat", 540.36);
db.insert(DATABASE_TABLE_LOCATIES, null, newRow);
*/
} else {
// do nothing
}
super.onReceive(context, intent);
}
}
由于某种原因,纬度和经度保持 0.0 和 0.0 我做错了什么。
提前致谢