Levanphong7887
Levanphong7887

Reputation: 643

How to send application to Background?

How to send my application to Background use code? How to catch event when "Home key" is pressed?

Many thanks !

Upvotes: 4

Views: 12507

Answers (4)

jeet.chanchawat
jeet.chanchawat

Reputation: 2575

  Intent i = new Intent();
  i.setAction(Intent.ACTION_MAIN);
  i.addCategory(Intent.CATEGORY_HOME);
  this.startActivity(i);

Home key press event cannot be captured as an android security feature.

Upvotes: 16

cottonBallPaws
cottonBallPaws

Reputation: 21600

If you simply want to know when your Activity is no longer visible, override the onPause() and/or onStop() methods.

If you want to start doing something in the background at this point, you could create a Service. A Service will run in the background, unlike an Activity which is more likely to be killed.

If you want to send some data to this Service from your Activity, you can use Intents and Extras. If you want to send more than the simple types of data you can send with extras, use the Application class. (Described in more detail here)

Upvotes: 0

Ted Hopp
Ted Hopp

Reputation: 234795

You can send your activity into the background using moveTaskToBack.

There's no way to catch a HOME key press, or even to detect that it was pressed. The closest you can come is to write a replacement Home screen that uses android.intent.category.HOME. I believe that the user would have control over which activity should get to handle such an intent.

Upvotes: 5

mibollma
mibollma

Reputation: 15108

You can't catch the home key... it would defeat it's purpose after all.

Upvotes: 0

Related Questions