होम बटन प्रेस का पता लगाना
यह उदाहरण Android में होम बटन प्रेस का पता लगाने को दर्शाता है।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project पर जाएं और आवश्यक विवरण भरकर एक नया प्रोजेक्ट बनाएं।
चरण 2: res/layout/activity_main.xml में कोड जोड़ें
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent">
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:gravity="center"
android:textSize="30sp"
android:layout_height="wrap_content"
android:text="Click here" />
</LinearLayout>
उपरोक्त कोड में, हमने एक TextView लिया है।
चरण 3: src/MainActivity.java में कोड जोड़ें
package com.example.myapplication;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
TextView text;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = findViewById(R.id.text);
text.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onUserLeaveHint();
}
});
}
@Override
protected void onUserLeaveHint() {
text.setText("Home button pressed");
Toast.makeText(MainActivity.this, "Home button pressed", Toast.LENGTH_LONG).show();
super.onUserLeaveHint();
}
}
ऐप चलाएं
अपने Android डिवाइस को कंप्यूटर से कनेक्ट करें। Android Studio में प्रोजेक्ट की एक्टिविटी फाइल खोलें और टूलबार से Run आइकन पर क्लिक करें। अपना मोबाइल डिवाइस चुनें और डिफॉल्ट स्क्रीन देखें।

अब होम बटन पर क्लिक करें, यह नीचे दिखाए अनुसार परिणाम दिखाएगा:
