How to save work in the background

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" /> 
+6
source share
7 answers

You do not have complete code to know for sure, but let me make an educated guess:

You didn’t implement the application or activity, right?

I think you need at least one of them to register the receiver in callback onCreate () onResume () by simply specifying the receiver in the MIGHT NOT work manifest.

And debugging in eclipse or any other IDE with Android integration can be very useful, since it shows the processes of your application, and you can know for sure whether they are brought up or not. A little debugging should tell you if your receiver is working properly.

if you want to share other code, we can, of course, discuss further.

0
source

Try it?

 MyService public class MyService extends Service { @Override public void onCreate() { super.onCreate(); ... if (!registered) { MyReceiver mybroadcast = new MyReceiver() { registerReceiver(mybroadcast, new IntentFilter(Intent.ACTION_SCREEN_ON)); registerReceiver(mybroadcast, new IntentFilter(Intent.ACTION_SCREEN_OFF)); } } } MyReceiver public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { ... } } BootStartUpReciever public class BootStartUpReciever extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // Start Service On Boot Start Up Intent service = new Intent(context, MyService.class); context.startService(service); } } Manifest <service android:name="my.package.MyService" /> <receiver android:name="my.package.BootStartUpReciever" android:enabled="true" android:exported="true" > <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </receiver> 
0
source

I think there is no need for a service class for your requirement. Because you will start the action when the screen is called on the action. Please send my recommendations below and share your thoughts.

 //Application class. public class MyApplication extends Application { static String TAG = "MyApplication"; @Override public void onCreate() { super.onCreate(); IntentFilter intentFilter = new IntentFilter(Intent.ACTION_SCREEN_ON); intentFilter.addAction(Intent.ACTION_SCREEN_OFF); registerReceiver(new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { // screen off if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { Log.d(TAG, Intent.ACTION_SCREEN_OFF); //screen on } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { Log.d(TAG, Intent.ACTION_SCREEN_ON); // To open your main activity. Intent i = new Intent(); i.setClass(context, MainActivity.class); i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i); } } }, intentFilter); } } // Manifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.hopabit.wakeup" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" /> <application android:name=".MyApplication" android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.hopabit.wakeup.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> </application> </manifest> 
0
source

For your work to work, you need to insert it into the stream. Make sure that service is in an additional thread, because the service for itself is not an additional process or thread. This can be done, for example, using the Handler .

 HandlerThread thread = new HandlerThread(SERVICE_THREAD_NAME); thread.start(); handlerThreadId = thread.getId(); serviceLooper = thread.getLooper(); serviceHandler = new ServiceHandler(serviceLooper); 
0
source

I saw your code. You are very close to a solution.

register a widescreen receiver for both the event screen and the screen inside the onCreate () function for your activity

 if (!registered) { MyReceiver mybroadcast = new MyReceiver() { registerReceiver(mybroadcast, new IntentFilter(Intent.ACTION_SCREEN_ON)); registerReceiver(mybroadcast, new IntentFilter(Intent.ACTION_SCREEN_OFF)); } 

To maintain the int counter in general preferences or elsewhere, first initialize it with 0 and increase this counter in onReceive ():

 public class MyReceiver extends BroadcastReceiver { private boolean screenOff; @Override public void onReceive(Context context, Intent intent) { int counter = get counter value from shared preference. if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { screenOff = true; //increment the counter++ } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { screenOff = false; //increment the counter++ } if(counter>3){ Intent i = new Intent(context, UpdateService.class); i.putExtra("screen_state", screenOff); context.startService(i); } } } 
0
source

You can start the service and repeat it often, as well as listen to the event at boot. You can use this link, which has a service that runs all the time. https://github.com/commonsguy/cwac-wakeful This may help in your problem.

0
source

I think you need to use IntentService.

I will tell you why:

The service runs in a user interface thread (even if in the background), so it’s better to interact with the user interface, but on the other hand, they should not work for a long period of time as you want. In addition, IntentService runs on a different thread, so it's best to use them for background tasks that are larger.

What you need to do is save the time on / off the screen in memory (using the Application object) or another Singleton.

If you need more help, just let me know.

0
source

All Articles