このトリックを試すことができます。
パワーボタンがクリックされたときに開始されるブロードキャストレシーバを登録します。 ReceiverのOnReceiveメソッドで、何をしたいのですか?例えば
:
マニフェストファイル内の受信レジスタ:受信
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
}
}
のonReceive()メソッドで
<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>
& &を今onReceive(任意の操作を実行します)受信者の方法。
あなたはここにsmiliar question.httpがある、私はそれをしようとしなかった、それを行うためにサービスを記述する必要がありますが://stackoverflow.com/questions/5907102/home-button-listener – ocanal