2017-01-23 50 views
0

在我的應用程序中,當我在服務和broadcastreceiver之間進行通信時,我的應用程序非常慢。如果我只使用服務,我的應用程序運行正常,但是當我嘗試通過廣播顯示服務結果時會出現問題。在我的代碼下面。應用程序在使用廣播接收器與服務進行通信時極度放慢使用android

清單:

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

廣播接收器:

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()); 

    } 

誰能請解決我的問題。

+0

所以不要使用'BroadcastReceiver's,使用「綁定本地服務」模式代替 – pskink

+0

,但根據我的要求,我需要使用BroadcastReceiver。請幫我做這個 – AAA

+0

有什麼要求? – pskink

回答

1

我剛剛解決了我自己的問題,只是刪除broadcastreceiver服務的重新啓動命令。謝謝

相關問題