Spread the love

Ideally you should do not it. But some Android APIs may need code to be run on main thread. e.g. MediaRouter

In that you can use following way to run the code on main thread.

Handler handler = new Handler(context.getMainLooper());
		handler.post(new Runnable() {
			@Override
			public void run() {
				MediaRouter mMediaRouter = MediaRouter.getInstance(context);

				List<MediaRouter.RouteInfo> routes = mMediaRouter.getRoutes();

				for(MediaRouter.RouteInfo info : routes){
					Log.i(TAG, "has routes " + info.getName() +" - " + info.getDescription());
				}
			}
		});

Cheers and Peace out !!!