comment faire pour démarrer l'application sur le bouton d'alimentation appuyez sur

Je veux commencer mon application lorsque l'utilisateur presse le bouton d'alimentation. Je m Ce code
mais il n'affiche pas tout Log et de pain grillé.

voici mon code complet.

MyReceiver.java

import android.content.BroadcastReceiver;
   import android.content.Context;
   import android.content.Intent;
   import android.util.Log;
   import android.widget.Toast;

   public class MyReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    //TODO Auto-generated method stub

    Log.v("onReceive", "Power button is pressed.");

    Toast.makeText(context, "power button clicked", Toast.LENGTH_LONG)
            .show();

    //perform what you want here

}

}

menifest Fichier

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.powerbuttontest"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.powerbuttontest.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver android:name=".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>
</application>
</manifest>

MainActivity.java

package com.example.powerbuttontest;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    //Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}
  • Je pense que je m commettre une erreur dans mon menifest file. jetez un oeil sur cette. merci.
pouvez-vous mettre votre logcat trace de la pile je pense que vous êtes à court avec l'autorisation de l'utilisateur
Je m de ne pas obtenir de toute erreur ou plantage de l'application/la force de fermeture. donc je n'ai pas de toute trace de la pile

OriginalL'auteur Qadir Hussain | 2013-03-08