gpt4 book ai didi

java - Intent 未通过广播监听器注册

转载 作者:行者123 更新时间:2023-11-30 08:43:57 27 4
gpt4 key购买 nike

我已经尝试了很长时间来弄清楚如何将数据从服务类发送到 mainactivity 类。我现在正在使用广播接收器来做这件事。问题是 MainActivity 类中没有接收到它们。

public class MyService extends Service {
//all my code for the service which takes the users gps location.
private static final String TAG = "Hello";
private LocationManager mLocationManager = null;
private static final int LOCATION_INTERVAL = 10;//change this for time
private static final float LOCATION_DISTANCE = 0f;//change this to get it every distance
private static final String MY_ACTION = "MY_ACTION";
private final Handler handler = new Handler();
Intent intent;
private class LocationListener implements android.location.LocationListener {
Location mLastLocation;

public LocationListener(String provider) {
//Log.e(TAG, "LocationListener " + provider);
mLastLocation = new Location(provider);
}

@Override
public void onLocationChanged(Location location) {
//Log.e(TAG, "onLocationChanged: " + location.toString());
mLastLocation.set(location);

}

@Override
public void onProviderDisabled(String provider) {
Log.e(TAG, "onProviderDisabled: " + provider);
}

@Override
public void onProviderEnabled(String provider) {
Log.e(TAG, "onProviderEnabled: " + provider);
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.e(TAG, "onStatusChanged: " + provider);
}
}
//different possible uses for locations
LocationListener[] mLocationListeners = new LocationListener[]{
new LocationListener(LocationManager.GPS_PROVIDER),
new LocationListener(LocationManager.NETWORK_PROVIDER)
};

@Override
public IBinder onBind(Intent arg0) {
return null;
}
//the start of the activity sends data every 5 seconds
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG, "onStartCommand");
super.onStartCommand(intent, flags, startId);
handler.removeCallbacks(sendUpdatesToUI);
handler.postDelayed(sendUpdatesToUI, 1000); // 1 second
return START_STICKY;
}

@Override
public void onCreate() {
Log.e(TAG, "onCreate");
//initializes locationManager
initializeLocationManager();
try {
Toast.makeText(MyService.this, "It Works", Toast.LENGTH_SHORT).show();
mLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
mLocationListeners[0]);
} catch (java.lang.SecurityException ex) {
Log.i(TAG, "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
Log.d(TAG, "gps provider does not exist " + ex.getMessage());
}
}

private void initializeLocationManager() {
if (mLocationManager == null) {
mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
}
}
private Runnable sendUpdatesToUI = new Runnable() {
public void run() {
DisplayLoggingInfo();
handler.postDelayed(this, 5000); // 5 seconds delay
}
};

private void DisplayLoggingInfo() {
Log.d(TAG, "entered DisplayLoggingInfo");
intent = new Intent();
initializeLocationManager();
try {
Toast.makeText(MyService.this, "It Works", Toast.LENGTH_SHORT).show();
mLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
mLocationListeners[0]);
intent.putExtra("Latitude", mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLatitude());
intent.putExtra("Longitude", mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLongitude());
sendBroadcast(intent);
Log.i(TAG,"intent sent");
} catch (java.lang.SecurityException ex) {
Log.i(TAG, "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
Log.d(TAG, "gps provider does not exist " + ex.getMessage());
}
}
}

数据已发送。但是,当我尝试从 MainActivity 访问它时,它似乎没有得到它。

public class MainActivity extends AppCompatActivity {
public SimpleLocation location;
Intent intent;
double latitude;
double longitude;


@Override
protected void onCreate(Bundle savedInstanceState) {
//Sets views for Android
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
startService(new Intent(this, MyService.class));
intent = new Intent(this, MyService.class);
LocalBroadcastManager.getInstance(this).registerReceiver(
broadcastReceiver, new IntentFilter());
Toast.makeText(MainActivity.this, "" + latitude, Toast.LENGTH_SHORT).show();
}

//sets up broadcast Reciever
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Double latitude = intent.getDoubleExtra("Latitude", 0);
Double longitude = intent.getDoubleExtra("Longitude", 0);
Log.d("hello", "It came this far");
Log.d("hello", " " + latitude);
Log.d("hello", " " + longitude);
TextView txtDateTime = (TextView) findViewById(R.id.textView);
txtDateTime.setText(latitude.toString());
}
};

@Override
public void onResume() {
super.onResume();
startService(intent);
registerReceiver(broadcastReceiver, new IntentFilter());
}

@Override
public void onPause() {
super.onPause();
unregisterReceiver(broadcastReceiver);
stopService(intent);
}

}

有谁知道我做错了什么?

最佳答案

首先,你应该在发送的 Intent 中添加一个 Action ,假设是location,然后替换

 intent.putExtra("Latitude", mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLatitude());
intent.putExtra("Longitude", mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLongitude());
sendBroadcast(intent);

 intent.setAction("location");
intent.putExtra("Latitude", mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLatitude());
intent.putExtra("Longitude", mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLongitude());
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);

并替换

LocalBroadcastManager.getInstance(this).registerReceiver(
broadcastReceiver, new IntentFilter());

IntentFilter filter = new IntentFilter("location");
LocalBroadcastManager.getInstance(this).registerReceiver(
broadcastReceiver, filter);

并且你应该在启动服务之前注册接收者,所以替换

    @Override
public void onResume() {
super.onResume();
startService(intent);
registerReceiver(broadcastReceiver, new IntentFilter());
}

    @Override
public void onResume() {
super.onResume();
LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver, new IntentFilter());
startService(intent);
}

因为你已经在onResume中注册了,所以你可以移除

LocalBroadcastManager.getInstance(this).registerReceiver(
broadcastReceiver, new IntentFilter());

来自您的onCreate 方法。

关于java - Intent 未通过广播监听器注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33979331/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com