我的活动没有收到来自我的 GPS 服务的广播
Posted
技术标签:
【中文标题】我的活动没有收到来自我的 GPS 服务的广播【英文标题】:My activity does not receive a Broadcast from my GPS service 【发布时间】:2018-07-30 03:01:57 【问题描述】:大家早上好,
我有一个在执行时启动服务的活动。具体来说就是 GPSService,这个服务会一直等待,直到它到达一个经纬度坐标。 当坐标更新时,服务通过 LocalBroadcastManager 将坐标发送到等待它的活动。
我遇到的问题是该活动从不接收任何东西。
我在这里离开我的活动:
public class MainActivity extends AppCompatActivity
private TextView latitude;
private TextView longitude;
private static final int PETITION_PERMISSION_LOCATION = 101;
private double dLatitude;
private double dLongitude;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
IntentFilter iff= new IntentFilter(GPSService.ACTION);
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, iff);
latitude = findViewById(R.id.lblLatitud);
longitude = findViewById(R.id.lblLongitud);
ejecuteService();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
Snackbar.make(view, dLatitude + " : " + dLongitude, Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
if (isMyServiceRunning(GPSService.class))
stopService(new Intent(MainActivity.this, GPSService.class));
latitude.setText(String.valueOf(dLatitude));
longitude.setText(String.valueOf(dLongitude));
else
ejecuteService();
);
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver()
@Override
public void onReceive(Context context, Intent intent)
Log.e("onReceive","onReceive called");
dLatitude = intent.getDoubleExtra("latitude", 0);
dLongitude = intent.getDoubleExtra("longitude", 0);
Log.e("receiver", "Got Latitude: " + dLatitude);
Log.e("receiver", "Got Longitude: " + dLongitude);
;
@Override
public boolean onCreateOptionsMenu(Menu menu)
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
@Override
public boolean onOptionsItemSelected(MenuItem item)
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in androidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings)
return true;
return super.onOptionsItemSelected(item);
private void ejecuteService()
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(this,
new String[]Manifest.permission.ACCESS_FINE_LOCATION,
PETITION_PERMISSION_LOCATION);
else
startService(new Intent(MainActivity.this, GPSService.class));
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)
if (requestCode == PETITION_PERMISSION_LOCATION)
if (grantResults.length == 1
&& grantResults[0] == PackageManager.PERMISSION_GRANTED)
//Permission granted
startService(new Intent(MainActivity.this, GPSService.class));
else
//Permission denied:
Log.e("PETITION PERMISSION", "Permission denied");
private boolean isMyServiceRunning(Class<?> serviceClass)
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE))
if (serviceClass.getName().equals(service.service.getClassName()))
return true;
return false;
服务内容如下:
public class GPSService extends Service
private static final String TAG = "GPSLocation";
private LocationManager mLocationManager = null;
private static final int LOCATION_INTERVAL = 1000;
private static final float LOCATION_DISTANCE = 10f;
public static final String ACTION = "gpssend";
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);
mLastLocation.set(location);
Log.i(TAG, "Latitude: " + location.getLatitude());
Log.i(TAG, "Longitude: " + location.getLongitude());
sendMessage(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);
LocationListener[] mLocationListeners = new LocationListener[]
new LocationListener(LocationManager.GPS_PROVIDER),
new LocationListener(LocationManager.NETWORK_PROVIDER)
;
@Override
public IBinder onBind(Intent arg0)
return null;
@Override
public int onStartCommand(Intent intent, int flags, int startId)
Log.e(TAG, "onStartCommand");
super.onStartCommand(intent, flags, startId);
return START_STICKY;
@Override
public void onCreate()
Log.e(TAG, "onCreate");
initializeLocationManager();
try
mLocationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
mLocationListeners[1]);
catch (java.lang.SecurityException ex)
Log.i(TAG, "fail to request location update, ignore", ex);
catch (IllegalArgumentException ex)
Log.d(TAG, "network provider does not exist, " + ex.getMessage());
try
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());
@Override
public void onDestroy()
Log.e(TAG, "onDestroy");
super.onDestroy();
if (mLocationManager != null)
for (int i = 0; i < mLocationListeners.length; i++)
try
mLocationManager.removeUpdates(mLocationListeners[i]);
catch (Exception ex)
Log.i(TAG, "fail to remove location listners, ignore", ex);
private void initializeLocationManager()
Log.e(TAG, "initializeLocationManager");
if (mLocationManager == null)
mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
private void sendMessage(Location location)
Log.d(TAG, "Broadcasting message");
Intent intent = new Intent(ACTION);
// You can also include some extra data.
intent.putExtra("latitude", location.getLatitude());
intent.putExtra("longitude", location.getLongitude());
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
在日志的内容中,您可以通过立即更新坐标来了解服务的工作方式。但是该活动没有收到任何东西。
02-19 20:03:18.815 5238-5267/com.google.android.apps.docs I/ProviderInstaller: Installed default security provider GmsCore_OpenSSL
02-19 20:03:24.347 5366-5366/es.jjsr.newmygpsapp:my_service E/GPSLocation: onLocationChanged: Location[gps 37.424998,-122.081998 acc=20 et=+1h2m37s264ms alt=0.0 Bundle[mParcelledData.dataSize=40]]
02-19 20:03:24.347 5366-5366/es.jjsr.newmygpsapp:my_service I/GPSLocation: Latitude: 37.42499833333333
02-19 20:03:24.347 5366-5366/es.jjsr.newmygpsapp:my_service I/GPSLocation: Longitude: -122.08199833333335
02-19 20:03:24.347 5366-5366/es.jjsr.newmygpsapp:my_service D/GPSLocation: Broadcasting message
02-19 20:03:24.348 5366-5366/es.jjsr.newmygpsapp:my_service E/GPSLocation: onStatusChanged: gps
02-19 20:03:27.159 1640-2158/system_process D/WifiService: acquireWifiLockLocked: WifiLockNlpWifiLock type=2 uid=10012
02-19 20:03:27.163 1640-1651/system_process D/WifiService: releaseWifiLockLocked: WifiLockNlpWifiLock type=2 uid=10012
02-19 20:03:47.163 1640-1667/system_process D/WifiService: acquireWifiLockLocked: WifiLockNlpWifiLock type=2 uid=10012
02-19 20:03:47.167 1640-1863/system_process D/WifiService: releaseWifiLockLocked: WifiLockNlpWifiLock type=2 uid=10012
02-19 20:04:07.210 1640-2187/system_process D/WifiService: acquireWifiLockLocked: WifiLockNlpWifiLock type=2 uid=10012
02-19 20:04:07.215 1640-1863/system_process D/WifiService: releaseWifiLockLocked: WifiLockNlpWifiLock type=2 uid=10012
02-19 20:04:11.092 1392-1412/? W/audio_hw_generic: Not supplying enough data to HAL, expected position 772859 , only wrote 772560
02-19 20:04:11.101 5366-5366/es.jjsr.newmygpsapp:my_service E/GPSLocation: onDestroy
02-19 20:04:11.102 1640-2156/system_process I/GnssLocationProvider: WakeLock acquired by sendMessage(3, 0, com.android.server.location.GnssLocationProvider$GpsRequest@35d3846)
02-19 20:04:11.103 1640-1653/system_process I/GnssLocationProvider: WakeLock released by handleMessage(3, 0, com.android.server.location.GnssLocationProvider$GpsRequest@35d3846)
02-19 20:04:14.303 1392-1413/? W/audio_hw_generic: Not supplying enough data to HAL, expected position 1078684 , only wrote 926640
02-19 20:05:07.215 1640-1652/system_process D/WifiService: acquireWifiLockLocked: WifiLockNlpWifiLock type=2 uid=10012
清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="es.jjsr.newmygpsapp">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:name=".GlobalApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".GPSService" android:process=":my_service" />
</application>
</manifest>
感谢这个社区,我学到了很多东西。 非常感谢你所做的一切
【问题讨论】:
【参考方案1】:你必须在清单文件中注册你的广播接收器。我认为这是一个问题
【讨论】:
我已将清单文件添加到问题中,以便我可以验证我没有添加广播接收器。如何将其添加到清单中?非常感谢 使用本指南 (***.com/questions/9425187/…)【参考方案2】:在 onCreate() 方法中编写以下代码-
LocalBroadcastManager.getInstance(getApplicationContext())
.registerReceiver(mBroadcastReceiver,
new IntentFilter(MyService3.MY_SERVICE_MESSAGE));
在 onDestroy 方法中使用以下代码-
LocalBroadcastManager.getInstance(getApplicationContext())
.unregisterReceiver(mBroadcastReceiver);
这将注册和注销您的听众。 由于您已经在清单文件中声明了服务,我认为使用上面的代码将使您的项目工作。如果没有,请发送任何错误
【讨论】:
【参考方案3】:我在这里说明了我是如何解决它的。有趣的是在MainActivity的BroadcastReceiver中
我的 GPS 服务
public class GPSService extends Service
private static final String TAG = "GPSLocation";
private LocationManager mLocationManager = null;
private static final int LOCATION_INTERVAL = 1000;
private static final float LOCATION_DISTANCE = 10f;
public static final String ACTION = "gpssend";
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);
mLastLocation.set(location);
Log.i(TAG, "Latitude: " + location.getLatitude());
Log.i(TAG, "Longitude: " + location.getLongitude());
sendMessage(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);
LocationListener[] mLocationListeners = new LocationListener[]
new LocationListener(LocationManager.GPS_PROVIDER),
new LocationListener(LocationManager.NETWORK_PROVIDER)
;
@Override
public IBinder onBind(Intent arg0)
return null;
@Override
public int onStartCommand(Intent intent, int flags, int startId)
Log.e(TAG, "onStartCommand");
super.onStartCommand(intent, flags, startId);
return START_STICKY;
@Override
public void onCreate()
Log.e(TAG, "onCreate");
initializeLocationManager();
try
mLocationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
mLocationListeners[1]);
catch (java.lang.SecurityException ex)
Log.i(TAG, "fail to request location update, ignore", ex);
catch (IllegalArgumentException ex)
Log.d(TAG, "network provider does not exist, " + ex.getMessage());
try
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());
@Override
public void onDestroy()
Log.e(TAG, "onDestroy");
super.onDestroy();
if (mLocationManager != null)
for (int i = 0; i < mLocationListeners.length; i++)
try
mLocationManager.removeUpdates(mLocationListeners[i]);
catch (Exception ex)
Log.i(TAG, "fail to remove location listners, ignore", ex);
private void initializeLocationManager()
Log.e(TAG, "initializeLocationManager");
if (mLocationManager == null)
mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
private void sendMessage(Location location)
Log.d(TAG, "Broadcasting message");
Intent intent = new Intent();
intent.setAction(ACTION);
// You can also include some extra data.
intent.putExtra("latitude", location.getLatitude());
intent.putExtra("longitude", location.getLongitude());
sendBroadcast(intent);
我的主要活动
public class MainActivity extends AppCompatActivity
private TextView latitude;
private TextView longitude;
private static final int PETITION_PERMISSION_LOCATION = 101;
private double dLatitude;
private double dLongitude;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
latitude = findViewById(R.id.lblLatitud);
longitude = findViewById(R.id.lblLongitud);
ejecuteService();
IntentFilter filter = new IntentFilter();
filter.addAction(GPSService.ACTION);
Receiver receiver = new Receiver();
registerReceiver(receiver, filter);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
Snackbar.make(view, dLatitude + " : " + dLongitude, Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
if (isMyServiceRunning(GPSService.class))
stopService(new Intent(MainActivity.this, GPSService.class));
latitude.setText(String.valueOf(dLatitude));
longitude.setText(String.valueOf(dLongitude));
else
ejecuteService();
);
@Override
public boolean onCreateOptionsMenu(Menu menu)
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
@Override
public boolean onOptionsItemSelected(MenuItem item)
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings)
return true;
return super.onOptionsItemSelected(item);
private void ejecuteService()
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(this,
new String[]Manifest.permission.ACCESS_FINE_LOCATION,
PETITION_PERMISSION_LOCATION);
else
startService(new Intent(MainActivity.this, GPSService.class));
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)
if (requestCode == PETITION_PERMISSION_LOCATION)
if (grantResults.length == 1
&& grantResults[0] == PackageManager.PERMISSION_GRANTED)
//Permission granted
startService(new Intent(MainActivity.this, GPSService.class));
else
//Permission denied:
Log.e("PETITION PERMISSION", "Permission denied");
private boolean isMyServiceRunning(Class<?> serviceClass)
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE))
if (serviceClass.getName().equals(service.service.getClassName()))
return true;
return false;
public class Receiver extends BroadcastReceiver
@Override
public void onReceive(Context context, Intent intent)
if (intent.getAction().equals(GPSService.ACTION))
Log.e("onReceive","onReceive called");
dLatitude = intent.getDoubleExtra("latitude", 0);
dLongitude = intent.getDoubleExtra("longitude", 0);
Log.e("receiver", "Got Latitude: " + dLatitude);
Log.e("receiver", "Got Longitude: " + dLongitude);
我的清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="es.jjsr.newmygpsapp">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:name=".GlobalApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".GPSService" android:process=":my_service" />
</application>
还有 Gradle
implementation 'com.google.android.gms:play-services-location:9.4.0'
非常感谢大家的帮助。
我学到了很多东西。
我把代码留在这里,以防我可以帮助需要它的人
【讨论】:
以上是关于我的活动没有收到来自我的 GPS 服务的广播的主要内容,如果未能解决你的问题,请参考以下文章
为啥我没有收到来自我的相机意图的任何图片(在 Android 中)
当自我没有广播交易时,如何使用以太坊客户端从状态恢复异常中获取消息?