I used BroadcastReciver to receive if user unlock screen or rebooted phone (user present) and launch activity but when I unlock screen nothing works . My Idea is to get user state and launch my app several times like pop up advertiser
here is my receiver code :
public class recieve extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED) || intent.getAction().equalsIgnoreCase(Intent.ACTION_SCREEN_ON) || intent.getAction().equalsIgnoreCase(Intent.ACTION_USER_PRESENT)) {
Intent serviceIntent = new Intent(context, serviceonboot.class);
context.startService(serviceIntent);
}
}
}
and this the service to start when user is present
public class serviceonboot extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
// here you can add whatever you want this service to do
startActivity(new Intent(this,BreakActivity.class));
}
}
and here is my manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zsoft.john.eyeguard">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:allowBackup="true"
android:icon="@drawable/eyeguard"
android:label="Eye Guard"
android:roundIcon="@drawable/eyeguard"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<activity android:name=".BreakActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<receiver
android:name="com.zsoft.john.eyeguard.recieve"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SCREEN_ON" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SCREEN_OFF" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<data android:scheme="package" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
<service android:name="com.zsoft.john.eyeguard.serviceonboot"></service>
</application>
</manifest>
Override the mathod called onsavedinstatestate and onrestor einstancestate of android activity or fragment.