android.content.Context.getMainLooper()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(9.2k)|赞(0)|评价(0)|浏览(338)

本文整理了Java中android.content.Context.getMainLooper()方法的一些代码示例,展示了Context.getMainLooper()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Context.getMainLooper()方法的具体详情如下:
包路径:android.content.Context
类名称:Context
方法名:getMainLooper

Context.getMainLooper介绍

暂无

代码示例

代码示例来源:origin: facebook/litho

public final Looper getMainLooper() {
 return mContext.getMainLooper();
}

代码示例来源:origin: oasisfeng/condom

@Override public Looper getMainLooper() {
  return mBase.getMainLooper();
}

代码示例来源:origin: robolectric/robolectric

private Handler getMainHandler(Context context) {
 if (mainHandler == null) {
  mainHandler = new Handler(context.getMainLooper());
 }
 return mainHandler;
}

代码示例来源:origin: robolectric/robolectric

@Implementation
protected void __constructor__(Context context, IAccountManager service) {
 mainHandler = new Handler(context.getMainLooper());
}

代码示例来源:origin: TeamNewPipe/NewPipe

/**
 * A simple and general error handler that show a Toast for known exceptions, and for others, opens the report error activity with the (optional) error message.
 */
public static void handleGeneralException(Context context, int serviceId, String url, Throwable exception, UserAction userAction, String optionalErrorMessage) {
  final Handler handler = new Handler(context.getMainLooper());
  handler.post(() -> {
    if (exception instanceof ReCaptchaException) {
      Toast.makeText(context, R.string.recaptcha_request_toast, Toast.LENGTH_LONG).show();
      // Starting ReCaptcha Challenge Activity
      Intent intent = new Intent(context, ReCaptchaActivity.class);
      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      context.startActivity(intent);
    } else if (exception instanceof IOException) {
      Toast.makeText(context, R.string.network_error, Toast.LENGTH_LONG).show();
    } else if (exception instanceof YoutubeStreamExtractor.GemaException) {
      Toast.makeText(context, R.string.blocked_by_gema, Toast.LENGTH_LONG).show();
    } else if (exception instanceof ContentNotAvailableException) {
      Toast.makeText(context, R.string.content_not_available, Toast.LENGTH_LONG).show();
    } else {
      int errorId = exception instanceof YoutubeStreamExtractor.DecryptException ? R.string.youtube_signature_decryption_error :
          exception instanceof ParsingException ? R.string.parsing_error : R.string.general_error;
      ErrorActivity.reportError(handler, context, exception, MainActivity.class, null, ErrorActivity.ErrorInfo.make(userAction,
          serviceId == -1 ? "none" : NewPipe.getNameOfService(serviceId), url + (optionalErrorMessage == null ? "" : optionalErrorMessage), errorId));
    }
  });
}

代码示例来源:origin: robolectric/robolectric

private static AccessibilityManager createInstance(Context context) throws Exception {
 if (getApiLevel() >= KITKAT) {
  AccessibilityManager accessibilityManager = Shadow.newInstance(AccessibilityManager.class,
    new Class[]{Context.class, IAccessibilityManager.class, int.class},
    new Object[]{context, ReflectionHelpers.createNullProxy(IAccessibilityManager.class), 0});
  ReflectionHelpers.setField(accessibilityManager, "mHandler", new MyHandler(context.getMainLooper(), accessibilityManager));
  return accessibilityManager;
 } else {
  AccessibilityManager accessibilityManager = Shadow.newInstance(AccessibilityManager.class, new Class[0], new Object[0]);
  ReflectionHelpers.setField(accessibilityManager, "mHandler", new MyHandler(context.getMainLooper(), accessibilityManager));
  return accessibilityManager;
 }
}

代码示例来源:origin: stackoverflow.com

class CheckData{
  private final Handler handler;

  public Checkdata(Context context){
    handler = new Handler(context.getMainLooper());
  } 

  public void someMethod() {
    // Do work
    runOnUiThread(new Runnable() {
      @Override
      public void run() {
        // Code to run on UI thread
      }
    });
  }

  private void runOnUiThread(Runnable r) {
    handler.post(r);
  }  
}

代码示例来源:origin: robolectric/robolectric

@Test
public void shouldReturnLooper() throws Exception {
 handlerThread = new HandlerThread("test");
 handlerThread.start();
 assertNotNull(handlerThread.getLooper());
 assertNotSame(
   handlerThread.getLooper(), ApplicationProvider.getApplicationContext().getMainLooper());
}

代码示例来源:origin: ACRA/acra

public void processReports(boolean isAcraEnabled) {
  final Calendar now = Calendar.getInstance();
  new Handler(context.getMainLooper()).post(() -> new Thread(() -> {
    final List<Report> reports = new ArrayList<>();
    for (File r : reportLocator.getUnapprovedReports()) {

代码示例来源:origin: Trumeet/MiPushFramework

@Override public Looper getMainLooper() {
  return mBase.getMainLooper();
}

代码示例来源:origin: robolectric/robolectric

@Test
public void soStaticRefsToLoopersInAppWorksAcrossTests_shouldRetainSameLooperForMainThreadBetweenResetsButGiveItAFreshScheduler() throws Exception {
 Looper mainLooper = Looper.getMainLooper();
 Scheduler scheduler = shadowOf(mainLooper).getScheduler();
 shadowOf(mainLooper).quit = true;
 assertThat(ApplicationProvider.getApplicationContext().getMainLooper()).isSameAs(mainLooper);
 Scheduler s = new Scheduler();
 RuntimeEnvironment.setMasterScheduler(s);
 ShadowLooper.resetThreadLoopers();
 Application application = new Application();
 ReflectionHelpers.callInstanceMethod(
   application,
   "attach",
   ReflectionHelpers.ClassParameter.from(
     Context.class,
     ((Application) ApplicationProvider.getApplicationContext()).getBaseContext()));
 assertThat(Looper.getMainLooper()).named("Looper.getMainLooper()").isSameAs(mainLooper);
 assertThat(application.getMainLooper()).named("app.getMainLooper()").isSameAs(mainLooper);
 assertThat(shadowOf(mainLooper).getScheduler()).named("scheduler").isNotSameAs(scheduler);
 assertThat(shadowOf(mainLooper).getScheduler()).named("scheduler").isSameAs(s);
 assertThat(shadowOf(mainLooper).hasQuit()).named("quit").isFalse();
}

代码示例来源:origin: aws-amplify/aws-sdk-android

@Override
  public void run() {
    final Handler handler = new Handler(context.getMainLooper());
    Runnable returnCallback;
    try {
      confirmSignUpInternal(confirmationCode, forcedAliasCreation);
      returnCallback = new Runnable() {
        @Override
        public void run() {
          callback.onSuccess();
        }
      };
    } catch (final Exception e) {
      returnCallback = new Runnable() {
        @Override
        public void run() {
          callback.onFailure(e);
        }
      };
    }
    handler.post(returnCallback);
  }
}).start();

代码示例来源:origin: aws-amplify/aws-sdk-android

@Override
  public void run() {
    final Handler handler = new Handler(context.getMainLooper());
    Runnable returnCallback;
    try {
      setUserMfaSettingsInternal(mfaSettings, session);
      returnCallback = new Runnable() {
        @Override
        public void run() {
          callback.onSuccess();
        }
      };
    } catch (final Exception e) {
      returnCallback = new Runnable() {
        @Override
        public void run() {
          callback.onFailure(e);
        }
      };
    }
    handler.post(returnCallback);
  }
}).start();

代码示例来源:origin: jakob-grabner/Circle-Progress-View

AnimationHandler(CircleProgressView circleView) {
  super(circleView.getContext().getMainLooper());
  mCircleViewWeakReference = new WeakReference<CircleProgressView>(circleView);
}

代码示例来源:origin: aws-amplify/aws-sdk-android

@Override
  public void run() {
    final Handler handler = new Handler(context.getMainLooper());
    Runnable nextStep;
    try {
      nextStep = user.initiateUserAuthentication(authenticationDetails, callback, RUN_IN_BACKGROUND);
    } catch (final Exception e) {
      nextStep = new Runnable() {
        @Override
        public void run() {
          callback.onFailure(e);
        }
      };
    }
    handler.post(nextStep);
  }
}).start();

代码示例来源:origin: ukanth/afwall

/**
 * Initializes a new remote preferences object.
 * You must use the same authority as the preference provider.
 * Note that if you pass invalid parameter values, the
 * constructor will complete successfully, but data accesses
 * will either throw {@link IllegalArgumentException} or return
 * default values.
 *
 * @param context Used to access the preference provider.
 * @param authority The authority of the preference provider.
 * @param prefName The name of the preference file to access.
 */
public SharePreference(Context context, String authority, String prefName) {
  mContext = context;
  mHandler = new Handler(context.getMainLooper());
  mBaseUri = Uri.parse("content://" + authority).buildUpon().appendPath(prefName).build();
  mListeners = new WeakHashMap<OnSharedPreferenceChangeListener, PreferenceContentObserver>();
}

代码示例来源:origin: aws-amplify/aws-sdk-android

@Override
  public void run() {
    final Handler handler = new Handler(context.getMainLooper());
    Runnable nextStep;
    try {
      nextStep = user.respondToChallenge(respondToAuthChallengeRequest, callback, RUN_IN_BACKGROUND);
    } catch (final Exception e) {
      nextStep = new Runnable() {
        @Override
        public void run() {
          callback.onFailure(e);
        }
      };
    }
    handler.post(nextStep);
  }
}).start();

代码示例来源:origin: aws-amplify/aws-sdk-android

@Override
  public void run() {
    final Handler handler = new Handler(context.getMainLooper());
    Runnable nextStep;
    try {
      nextStep = user.respondToMfaChallenge(mfaCode, challenge, callback,
          RUN_IN_BACKGROUND);
    } catch (final Exception e) {
      nextStep = new Runnable() {
        @Override
        public void run() {
          callback.onFailure(e);
        }
      };
    }
    handler.post(nextStep);
  }
}).start();

代码示例来源:origin: aws-amplify/aws-sdk-android

@Override
  public void run() {
    final Handler handler = new Handler(context.getMainLooper());
    Runnable returnCallback;
    try {
      final CognitoUserSession session = user.getCachedSession();
      deleteAttributesInternal(attributeNamesToDelete, session);
      returnCallback = new Runnable() {
        @Override
        public void run() {
          callback.onSuccess();
        }
      };
    } catch (final Exception e) {
      returnCallback = new Runnable() {
        @Override
        public void run() {
          callback.onFailure(e);
        }
      };
    }
    handler.post(returnCallback);
  }
}).start();

代码示例来源:origin: aws-amplify/aws-sdk-android

@Override
  public void run() {
    final Handler handler = new Handler(context.getMainLooper());
    Runnable returnCallback;
    try {
      updateDeviceStatusInternal(user.getCachedSession(), DEVICE_TYPE_REMEMBERED);
      returnCallback = new Runnable() {
        @Override
        public void run() {
          callback.onSuccess();
        }
      };
    } catch (final Exception e) {
      returnCallback = new Runnable() {
        @Override
        public void run() {
          callback.onFailure(e);
        }
      };
    }
    handler.post(returnCallback);
  }
}).start();

相关文章

Context类方法