Casa > Q > Quais São Os Passos Para Criar Um Método De Entrada Personalizado No Android?

Quais são os passos para criar um método de entrada personalizado no Android?

The method getBaseContext() is only relevant when you have a ContextWrapper.

Android provides a ContextWrapper class that is created around an existing Context using:

  1. ContextWrapper wrapper = new ContextWrapper(context); 

The benefit of using a ContextWrapper is that it lets you “modify behavior without changing the original Context”. For example, if you have an activity called myActivity then can create a View with a different theme than myActivity:

  1. ContextWrapper customTheme = new ContextWrapper(myActivity) { 
  2. @Override 
  3. public Resources.Theme getTheme() {  
  4. return someTheme; 
  5. View myView = new MyView(customTheme); 

ContextWrapper is really powerful because it lets you override most functions provided by Context including code to access resources (e.g. openFileInput(), getString()), interagir com outros componentes (ex. sendBroadcast(), registerReceiver()), solicitar permissões (ex. checkCallingOrSelfPermission()) e resolver localizações do sistema de arquivos (ex. getFilesDir()). ContextWrapper é realmente útil para trabalhar com problemas específicos de dispositivos/versão ou para aplicar customizações únicas a componentes como Views que requerem um contexto.

O método getBaseContext() pode ser usado para acessar o Contexto "base" que o ContextWrapper envolve. You might need to access the “base” context if you need to, for example, check whether it’s a Service, Activity or Application:

  1. public class CustomToast { 
  2. public void makeText(Context context, int resId, int duration) { 
  3. while (context instanceof ContextWrapper) { 
  4. context = context.baseContext(); 
  5. if (context instanceof Service)) { 
  6. throw new RuntimeException("Cannot call this from a service"); 
  7. ... 

Or if you need to call the “unwrapped” version of a method:

  1. class MyCustomWrapper extends ContextWrapper { 
  2. @Override 
  3. public Drawable getWallpaper() { 
  4. if (BuildInfo.DEBUG) { 
  5. return debugBackground; 
  6. } else { 
  7. return getBaseContext().getWallpaper(); 

De Bernetta Mlakar

Eu deixei um pacote UPS hoje, mas as informações de rastreamento online mostram que o pacote não saiu da loja no final do dia. Isto é normal? :: Como ser mais rápido a digitar com uma mão