广播接收器未被触发

时间:2018-05-28 04:59:48

标签: android

您好我正在尝试创建一个服务,当位置发生变化时会触发广播接收器。但由于某种原因,我的广播接收器没有被触发。我已经为位置添加了必要的权限。任何帮助将不胜感激。我正在我的主要活动中启动服务。在我的广播接收器中,我正在重新启动服务,以便我的应用程序将不断向服务器发送位置更新。

我的广播接收器类:

public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {


   // updateLocationServer();
    Intent intent1 = new Intent(context,LocationService.class);
    Log.i("in broadcast","in");
    String latitude = intent1.getStringExtra("latitude");
    String longitude = intent1.getStringExtra("longitude");
    Log.i("lat",latitude);
    Log.i("lat",longitude);

    updateLocationServer(latitude,longitude);
    context.startService(intent1);

}

我的服务类:

public class LocationService extends Service
{

    public static final String BROADCAST_ACTION = "Hello World";
    private static final int TWO_MINUTES = 1000 * 60 * 2;
    public LocationManager locationManager;
    public MyLocationListener listener;
    public Location previousBestLocation = null;

    Intent intent;
    int counter = 0;

    @Override
    public void onCreate()
    {
        super.onCreate();

        intent = new Intent(BROADCAST_ACTION);
    }

    @SuppressLint("MissingPermission")
    @Override
    public void onStart(Intent intent, int startId)
    {
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        listener = new MyLocationListener();
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 20000, 0, listener);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 20000, 0, listener);
    }

    @Override
    public IBinder onBind(Intent intent)
    {
        return null;
    }



    @SuppressLint("MissingPermission")
    @Override
    public void onDestroy() {
        // handler.removeCallbacks(sendUpdatesToUI);
        super.onDestroy();
        Log.v("STOP_SERVICE", "DONE");
        locationManager.removeUpdates(listener);
    }

    public static Thread performOnBackgroundThread(final Runnable runnable) {
        final Thread t = new Thread() {
            @Override
            public void run() {
                try {
                    runnable.run();
                } finally {

                }
            }
        };
        t.start();
        return t;
    }




    public class MyLocationListener implements LocationListener
    {

        public void onLocationChanged(final Location loc) {

            Log.i("lat1", String.valueOf(loc.getLatitude()));
            Log.i("lat2", String.valueOf(loc.getLongitude()));
            intent.putExtra("Latitude", loc.getLatitude());
            intent.putExtra("Longitude", loc.getLongitude());

            sendBroadcast(intent);

        }

        public void onProviderDisabled(String provider)
        {

        }


        public void onProviderEnabled(String provider)
        {

        }


        public void onStatusChanged(String provider, int status, Bundle extras)
        {

        }

    }




}

清单中的静态注册:

    <receiver android:name=".helper.MyBroadcastReceiver"
     >
        <intent-filter>
            <action android:name="android.intent.action.Hello World" >
            </action>
        </intent-filter>
    </receiver>

1 个答案:

答案 0 :(得分:0)

首先在清单

 <action android:name="android.intent.action.Hello World" >

换行

  <action android:name="android.intent.action.HelloWorld" >

在我的服务类

之后
public static final String BROADCAST_ACTION = "Hello World";

public static final String BROADCAST_ACTION = "android.intent.action.HelloWorld";