Bạn có thể thử thủ thuật này.
Đăng ký bộ thu phát sóng được bắt đầu khi nhấn nút nguồn. Bây giờ trong phương pháp OnReceive của Người nhận, hãy làm những gì bạn muốn.
Ví dụ:
trong file manifest đăng ký một máy thu:
<receiver android:name="com.test.check.MyReceiver">
<intent-filter>
<action android:name="android.intent.action.SCREEN_OFF"></action>
<action android:name="android.intent.action.SCREEN_ON"></action>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"></action>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"></action>
<action android:name="android.intent.action.ACTION_SHUTDOWN"></action>
</intent-filter>
</receiver>
& & trong onReceive() phương pháp của Receiver
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
Log.v("#@%@%#", "Power button is pressed.");
Toast.makeText(arg0, "power button clicked",Toast.LENGTH_LONG).show();
//perform what you want here
}
}
Bây giờ thực hiện bất kỳ hoạt động trong onReceive() phương thức của người nhận.
Nguồn
2012-01-20 11:51:26
bạn phải viết một dịch vụ để làm điều đó, tôi đã không thử nó nhưng, đây là một question.http smiliar: //stackoverflow.com/questions/5907102/home-button-listener – ocanal