package com.blogspot.programer27android.stopwacth; import android.content.DialogInterface;import android.support.v7.app.AlertDialog;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.KeyEvent;import android.view.WindowManager; public class Activity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_HOME)) { System.out.println("KEYCODE_HOME"); showDialog("'HOME'"); return true; } if ((keyCode == KeyEvent.KEYCODE_BACK)) { System.out.println("KEYCODE_BACK"); showDialog("'BACK'"); return true; } if ((keyCode == KeyEvent.KEYCODE_MENU)) { System.out.println("KEYCODE_MENU"); showDialog("'MENU'"); return true; } return false; } void showDialog(String the_key){ AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("You have pressed the " + the_key + " button. Would you like to exit the app?") .setCancelable(true) .setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); finish(); } }) .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); AlertDialog alert = builder.create(); alert.setTitle("CoderzHeaven."); alert.show(); } @Override public void onAttachedToWindow() { super.onAttachedToWindow(); this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG); } public void onUserLeaveHint() { // this only executes when Home is selected. // do stuff super.onUserLeaveHint(); System.out.println("HOMEEEEEEEEE"); } }
is there a solution for android 4.0 and above.
the answer is ..
but only one possibility that we can do. just add code to the manifest ..
<activityandroid:name=".Activity"android:launchMode="singleInstance"><intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.HOME" /> <category android:name="android.intent.category.DEFAULT" /></intent-filter></activity>
dan anda harus menentukan apa yang akan dikerjakan oleh onCread() dan apa yang akan dikerjakan oleh onResume()
and you must specify the default application as launcher
thanks..
Tidak ada komentar:
Posting Komentar