2013-03-08 69 views
3

我想在用戶按下電源按鈕時啓動我的應用程序。我在以下This code 但它沒有顯示任何Log和吐司。如何在電源按鈕上按啓動應用程序

這是我的完整代碼。

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文件

<?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; 
} 

} 
  • 我想我犯了一個錯誤在我的menifest file。請看看這個。謝謝。
+0

你可以把你的logcat堆棧跟蹤我認爲你短與用戶權限 – DjHacktorReborn 2013-03-08 11:02:32

+0

我沒有得到任何錯誤或應用程序崩潰/強制關閉。因此我沒有任何堆棧跟蹤 – 2013-03-08 11:06:48

回答

4

首先,與其他廣泛流傳的意圖不同,對於Intent.ACTION_SCREEN_OFF和Intent.ACTION_SCREEN_ON,您無法在Android清單中聲明它們!所以,你需要做,這將繼續運行這樣

public static class UpdateService extends Service { 

     @Override 
     public void onCreate() { 
      super.onCreate(); 
      // register receiver that handles screen on and screen off logic 
      IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON); 
      filter.addAction(Intent.ACTION_SCREEN_OFF); 
      BroadcastReceiver mReceiver = new Receiver(); 
      registerReceiver(mReceiver, filter); 
     } 

     @Override 
     public void onStart(Intent intent, int startId) { 
      boolean screenOn = intent.getBooleanExtra("screen_state", false); 
      if (!screenOn) { 
       // your code 
      } else { 
       // your code 
      } 
     } 
} 

服務和接收器可以是一些

public class Receiver extends BroadcastReceiver { 

    private boolean screenOff; 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { 
      screenOff = true; 
     } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { 
      screenOff = false; 
     } 
     Intent i = new Intent(context, UpdateService.class); 
     i.putExtra("screen_state", screenOff); 
     context.startService(i); 
    } 

} 
+0

ScreenReceiver未定義?這是什麼。 – 2013-03-08 11:22:02

+0

更新m答案,這是你的接收機班,我已經發布該代碼也 – DjHacktorReborn 2013-03-08 11:28:15

+0

感謝rplying,但如何menifest文件。 – 2013-03-08 11:31:33

3

這裏是我完整的代碼。希望這可以幫助。我基本上是做一個看屏幕的應用程序。這將禁用您的默認鎖定屏幕。並在電源按鈕按下它將啓動服務並運行以查找電源按鈕按下事件。

Layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<Button 
    android:id="@+id/button1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/toggleButton1" 
    android:layout_marginTop="72dp" 
    android:enabled="false" 
    android:text="Settings" /> 

<ToggleButton 
    android:id="@+id/toggleButton1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:layout_marginTop="72dp" 
    android:checked="true" 
    android:textOff="Disable" 
    android:textOn="Enable" /> 

</RelativeLayout> 

MainActivity.java

package com.example.powerbuttontest; 

import android.app.Activity; 
import android.app.KeyguardManager; 
import android.app.KeyguardManager.KeyguardLock; 
import android.content.Context; 
import android.content.Intent; 
import android.content.res.Configuration; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.Toast; 
import android.widget.ToggleButton; 

public class MainActivity extends Activity { 

ToggleButton btnToggleLock; 
Button btnMisc; 

Toast toast; 

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

    btnMisc = (Button) findViewById(R.id.button1); 
    btnToggleLock = (ToggleButton) findViewById(R.id.toggleButton1); 

    toast = Toast.makeText(getApplicationContext(), "", Toast.LENGTH_SHORT); 

    btnToggleLock.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      if (btnToggleLock.isChecked()) { 

       toast.cancel(); 
       toast.setText("Unlocked"); 
       toast.show(); 

       Log.i("Unlocked", "If"); 

       Context context = getApplicationContext(); 
       KeyguardManager _guard = (KeyguardManager) context 
         .getSystemService(Context.KEYGUARD_SERVICE); 
       KeyguardLock _keyguardLock = _guard 
         .newKeyguardLock("KeyguardLockWrapper"); 
       _keyguardLock.disableKeyguard(); 

       MainActivity.this.startService(new Intent(
         MainActivity.this, UpdateService.class)); 

      } else { 

       toast.cancel(); 
       toast.setText("Locked"); 
       toast.show(); 

       Context context = getApplicationContext(); 
       KeyguardManager _guard = (KeyguardManager) context 
         .getSystemService(Context.KEYGUARD_SERVICE); 
       KeyguardLock _keyguardLock = _guard 
         .newKeyguardLock("KeyguardLockWrapper"); 
       _keyguardLock.reenableKeyguard(); 

       Log.i("Locked", "else"); 

       MainActivity.this.stopService(new Intent(MainActivity.this, 
         UpdateService.class)); 

      } 

     } 
    }); 

} 

@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; 
} 

@Override 
public void onConfigurationChanged(Configuration newConfig) { 
    // TODO Auto-generated method stub 
    super.onConfigurationChanged(newConfig); 

    Log.i("onConfigurationChanged", "Called"); 
} 

} 

MyReciever.java

package com.example.powerbuttontest; 

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 { 
private boolean screenOff; 

@Override 
public void onReceive(Context context, Intent intent) { 
    if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { 
     screenOff = true; 
    } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { 
     screenOff = false; 
    } 
    Intent i = new Intent(context, UpdateService.class); 
    i.putExtra("screen_state", screenOff); 
    context.startService(i); 
} 

} 

UpdateService.java

package com.example.powerbuttontest; 

import android.app.Service; 
import android.content.BroadcastReceiver; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.os.IBinder; 
import android.util.Log; 
import android.widget.Toast; 

public class UpdateService extends Service { 

    BroadcastReceiver mReceiver; 

@Override 
public void onCreate() { 
    super.onCreate(); 
    // register receiver that handles screen on and screen off logic 
    IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON); 
    filter.addAction(Intent.ACTION_SCREEN_OFF); 
    mReceiver = new MyReceiver(); 
    registerReceiver(mReceiver, filter); 
} 

@Override 
public void onDestroy() { 

    unregisterReceiver(mReceiver); 
    Log.i("onDestroy Reciever", "Called"); 

    super.onDestroy(); 
} 

@Override 
public void onStart(Intent intent, int startId) { 
    boolean screenOn = intent.getBooleanExtra("screen_state", false); 
    if (!screenOn) { 
     Log.i("screenON", "Called"); 
     Toast.makeText(getApplicationContext(), "Awake", Toast.LENGTH_LONG) 
       .show(); 
    } else { 
     Log.i("screenOFF", "Called"); 
     // Toast.makeText(getApplicationContext(), "Sleep", 
     // Toast.LENGTH_LONG) 
     // .show(); 
    } 
} 

@Override 
public IBinder onBind(Intent intent) { 
    // TODO Auto-generated method stub 
    return null; 
} 
} 

Menifest.xml

<?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" /> 
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" > 
    <activity 
     android:name=".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" /> 

    <service android:name=".UpdateService" /> 
</application> 

</manifest> 
+0

謝謝。它幫助 – surhidamatya 2013-04-05 10:53:42

+1

很高興聽到:) – 2013-04-05 11:00:41

+1

這是不工作,而從後臺運行應用程序關閉應用程序。 – Karthick 2014-12-28 18:39:44

0

下面這一塊是完整的代碼,這會盡快與您presss電源按鈕打開應用程序。我也在做同樣的項目,我想在按下電源按鈕(打開)後直接打開我的應用程序。

MainActivity.java

public class MainActivity extends Activity 
    { 

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

     startService(new Intent(getApplicationContext(), LockService.class)); 
    }//EOF Oncreate 
    }//EOF Activity 

LockService.java

public class LockService extends Service { 

@Override 
    public IBinder onBind(Intent intent) { 
    return null; 
    } 
    @Override 
    public void onCreate() { 
    super.onCreate(); 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) 
{ 
    final IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON); 
    filter.addAction(Intent.ACTION_SCREEN_OFF); 
    filter.addAction(Intent.ACTION_USER_PRESENT); 
    final BroadcastReceiver mReceiver = new ScreenReceiver(); 
registerReceiver(mReceiver, filter); 
return super.onStartCommand(intent, flags, startId); 
    } 
public class LocalBinder extends Binder 
{ 
    LockService getService() { 
    return LockService.this; 
} 
}//EOF SERVICE 

ScreenReceiver。java的

public class ScreenReceiver extends BroadcastReceiver { 


public static boolean wasScreenOn = true; 

public void onReceive(final Context context, final Intent intent) { 
Log.e("LOB","onReceive"); 

if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) 
{ 
     // do whatever you need to do here 
     wasScreenOn = false; 
     //Log.e("LOB","wasScreenOn"+wasScreenOn); 
     Log.e("Screen ","shutdown now"); 
} 
    else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) 
    { 
     // and do whatever you need to do here 
     wasScreenOn = true; 
     Log.e("Screen ","awaked now"); 

     Intent i = new Intent(context, MainActivity.class); //MyActivity can be anything which you want to start on bootup... 
     i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     context.startActivity(i); 

    } 
    else if(intent.getAction().equals(Intent.ACTION_USER_PRESENT)) 
    { 
     Log.e("LOB","userpresent"); 
     // Log.e("LOB","wasScreenOn"+wasScreenOn); 


    } 
} 

} // EOF SCREENRECEIVER.JAVA

現在這是一個XML文件,請複製粘貼,只是改變你正在使用

<?xml version="1.0" encoding="utf-8"?> 

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


<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name="com.example.userpresent.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> 

     <service android:name="com.example.userpresent.LockService" > 
      <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      </intent-filter> 
     </service> 
</application> 
包名

相關問題