Skip to content

Instantly share code, notes, and snippets.

@simlegate
Last active April 14, 2021 02:44
Show Gist options
  • Save simlegate/5545512 to your computer and use it in GitHub Desktop.
Save simlegate/5545512 to your computer and use it in GitHub Desktop.
something should android do when screen on and screen off .
package com.simlegate.saveresource;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ScreenActionReceiver screenactionreceiver = new ScreenActionReceiver();
registerReceiver(screenactionreceiver, screenactionreceiver.getFilter());
}
}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.simlegate.saveresource"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".ScreenActionReceiver">
<intent-filter android:priority="90000">
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
</application>
</manifest>
package com.simlegate.saveresource;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
public class ScreenActionReceiver extends BroadcastReceiver {
private String TAG = "ScreenActionReceiver";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(Intent.ACTION_SCREEN_ON.equals(action))
{
Log.d(TAG, "screen is on...");
}
else if(Intent.ACTION_SCREEN_OFF.equals(action))
{
Log.d(TAG, "screen is off...");
}
else if(Intent.ACTION_USER_PRESENT.equals(action))
{
Log.d(TAG, "screen is unlock...");
}
}
public IntentFilter getFilter(){
final IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_SCREEN_ON);
return filter;
}
}
@LunaticPrakash
Copy link

screen is unlock is not working

@georgeraveen
Copy link

Does this app work even in the background??

@maqsats
Copy link

maqsats commented Jan 6, 2021

This doesn't work in kill state

@LunaticPrakash
Copy link

Does this app work even in the background??

If you don't remove it from Recents menu it will work. If you want it to run always even if you clear it from recent then register it as a service.
PS - I'm currently not working in Android xd, just trying other techs. So, refer to someone else in case, you get error

@LunaticPrakash
Copy link

This doesn't work in kill state

I guess, You have to register the app as a service to make it work in kill state

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment