使用broadcastreceiver与服务android进行通信时,应用程序的速度极慢

时间:2017-01-23 05:50:14

标签: android service android-broadcastreceiver

在我的应用程序中,当我在服务和广播接收器之间进行通信时,我的应用程序非常慢。如果我只使用服务我的应用运行正常,但当我尝试通过广播显示服务的结果时出现问题。低于我的代码。

清单:

<service
            android:name=".ServiceClass_GetGps"
            android:exported="false" />

broadcastreceiver:

public class FragmentMap extends Fragment implements OnMapReadyCallback{

    double lat,lng;
    GoogleMap googleMap=null;

    public FragmentMap() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        setRetainInstance(true);
        // Inflate the layout for this fragment

        View view = inflater.inflate(R.layout.fragment_map, null, false);

        SupportMapFragment mapFragment=(SupportMapFragment)this.getChildFragmentManager().findFragmentById(R.id.map_FragmentMapId);
        mapFragment.getMapAsync(this);

        Intent intent=new Intent(getActivity(),ServiceClass_GetGps.class);
        getActivity().startService(intent);

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        IntentFilter intentFilter=new IntentFilter("imran");
        LocalBroadcastManager.getInstance(getActivity()).registerReceiver(broadcastReceiver,intentFilter);
    }

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

        LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(broadcastReceiver);

    }

    //broadcast receiver

    private BroadcastReceiver broadcastReceiver=new BroadcastReceiver() {
        @Override
        public void onReceive(final Context context, final Intent intent) {

            //String result=intent.getStringExtra("result");
//            Toast.makeText(getActivity(), "Lat " +intent.getStringExtra("LAT") +" Lng "+intent.getStringExtra("LNG"), Toast.LENGTH_SHORT).show();
            Handler handler=new Handler();

            handler.post(new Runnable() {
                @Override
                public void run() {

                    context.startService(new Intent(context.getApplicationContext(),ServiceClass_GetGps.class));

                    if(intent.getStringExtra("LAT")!=null && intent.getStringExtra("LNG")!=null){
                        lat=Double.valueOf(intent.getStringExtra("LAT"));
                        lng=Double.valueOf(intent.getStringExtra("LNG"));

                        LatLng latLng=new LatLng(lat,lng);

                        if(googleMap!=null){
                            googleMap.addMarker(new MarkerOptions().position(latLng));
                            googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng,16));
                        }else {
                            Toast.makeText(getActivity(),"Map is null",Toast.LENGTH_LONG).show();
                        }
                    }

                }
            });

        }
    };

    @Override
    public void onMapReady(GoogleMap googleMap) {

        this.googleMap=googleMap;

    }

服务类:

public class ServiceClass_GetGps extends Service implements GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener,LocationListener{


    private HandlerThread handlerThread;
    ServiceHandler serviceHandler;
    LocalBroadcastManager localBroadcastManager;

    private final static class ServiceHandler extends Handler {

        public ServiceHandler(Looper looper){
            super(looper);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        handlerThread=new HandlerThread("com.example.imranrana.datingapp");
        handlerThread.start();

        serviceHandler=new ServiceHandler(handlerThread.getLooper());
        localBroadcastManager=LocalBroadcastManager.getInstance(this);
    }

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

    @Override
    public int onStartCommand(final Intent intent, int flags, int startId) {

        final Handler handler=new Handler();

        new Thread(new Runnable() {
            @Override
            public void run() {

//                Intent intent=new Intent("com.example.imranrana.datingapp");
//                intent.putExtra("result","Imran");
//                localBroadcastManager.sendBroadcast(intent);

                handler.post(new Runnable() {
                    @Override
                    public void run() {

                        Intent intent1=new Intent("imran");
                        intent1.putExtra("LAT","23.8758448");
                        intent1.putExtra("LNG","90.3981515");
                        LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent1);

                        Toast.makeText(getApplicationContext(),"Service test",Toast.LENGTH_LONG).show();

                    }
                });

                handler.postDelayed(this,5000);
            }
        }).start();

        return START_STICKY;
    }


    @Override
    public void onConnected(@Nullable Bundle bundle) {

    }

    @Override
    public void onConnectionSuspended(int i) {

    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

    }

    @Override
    public void onLocationChanged(Location location) {


    }



    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(getApplicationContext(),"apps stopped",Toast.LENGTH_LONG).show();
        sendBroadcast(new Intent());

    }

任何人都可以解决我的问题。

1 个答案:

答案 0 :(得分:1)

我刚刚解决了我的问题,只需从broadcastreceiver中删除重启命令。感谢