Issue
I am developing an application where I could listen to POWER_BUTTON clicked for 3 times consecutively in 3-5 seconds.
I have searched all the StackOverflow for answers but none of them has worked for me.
This answer from href="https://stackoverflow.com/questions/3703071/how-to-hook-into-the-power-button-in-android">Lars D which should work on the activity doesn't work too though it is accepted.
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
Intent i = new Intent(this, ActivitySetupMenu.class);
startActivity(i);
return true;
}
return super.dispatchKeyEvent(event);
}
There is an app that is intending to do this, I installed it but still, it doesn't work maybe the API they used are depreciated or even has been removed.
The Reasons why these solutions do not work :
- when the App gets killed/destroyed and it no longer detects.
- The screen is locked and again it no longer detects.
- Maybe the SDK does not support it.
Maybe we can find a way to do it on the activity but I want to listen to the actions using a service/broadcast Receiver while the app is killed/in the background/when screen locked/ when screen off.
Well, this question is definitely repeated many times on StackOverflow but no complete or working answer has been given.
Solution
Since nobody tried to solve the question or maybe couldn't even understand the question, fortunately after many hours of searching the web I found this awesome website that solves exactly my problem and wanted to post it here too.
Problems which are solved now :
- My Application always runs in the background even if it is killed/destroyed or removed from the System Tray.
- The service always listens to the POWER_BUTTON when clicked.
- The SDK supports this since it uses only BroadCast Receivers and Services.
I am just writing down the steps in case the link may not work or be removed in future:
1.First we will create a broadcast receiver which can listen and process android screen on / off broadcast event as below.
ScreenOnOffReceiver.java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class ScreenOnOffReceiver extends BroadcastReceiver {
private final static String SCREEN_TOGGLE_TAG = "SCREEN_TOGGLE_TAG";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(Intent.ACTION_SCREEN_OFF.equals(action))
{
Log.d(SCREEN_TOGGLE_TAG, "Screen is turn off.");
}else if(Intent.ACTION_SCREEN_ON.equals(action))
{
Log.d(SCREEN_TOGGLE_TAG, "Screen is turn on.");
}}
}
2. Register And Unregister ScreenOnOffReceiver In Activity.
Now we will create an activity and register ScreenOnOffReceiver in it’s onCreate() method, and unregister the receiver in it’s onDestroy() method as below.
ScreenOnOffActivity.java
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import com.dev2qa.example.R;
import com.dev2qa.example.broadcast.receiver.ScreenOnOffReceiver;
public class ScreenOnOffActivity extends AppCompatActivity {
private ScreenOnOffReceiver screenOnOffReceiver = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen_on_off);
setTitle("dev2qa.com - Keep BroadcastReceiver Running After App Exit.");
// Create an IntentFilter instance.
IntentFilter intentFilter = new IntentFilter();
// Add network connectivity change action.
intentFilter.addAction("android.intent.action.SCREEN_ON");
intentFilter.addAction("android.intent.action.SCREEN_OFF");
// Set broadcast receiver priority.
intentFilter.setPriority(100);
// Create a network change broadcast receiver.
screenOnOffReceiver = new ScreenOnOffReceiver();
// Register the broadcast receiver with the intent filter object.
registerReceiver(screenOnOffReceiver, intentFilter);
Log.d(ScreenOnOffReceiver.SCREEN_TOGGLE_TAG, "onCreate: screenOnOffReceiver is registered.");
}
@Override
protected void onDestroy() {
super.onDestroy();
// Unregister screenOnOffReceiver when destroy.
if(screenOnOffReceiver!=null)
{
unregisterReceiver(screenOnOffReceiver);
Log.d(ScreenOnOffReceiver.SCREEN_TOGGLE_TAG, "onDestroy: screenOnOffReceiver is unregistered.");
}
}
}
Run Above Activity In Below Steps.
- Start the activity, there is a log message which said the broadcast receiver has been registered in the activity’s onCreate() method.
- Press the power button to turn off screen.
- Press the power button again to turn on screen.
- You can see log data in android monitor console for above steps.
- Type the back menu to exit the activity. You can see the broadcast receiver is unregistered in the activity’s onDestroy() method also.
- Press the power button to execute step 2 , 3 again, but there is not any log data printed in the android monitor console.
3. Register And Unregister Broadcast Receiver In Android Background Service
When you register the broadcast receiver in activity, it will be stopped after the activity exit.
To resolve this problem, we will create an android service object, and register and unregister the broadcast receiver in the service object.
Because the android service object will still run at the background after the activity exit, so the broadcast receiver will still run also after the android app exit.
3.1 Create Android Service Class.
3.1.1 Create A Java Class That Extends android.app.Service.
ScreenOnOffBackgroundService.java
import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
import ScreenOnOffReceiver;
public class ScreenOnOffBackgroundService extends Service {
private ScreenOnOffReceiver screenOnOffReceiver = null;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onCreate() {
super.onCreate();
// Create an IntentFilter instance.
IntentFilter intentFilter = new IntentFilter();
// Add network connectivity change action.
intentFilter.addAction("android.intent.action.SCREEN_ON");
intentFilter.addAction("android.intent.action.SCREEN_OFF");
// Set broadcast receiver priority.
intentFilter.setPriority(100);
// Create a network change broadcast receiver.
screenOnOffReceiver = new ScreenOnOffReceiver();
// Register the broadcast receiver with the intent filter object.
registerReceiver(screenOnOffReceiver, intentFilter);
Log.d(ScreenOnOffReceiver.SCREEN_TOGGLE_TAG, "Service onCreate: screenOnOffReceiver is registered.");
}
@Override
public void onDestroy() {
super.onDestroy();
// Unregister screenOnOffReceiver when destroy.
if(screenOnOffReceiver!=null)
{
unregisterReceiver(screenOnOffReceiver);
Log.d(ScreenOnOffReceiver.SCREEN_TOGGLE_TAG, "Service onDestroy: screenOnOffReceiver is unregistered.");
}
}
}
3.1.2 Add Service Xml Tag In AndroidManifest.xml File.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="put your own package">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".broadcast.activity.ScreenOnOffActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:enabled="true" android:name=".broadcast.service.ScreenOnOffBackgroundService" />
</application>
</manifest>
3.1.3 Change Activity Java Code To Below.
Please notice the java code that start the service object.
Intent backgroundService = new Intent(getApplicationContext(), ScreenOnOffBackgroundService.class);
startService(backgroundService);
ScreenOnOffActivity.java
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import com.dev2qa.example.R;
import com.dev2qa.example.broadcast.receiver.ScreenOnOffReceiver;
import com.dev2qa.example.broadcast.service.ScreenOnOffBackgroundService;
public class ScreenOnOffActivity extends AppCompatActivity {
private ScreenOnOffReceiver screenOnOffReceiver = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen_on_off);
setTitle("dev2qa.com - Keep BroadcastReceiver Running After App Exit.");
Intent backgroundService = new Intent(getApplicationContext(), ScreenOnOffBackgroundService.class);
startService(backgroundService);
Log.d(ScreenOnOffReceiver.SCREEN_TOGGLE_TAG, "Activity onCreate");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(ScreenOnOffReceiver.SCREEN_TOGGLE_TAG, "Activity onDestroy");
}
}
Run the example again, you can see below picture. From the logcat output, we can see the broadcast receiver still running after the android app exit.
(source: dev2qa.com)
Answered By - Niamatullah Bakhshi
Answer Checked By - Katrina (JavaFixing Volunteer)