繁体   English   中英

点击(活动)从服务获取广播

[英]Get a broadcast from service on click(activity)

我有一项在后台运行的服务,负责更改位置更改和这些内容。

在我的活动中,我需要在单击按钮时恢复上一个位置,例如拍照,在单击发送时我想获取服务中的数据。

我试图通过广播发送数据,但是没有按预期方式工作,当我按下按钮时,我尝试访问服务内部的方法。

这是我的服务 (只是重要的代码)

   public void connected() {
        lm = (LocationManager) getSystemService(LOCATION_SERVICE);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

                ActivityCompat.requestPermissions((Activity) getApplicationContext(), new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION},
                        30);
            }
        }

            location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

            if(location != null){
                longitude = location.getLongitude();
                latitude = location.getLatitude();
                altitude = location.getAltitude();
            }


            final LocationListener locationListener = new LocationListener() {
                public void onLocationChanged(Location location) {

                }

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

                }

                @Override
                public void onProviderEnabled(String provider) {

                }

                @Override
                public void onProviderDisabled(String provider) {

                }
            };

            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000, 500, locationListener);

        }

    public void getBroadcastData(Intent i) {
        longitude = location.getLongitude();
        latitude = location.getLatitude();
        altitude = location.getAltitude();
        Log.d("latitude",String.valueOf(latitude));
        Intent intent = new Intent(i);
        intent.setAction("com.myapp.LOCATION_CHANGED");
        intent.putExtra("latitude",latitude);
        intent.putExtra("longitude",longitude);
        intent.putExtra("altitude",altitude);
        sendBroadcast(intent);
    }

你们可以看到我有getBroadcast方法,当我按一下发送图像时,我想捕获该数据。

活动

private void sendImage(byte[] b) {
        ImageStore.getInstance().setCapturedPhotoData(b);
        GoogleLocation.getBroadcastData("com.myapp.LOCATION_CHANGED");

广播接收器活动(动态注册)

onCreate(内部活动)

BroadcastReceiver receiver;

IntentFilter filter;

MyReceiver reciver;



filter = new IntentFilter("com.myapp.LOCATION_CHANGED");
reciver = new MyReceiver();



public class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("receiver","received");
    }
}

然后我注册并取消注册

public void onResume() {
        super.onResume();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.myapp.LOCATION_CHANGED");
        reciver = new MyReceiver();
        registerReceiver(reciver, intentFilter);
    }

    public void onPause() {
        super.onPause();
        if(reciver != null){
            unregisterReceiver(reciver);
            reciver= null;
        }

    }

    public void onStop() {
        super.onPause();
        if(reciver != null){
            unregisterReceiver(receiver);
            reciver = null;
        }

    }

我这样累了,但它说它在另一侧必须是静态的,如果我在服务上更改为静态,则它不能按预期工作,因此我不能从静态上下文访问非静态方法。

有小费吗?

谢谢

您可以共享BroadcastReceiver的代码吗?如果没有,则必须使用一个代码。 以此替换您的getBroadcastData方法-

private static void sendMessageToActivity(Location l, String msg) {
Intent intent = new Intent("LocationUpdates");
// You can also include some extra data.
intent.putExtra("Status", msg);
Bundle b = new Bundle();
b.putParcelable("Location", l);
intent.putExtra("Location", b);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);

}

在活动中-

LocalBroadcastManager.getInstance(getActivity()).registerReceiver(
        mMessageReceiver, new IntentFilter("LocationUpdates"));

也在活动中

private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
    // Get extra data included in the Intent
    String message = intent.getStringExtra("Status");
    Bundle b = intent.getBundleExtra("Location");
    lastKnownLoc = (Location) b.getParcelable("Location");
    //do whatever with lastknownLoc
}

};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM