nvavadiya
nvavadiya

Reputation: 127

Activity restart when unlock android device

In my android application I am starting the activity using broadcast receiver. If my device lock during this activity and if I unlock it then activity restart mean it run on create again Please help me to solve this problem

Thanks in advance

Upvotes: 0

Views: 1718

Answers (3)

Yash
Yash

Reputation: 1751

for detect screen on and screen off register a broadcast reciver like:

<receiver android:name="receiverScreen">
    <intent-filter> 
        <action android:name="android.intent.action.SCREEN_ON" />
        <action android:name="android.intent.action.SCREEN_OFF" />
<action android:name="android.Intent.ACTION_USER_PRESENT" />
    </intent-filter> 
</receiver>

In Activity or Service:

try {
          IntentFilter if= new IntentFilter(Intent.ACTION_SCREEN_ON);

          if.addAction(Intent.ACTION_SCREEN_OFF);
if.addAction(Intent.ACTION_USER_PRESENT);

          BroadcastReceiver mReceiver = new receiverScreen();

          registerReceiver(mReceiver, if);
     } catch (Exception e) {

     }

receiver code where System inform you if Screen on/off happen:

public class receiverScreen extends BroadcastReceiver {

 @Override
 public void onReceive(Context context, Intent intent) {

     if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)){

     }
    if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){

     }
if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)){

     }
 }

}

Upvotes: 0

Shishir Shetty
Shishir Shetty

Reputation: 2049

In your Manifest do you have anything like this:

<action android:name="android.intent.action.USER_PRESENT" />

        <receiver android:name="com.activities.app" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

If yes, then please handle it properly.

Upvotes: 0

enjoy-writing
enjoy-writing

Reputation: 520

What's your activity declaration in AndroidManifest.xml? I think u should appoint launchMode to "singleTask" like that:

<activity android:name=".Youracticity" android:launchMode="singleTask" android:configChanges="orientation|keyboardHidden" android:screenOrientation="portrait">
</activity>

^-^

Upvotes: 1

Related Questions