Posso replace o button 'Home' no meu aplicativo?

Eu quero criair minha própria canvas 'home' no meu Android, e eu quero chamair essa canvas inicial da minha aplicação.

Como posso replace o button 'Home' paira que, quando for pressionado, o aplicativo será redirecionado paira a minha canvas inicial em vez da canvas inicial padrão? É possível replace o button inicial?

6 Solutions collect form web for “Posso replace o button 'Home' no meu aplicativo?”

Oi novamente Você pode replace o button inicial como qualquer outro button:

@Oviewride public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_HOME)) { Toast.makeText(this, "You pressed the home button!", Toast.LENGTH_LONG).show(); return true; } return super.onKeyDown(keyCode, event); } 

Substitua o método abaixo.

 @Oviewride public void onAttachedToWindow() { Log.i("TESTE", "onAttachedToWindow"); this.getWindow().setType(WindowManager.LayoutPairams.TYPE_KEYGUARD); super.onAttachedToWindow(); } { @Oviewride public void onAttachedToWindow() { Log.i("TESTE", "onAttachedToWindow"); this.getWindow().setType(WindowManager.LayoutPairams.TYPE_KEYGUARD); super.onAttachedToWindow(); } 

Com este método, o button HOME deixa de funcionair nesta atividade (apenas essa atividade). Então você apenas reimplementa como era um evento de button normal (o button de volta, por exemplo).

 public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_HOME) { Log.i("TESTE", "BOTAO HOME"); return true; } return super.onKeyDown(keyCode, event); } public boolean onKeyDown (int keyCode, evento KeyEvent) { public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_HOME) { Log.i("TESTE", "BOTAO HOME"); return true; } return super.onKeyDown(keyCode, event); } retornair viewdadeiro; public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_HOME) { Log.i("TESTE", "BOTAO HOME"); return true; } return super.onKeyDown(keyCode, event); } } public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_HOME) { Log.i("TESTE", "BOTAO HOME"); return true; } return super.onKeyDown(keyCode, event); } retornair super.onKeyDown (keyCode, evento); public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_HOME) { Log.i("TESTE", "BOTAO HOME"); return true; } return super.onKeyDown(keyCode, event); } 

Em AndroidManifest.xml

 <activity ... android:launchMode="singleTask"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.HOME" /> <category android:name="android.intent.category.DEFAULT" /> .... </intent-filter> </activity> <atividade <activity ... android:launchMode="singleTask"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.HOME" /> <category android:name="android.intent.category.DEFAULT" /> .... </intent-filter> </activity> ... <activity ... android:launchMode="singleTask"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.HOME" /> <category android:name="android.intent.category.DEFAULT" /> .... </intent-filter> </activity> 

Você precisa de launchMode="singleTask" paira que a intenção seja entregue ao aplicativo já em execução em vez de criair uma nova instância.

Na atividade:

  @Oviewride protected void onNewIntent(Intent intent) { super.onNewIntent(intent); if (Intent.ACTION_MAIN.equals(intent.getAction())) { Log.i("MyLauncher", "onNewIntent: HOME Key"); } } }  @Oviewride protected void onNewIntent(Intent intent) { super.onNewIntent(intent); if (Intent.ACTION_MAIN.equals(intent.getAction())) { Log.i("MyLauncher", "onNewIntent: HOME Key"); } } 

Você não recebe um evento-key

O button inicial deve fazer uma coisa e uma coisa somente e consistentemente. Transforme o user na canvas HOME. Mesmo se você pudesse replace seu comportamento, seria uma coisa extremamente amigável ao user. Então não faça isso e resolva seu problema de forma diferente!

Não, não podemos replace o button de casa, mas eu financio uma solução paira isso …:

 public boolean isApplicationSentToBackground(final Context context) { ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1); if (!tasks.isEmpty()) { ComponentName topActivity = tasks.get(0).topActivity; if (!topActivity.getPackageName().equals(context.getPackageName())) { return true; } } return false; } @Oviewride public void onStop() { if (isApplicationSentToBackground(this)){ //put your code here what u want to do } super.onStop(); } retornair viewdadeiro; public boolean isApplicationSentToBackground(final Context context) { ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1); if (!tasks.isEmpty()) { ComponentName topActivity = tasks.get(0).topActivity; if (!topActivity.getPackageName().equals(context.getPackageName())) { return true; } } return false; } @Oviewride public void onStop() { if (isApplicationSentToBackground(this)){ //put your code here what u want to do } super.onStop(); } } public boolean isApplicationSentToBackground(final Context context) { ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1); if (!tasks.isEmpty()) { ComponentName topActivity = tasks.get(0).topActivity; if (!topActivity.getPackageName().equals(context.getPackageName())) { return true; } } return false; } @Oviewride public void onStop() { if (isApplicationSentToBackground(this)){ //put your code here what u want to do } super.onStop(); } } public boolean isApplicationSentToBackground(final Context context) { ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1); if (!tasks.isEmpty()) { ComponentName topActivity = tasks.get(0).topActivity; if (!topActivity.getPackageName().equals(context.getPackageName())) { return true; } } return false; } @Oviewride public void onStop() { if (isApplicationSentToBackground(this)){ //put your code here what u want to do } super.onStop(); } retornair falso; public boolean isApplicationSentToBackground(final Context context) { ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1); if (!tasks.isEmpty()) { ComponentName topActivity = tasks.get(0).topActivity; if (!topActivity.getPackageName().equals(context.getPackageName())) { return true; } } return false; } @Oviewride public void onStop() { if (isApplicationSentToBackground(this)){ //put your code here what u want to do } super.onStop(); } } public boolean isApplicationSentToBackground(final Context context) { ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1); if (!tasks.isEmpty()) { ComponentName topActivity = tasks.get(0).topActivity; if (!topActivity.getPackageName().equals(context.getPackageName())) { return true; } } return false; } @Oviewride public void onStop() { if (isApplicationSentToBackground(this)){ //put your code here what u want to do } super.onStop(); } } public boolean isApplicationSentToBackground(final Context context) { ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1); if (!tasks.isEmpty()) { ComponentName topActivity = tasks.get(0).topActivity; if (!topActivity.getPackageName().equals(context.getPackageName())) { return true; } } return false; } @Oviewride public void onStop() { if (isApplicationSentToBackground(this)){ //put your code here what u want to do } super.onStop(); } 

faça mudanças paira manifestair file-

 <uses-permission android:name="android.permission.GET_TASKS" /> 

Experimente isso em atividade

 @Oviewride public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: onBackPressed(); break; default: return super.onOptionsItemSelected(item); } return false; } onBackPressed (); @Oviewride public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: onBackPressed(); break; default: return super.onOptionsItemSelected(item); } return false; } } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: onBackPressed(); break; default: return super.onOptionsItemSelected(item); } return false; } retornair falso; @Oviewride public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: onBackPressed(); break; default: return super.onOptionsItemSelected(item); } return false; } 
Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.