Last active
October 9, 2018 07:11
-
-
Save alitamoor65/3acd909bf8081a50d05a415a3adfa868 to your computer and use it in GitHub Desktop.
Follow the step the make it done 1. Create a java class and name `UnlockReceiver.java` whatever you want :) 2. Add the following `UnlockReceiver.java` code in your whatever java class 3. Register your receiver in your manifest file by adding following lines ```java <receiver android:name=".textonphoto.AdsControler.UnlockReceiver.java" android:en…
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
<receiver | |
android:name=".textonphoto.AdsControler.UnlockReceiver.java" | |
android:enabled="true"> | |
<intent-filter> | |
<action android:name="android.intent.action.BOOT_COMPLETED" /> | |
<action android:name="android.intent.action.SCREEN_OFF" /> | |
<action android:name="android.intent.action.SCREEN_ON" /> | |
<action android:name="android.intent.action.USER_PRESENT" /> | |
<category android:name="android.intent.category.DEFAULT"></category> | |
</intent-filter> | |
</receiver> |
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.dluxtools.photext.textonphoto.AdsControler; | |
import android.app.KeyguardManager; | |
import android.content.BroadcastReceiver; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.nfc.Tag; | |
import android.os.Build; | |
import android.os.Handler; | |
import android.os.PowerManager; | |
import android.util.Log; | |
import com.google.android.gms.ads.AdListener; | |
import com.google.android.gms.ads.AdRequest; | |
import com.google.android.gms.ads.InterstitialAd; | |
import com.google.android.gms.ads.MobileAds; | |
public class UnlockReceiver extends BroadcastReceiver { | |
Context context=null; | |
final static String TAG = "Receiver"; | |
private InterstitialAd mInterstitialAd; | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
this.context=context; | |
Log.i(TAG, "onReceive: " + intent.getAction()); | |
mInterstitialAd = new InterstitialAd(context); | |
mInterstitialAd.setAdUnitId("ca-app-pub-2351400565905544/6243970224"); | |
mInterstitialAd.loadAd(new AdRequest.Builder().build()); | |
showAdd(); | |
mInterstitialAd.setAdListener(new AdListener() { | |
@Override | |
public void onAdClosed() { | |
mInterstitialAd.loadAd(new AdRequest.Builder().build()); | |
} | |
@Override | |
public void onAdFailedToLoad(int i) { | |
mInterstitialAd.loadAd(new AdRequest.Builder().build()); | |
} | |
}); | |
isDeviceLocked(context); | |
if(!isDeviceLocked(context)){ | |
Log.i(TAG, "onReceive: " + intent.getAction()); | |
} | |
} | |
void showAdd(){ | |
Handler handler = new Handler(); | |
Runnable runnable = new Runnable() { | |
@Override | |
public void run() { | |
if (mInterstitialAd.isLoaded()) { | |
mInterstitialAd.show(); | |
} else { | |
mInterstitialAd.loadAd(new AdRequest.Builder().build()); | |
} | |
} | |
}; | |
handler.postDelayed(runnable,5000); | |
} | |
public static boolean isDeviceLocked(Context context) { | |
boolean isLocked = false; | |
// First we check the locked state | |
KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE); | |
boolean inKeyguardRestrictedInputMode = keyguardManager.inKeyguardRestrictedInputMode(); | |
if (inKeyguardRestrictedInputMode) { | |
isLocked = true; | |
} else { | |
// If password is not set in the settings, the inKeyguardRestrictedInputMode() returns false, | |
// so we need to check if screen on for this case | |
PowerManager powerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE); | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) { | |
isLocked = !powerManager.isInteractive(); | |
} else { | |
//noinspection deprecation | |
isLocked = !powerManager.isScreenOn(); | |
} | |
} | |
Log.i(TAG,String.format("Now device is %s.", isLocked ? "locked" : "unlocked")); | |
return isLocked; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment