在我的应用程序中,当我在服务和广播接收器之间进行通信时,我的应用程序速度非常慢。 如果我只使用服务,我的应用程序运行正常,但是当我尝试通过广播显示服务的结果时出现问题。 在我的代码下方。
清单:
<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());
}
任何人都可以解决我的问题.
我刚刚自己解决了我的问题,只是从广播接收器中删除了服务的重新启动命令。 谢谢