Last active
April 14, 2021 02:44
-
-
Save simlegate/5545512 to your computer and use it in GitHub Desktop.
something should android do when screen on and screen off .
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Does this app work even in the background??
This doesn't work in kill state
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
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
screen is unlock is not working