解锁android设备时重启活动

时间:2012-08-01 08:47:55

标签: android android-activity

在我的Android应用程序中,我使用广播接收器启动活动。如果我的设备在此活动期间锁定,并且如果我解锁它,那么活动重启意味着它再次在创建时运行 请帮我解决这个问题

提前致谢

3 个答案:

答案 0 :(得分:0)

您在AndroidManifest.xml中的活动声明是什么? 我想你应该将launchMode指定为“singleTask”:

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

^ - ^

答案 1 :(得分:0)

在你的Manifest中你有这样的事情:

<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>

如果是,请妥善处理。

答案 2 :(得分:0)

用于检测屏幕和屏幕关闭注册广播接收器,如:

<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>

在活动或服务中

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) {

     }

收件人代码,系统会通知您是否发生屏幕开启/关闭:

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)){

     }
 }

}