I am creating an application that launches an action as soon as the user presses the power button three times. After some research, I found that to do this, you first need to create a service that starts the broadcast receiver in order to check the on / off status of the screen.
Basically, I want this to start even when the application is closed. He should start another action as soon as the power button is pressed 3 or more times.
If you know the solution, please give me the full code or link to the answer.
I somehow managed to write this code, but it does not work:
UpdateService.java
This is the class of service I used:
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; } }
Myreceiver.java
import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; 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); } }
mainfest.xml
<receiver android:name=".MyReceiver"> <intent-filter> <action android:name="android.intent.action.SCREEN_OFF"/> <action android:name="android.intent.action.SCREEN_ON"/> </intent-filter> </receiver> <service android:name=".UpdateService" />
Also indicate if I need to include anything in my mainactivity.java file or redirect the action.
I used these permissions:
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.READ_CONTACTS" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.SEND_SMS" /> <uses-permission android:name="android.permission.RECEIVE_SMS" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />