Skip to content

Instantly share code, notes, and snippets.

@darwis059
Forked from simlegate/MainActivity.java
Created April 14, 2021 02:44
Show Gist options
  • Select an option

  • Save darwis059/84883dcbba472b06e1b174930ace6b50 to your computer and use it in GitHub Desktop.

Select an option

Save darwis059/84883dcbba472b06e1b174930ace6b50 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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment